What is pytest
pytest is a testing framework for Python that makes it easy to write and run tests. It allows you to write test functions, test methods, and test classes to test the behavior of your code.
Test by convention
One of the key features of pytest is "test by convention". This means that pytest will automatically discover and run test functions and methods based on certain naming conventions.
For example, if you want to write a test function, you can name it test_ followed by the name of the function or method you want to test. For example:
def test_add(): assert add(2, 3) == 5
pytest will automatically discover and run this test function when you run the tests.
You can also write test methods by creating a class and prefixing the method name with test_. For example:
class TestClass: def test_add(self): assert add(2, 3) == 5
Running pytest tests
To run your pytest tests, you can use the pytest command followed by the name of the file or directory containing your tests. For example:
This will run all the test functions and methods in the test_file.py file.
You can also specify a specific test function or method to run by using the -k option followed by the name of the test. For example:
pytest -k test_add
This will only run the test function or method with the name test_add.
Pytest built-in fixtures
Fixtures are a way to provide setup and teardown for your tests. Pytest has a number of built-in fixtures that you can use to set up your tests.
For example, the tmpdir fixture allows you to create a temporary directory for your tests and provides the path to the directory as a parameter to your test function.
def test_write_to_file(tmpdir): p = tmpdir.join("test.txt") p.write("content") assert p.read() == "content"
You can also use the capsys fixture to capture the output of your tests to stdout and stderr.
def test_print_output(capsys): print("Hello, world!") captured = capsys.readouterr() assert captured.out == "Hello, world!\n"
Pytest plugin extensions
One of the strengths of pytest is its extensibility through plugins. There are a large number of plugins available that provide additional features and functionality for pytest.
For example, the pytest-cov plugin adds support for measuring code coverage during your tests. To use it, you can install the plugin using pip and then run pytest with the --cov option.
pip install pytest-cov pytest --cov=my_module
This will run your tests and generate a code coverage report for the my_module module.
Another popular plugin is pytest-xdist, which allows you to run your tests in parallel across multiple CPU cores or even multiple machines. This can greatly speed up your test suite if you have a large number of tests.
To use pytest-xdist, you can install the plugin using pip and then run pytest with the -n option followed by the number of cores or machines you want to use.
pip install pytest-xdist pytest -n 4
This will run your tests in parallel across 4 CPU cores.
There are many other plugins available for pytest, including plugins for handling specific testing needs such as database testing, web testing, and more. You can find a list of available plugins at https://pytest.org/en/latest/plugins.html.