Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does unittest allow single case/suite testing through "setup.py test"?

Tags:

I'm a newbie when it comes to python unit testing, but I'm eager to learn! I just read python setup.py test can run all suites derived from unittest classes. I wonder if I also can use setup.py to run a single suite and/or a single test case, maybe adding some modifier to the previous command like python setup.py tests suitename. If so, can you please point me to any docs/examples?

like image 616
jmborr Avatar asked Jan 16 '14 16:01

jmborr


People also ask

What is setup in unittest Python?

unittest provide others functions such as: assertTrue() or assertFalse() to verify a condition. assertRaises() to verify that a specific exception gets raised. setUp() and tearDown() methods to define instructions that will be executed before and after each test method.

Can you run unittest with pytest?

pytest supports running Python unittest -based tests out of the box. It's meant for leveraging existing unittest -based test suites to use pytest as a test runner and also allow to incrementally adapt the test suite to take full advantage of pytest's features.

Which is better unittest or pytest?

Which is better – pytest or unittest? Although both the frameworks are great for performing testing in python, pytest is easier to work with. The code in pytest is simple, compact, and efficient. For unittest, we will have to import modules, create a class and define the testing functions within that class.


2 Answers

You guys are all wrong, setup.py test can be used with the -s option the same way python -m unittest does:

cd root_of_your_package python setup.py test -s tests.TestClass.test_method 
like image 132
rodfersou Avatar answered Nov 02 '22 22:11

rodfersou


The setup.py test runner is rather limited; it only supports letting you specify a specific module. The documentation for the command-line switches is given when you use the --help switch:

python setup.py test --help Common commands: (see '--help-commands' for more)   [ ... cut ... ]  Options for 'test' command:   --test-module (-m)  Run 'test_suite' in specified module   --test-suite (-s)   Test suite to run (e.g. 'some_module.test_suite')    [ ... more cut ... ] 

so python setup.py test -m your.package.tests.test_module would limit running the tests from the test_module.py file only.

All the test command does, really, is make sure your egg has been built already, extract the test_suite value from setup() metadata, configure a test loader that understands about zipped eggs, then run the unittest.main() function.

If you need to run a single test only, have already built your egg, are not running this with a zipped egg, then you can also just use the unittest command line interface, which does pretty much everything else:

python -m unittest yourpackage.tests.TestClass.test_method 

would instruct unittest to only run a very specific test method.

like image 44
Martijn Pieters Avatar answered Nov 02 '22 23:11

Martijn Pieters