Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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

setup.py test

setup.py test is not that flexible, but here's an alternative:

The unittest module can run specific test methods

From the Documentation on unittest

The unittest module can be used from the command line to run tests from modules, classes or even individual test methods:

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method

You can pass in a list with any combination of module names, and fully qualified class or method names.

You can run tests with more detail (higher verbosity) by passing in the -v flag:

python -m unittest -v test_module

For a list of all the command-line options:

python -m unittest -h

in case of pytest run specific test:

python setup.py test --addopts tests/jobs/test_data_monitoring.py::TestDataMonitoring::test_dataframe__bulk_update

In case you use pytest for unit testing you can run a file directly by avoiding the setup.py file.

Running all tests for me is:

python setup.py test

Running a specific test:

pytest --pyargs path/to/test.py

Hope it helps.

(I know this post is already a bit old, but I came here looking for running a way to run pytest tests alone, figured it might be of use to someone)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top