2 - Invoking pytest#
Specifying which tests to run#
To run tests in a module:
pytest test_mod.py
To run tests in a directory:
pytest testing/
To run tests by keyword expression:
pytest -k "MyClass and not method"
The above command will run all modules that contain “MyClass” except those that contain “method”.
To run tests by collection arguments:
# Run a specific test in a module
pytest tests/test_mod.py::test_func
# Run all tests in a class
pytest tests/test_mod.py::TestClass
# Run a specific method in a class
pytest tests/test_mod.py::TestClass::test_method
# Specify parameters for a test
pytest tests/test_mod.py::test_func[x1,y2]
To run tests by marker expressions:
# Run tests with @pytest.mark.slow decorator
pytest -m slow
# Run tests with @pytest.mark.slow(phase=1) decorator
pytest -m "slow(phase=1)"
Getting help on version, option names, environment variables#
pytest --version # shows where pytest was imported from
pytest --fixtures # show available builtin function arguments
pytest -h | --help # show help on command line and config file options
Calling pytest from Python code#
You can invoke pytest from Python code directly:
retcode = pytest.main(["-x", "mytestdir"])