Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing python script arguments to test modules

I have several test modules that are all invoked together via a driver script that can take a variety of arguments. The tests themselves are written using the python unittest module.

import optparse
import unittest
import sys
import os

from tests import testvalidator
from tests import testmodifier
from tests import testimporter

#modify the path so that the test modules under /tests have access to the project root
sys.path.insert(0, os.path.dirname(__file__))

def run(verbosity):
    if verbosity == "0":
            sys.stdout = open(os.devnull, 'w')

    test_suite = unittest.TestSuite()
    test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(testvalidator.TestValidator))
    test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(testmodifier.TestModifier))
    test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(testimporter.TestDataImporter))

    unittest.TextTestRunner(verbosity=int(verbosity)).run(test_suite)

if __name__ == "__main__":

    #a simple way to control output verbosity
    parser = optparse.OptionParser()
    parser.add_option("--verbosity", "--verbosity", dest="verbosity", default="0")
    (options, args) = parser.parse_args()

    run(options.verbosity)

My issue is that, within these test modules, I have certain tests I'd like to skip based on different parameters passed to the driver. I'm aware that unittest provides a family of decorators meant to do this, but I don't know the best way to pass this information on to the individual modules. If I had a --skip-slow argument, for example, how could I then annotate tests as slow, and have them skipped?

Thank you for your time.

like image 418
zchtodd Avatar asked Dec 13 '11 21:12

zchtodd


People also ask

How do you pass arguments to a python script?

In Python, arguments are passed to a script from the command line using the sys package. The argv member of sys ( sys.argv) will store all the information in the command line entry and can be accessed inside the Python script. Python’s getopt module can also be used to parse named arguments. Let’s go through some examples.

How do I pass a variable to a python script?

One approach is to parameterize some of the variables originally hard coded in the script, and pass them as arguments to your script. If you have only 1 or 2 arguments, you may find sys.argv is good enough since you can easily access the arguments by the index from argv list.

How to read command-line arguments in Python script?

How to Read Command-line arguments in Python Script? 1. Reading Python Command-line arguments using the sys module The command-line arguments are stored in the sys module... 2. Parsing Command-line arguments using the getopt module Python getopt module works in a similar way as the Unix getopt... 3. ...

How do I retrieve named arguments from the command line in Python?

To retrieve named arguments from the command line we’ll use Python’s getopt module. getopt is built into base Python so you don’t need to install it. Let’s start a new script that uses both sys and getopt to parse command-line arguments. The script will have the possibility of four named arguments, ‘help’, ‘input’, ‘user’, and ‘output’.


2 Answers

I had in fact been wondering this myself, and finally found the solution.

main file...

...
if __name__ == '__main__':
    args = argparser()

    from tests import *

    ...

And in your test modules, just do:

from __main__ import args

print args

I tested this out, and it worked rather nicely. Nice thing is how simple it is, and it's not too much of a hack at all.

like image 158
John Doe Avatar answered Nov 04 '22 23:11

John Doe


You can use nose test runner with the attrib plugin that lets you select test cases based on attributes. In particular, the example in the plugin documentation uses @attr(slow) to mark slow test cases.

After that, from the command line:

  • To select all the test cases marked as slow:

    $ nosetests -a slow

  • To select all the test cases not marked as slow:

    $ nosetests -a '!slow'

like image 32
jcollado Avatar answered Nov 04 '22 23:11

jcollado