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?
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.
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 – 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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With