Search code examples
pythontestingcommand-line-argumentsnosetestsargs

Python nosetests: how to access cmd line options? Namely `--failed`


Running: Windows 7, python 3.4 & 2.7

In one of my nosetests plugin, (one that post test data to a website), I need to ascertain if the test is being run with the --failed option or without. If --failed is enabled, that means this test failed the first time and is being run once more to see if that fail was a fluke. If this is a re-run of a failed test I need to direct my plugin to some different behavior vs. if the test is being run for the first time.

In other words, I want to ascertain inside the plugin if we are inside nosetests or nosetests --failed.

How can I access nosetest's command line options from inside a plug in? What variable are the options stored in?

My eventual code will look something like this:

 if <--failed option was invoked with nosetests command>: 
     do something
 else:
     do something different

What's the correct code to replace what's inside <>?


Solution

  • I don't fully understand, but the command line arguments part is easy. Just use the following code:

    from sys import argv as arguments
    
    if "--failed" in arguments :
        do_something()
    else :
        do_something_else()
    

    When you import sys, you have access to the sys.argv