I have a test file that contains tests taking quite a lot of time (they send calculations to a cluster and wait for the result). All of these are in specific TestCase class.
Since they take time and furthermore are not likely to break, I'd want to be able to choose whether this subset of tests does or doesn't run (the best way would be with a command-line argument, ie "./tests.py --offline
" or something like that), so I could run most of the tests often and quickly and the whole set once in a while, when I have time.
For now, I just use unittest.main()
to start the tests.
Thanks.
To run only a single specific test you can use:
More information here
Actually, one can pass the names of the test case as sys.argv and only those cases will be tested.
For instance, suppose you have
You can call
to have only account tests, or even
to have both cases tested
I tried @slott's answer:
But that gave me the following error:
The following worked for me:
I'm doing this using a simple
skipIf
:This way I need only decorate an already existing test case with this single line (no need to create test suites or similar, just that one
os.getenv()
call line in the beginning of my unit test file), and as a default this test gets skipped.If I want to execute it despite being slow, I just call my script like this:
You have basically two ways to do it:
I am a strong proponent of he second approach; a unit test should test only a very unit of code, and not complex systems (like databases or clusters). But I understand that it is not always possible; sometimes, creating mock ups is simply too expensive, or the goal of the test is really in the complex system.
Back to option (1), you can proceed in this way:
and then passing the suite to the test runner:
More information on the python documentation: http://docs.python.org/library/unittest.html#testsuite-objects
Or you can make use of the
unittest.SkipTest()
function. Example, add askipOrRunTest
method to your test class like this:Then add a call to this skipOrRunTest method to the very beginning of each of your unit tests like this: