[py-dev] branch is being merged back

holger krekel hpk at trillke.net
Mon Apr 18 22:00:36 CEST 2005


Hi David, 

On Mon, Apr 18, 2005 at 06:52 -0700, David Ripton wrote:
> On 2005.04.16 21:57:51 +0000, holger krekel wrote:
> > Note that the command line options changed slightly, because 
> > there is a new rule: py.test will consume all lowercase 
> > single-letter options like "-h", "-v", "-x" and so on while
> > applications should add only consume capital cased letters. 
> 
> Haven't looked at the new code yet, so maybe I'm interpreting your
> statement incorrectly, but this seems wrong.

Sorry, i wasn't very precise in my above statement.  py.test
has always been using command line options.  But, for example
with PyPy, we have a couple of extra options to the testing
process (which you can provide through 'conftest.py' files
in your package).  Now the idea is that such test extensions
(what i called "applications" above) should use upper-case
letters while py.test restrains itself to only use lower-case 
ones.  This way py.test has enough future possible options :-) 

So with respect to what a test module finds in sys.argv nothing
has changed.   In fact, if a test module inspects sys.argv 
it will find the complete list of options (provided to py.test). 

> I currently don't rely on the value of sys.argv in unit tests.  If I need 
> to test a script that uses argv, I split out a main function from the 
> top-level "if __name__ == '__main__'" block and make the latter as small 
> and simple as possible (usually just "main()") so I don't have to test it.

makes sense. 

> ... 
> This is occasionally inconvenient.  For example, sometimes a script
> requires a password, and you don't want to hardcode it, and you don't
> want to put it in a file, and you don't want your test to be overly
> interactive, and putting it on the command line feels like the least
> insecure of the simple options.  (I know, someone could use ps to sniff
> it, so this is hardly super-secure.)
> 
> Now, it would be nice to assume that py.test will leave {some of}
> sys.argv alone, so that I have the option of using it.

Yes, although it is not completely obvious how to exactly do it
(also with respect to the underlying optparse-usage). 

> But a rule based on capitalization seems like the wrong way to do this.
> I want to pick option names for my programs based on what's
> user-friendly, not what my test framework allows.

Sure but py.test is usually not running your application, is it? :-) 
Basically i would suggest that you add a special testing command line
option that holds your password information.  E.g. in a conftest.py  
in your package you could add: 

    option = py.test.addptions("my app testing options", 
            Option('-P', '--password', action="store", default=None,
                           type="string", dest="password",
                           help="password used for XXX connections."), 

And then in your test_whatever.py: 

    from mypackage.conftest import option 
    .... 
    def test_something(): 
        myscript.main(...use option.password... ) 

If you then are in your package and issue 'py.test -h' it will 
have your option. 

cheers, 

    holger



More information about the Pytest-dev mailing list