Forums

Python Unittest select specific test cases from a test suite

Is there a way to run specific tests from a test suite in the unittest framework? TestNG allows the user to mention which test cases to run, using an XML file, I was wondering if there is something similar in Unittest.

Found this via Google:

http://stackoverflow.com/questions/15971735/running-single-test-from-unittest-testcase-via-command-line

Any help? Jim

Hi Jim, Thanks for the reply. I did see that before, but was wondering if it was possible without using the command line

I don't think there's any way of doing it using an XML file, but perhaps you could write a Python script that specified a new suite with the appropriate methods?

@satmaram If you're doing something similar to the SO post and deriving your own test classes from 'unittest.TestCase', why don't you just temporarily inhibit your normal base class, e.g. by deriving from something else:

class OurTcFw(object):  # unittest.TestCase):

and for the test case(s) that you want to test, derive them from e.g.

class DevTcFw(unittest.TestCase):

(or you could switch different source file(s) to achieve the same.)

Wouldn't something similar to that involve much less editing, or have I misconstrued (as usual)?

Jim

Yes that would make it easier, thanks for the tip!

I see you can also 'skip' tests:

https://docs.python.org/3/library/unittest.html#unittest-skipping

So you could have a global string containing the names of tests you wish to execute, and execute all if this string is None. Then you'd just need to assign the string somehow, e.g. from console input. Easy then to execute either (i) a small number of specified tests, or (ii) all of them, without changing your source files?

yeah thats a good alternative as well. Thanks