|
pip install pytest
|
|
pytest test_mod.py
|
Run tests in module
|
|
pytest testing/
|
Run multiple modules
|
|
pytest test_mod.py::test_func
|
Run a specific test within a module.
|
|
pytest test_mod.py::TestClass::test_method
|
Run a specific method of a class.
|
|
pytest allows you to use the standard Python assert for verifying expectations and values in Python tests. For example, you can write the following:
|
|
assert a % 2 == 0 |
assert that you have a certain value.
|
assert a % 2 == 0, |
specify a message with the assertion like this
|
|
@pytest.mark.<markername>
|
mark it in tests
|
|
pytest tests/ -m "sanitytests"
|
run using -m flag
|
@pytest.mark.skip( |
|
Test if the exception is raised
|
def test_zero_division(): |
|
Test for specific exception info
|
def test_recursion_depth(): |
pip install pytest-xdist |
Pytest does not run in parallel by default, therefor we need to enable this behaviour through a plugin.
|
|
In testing, a fixture provides a defined, reliable and consistent context for the tests. This could include environment (for example a database configured with known parameters) or content (such as a dataset).
|
|
@pytest.fixture |
Create fixture in conftest
|
def test_divisible_by_3(input_value): |
Use it in tests
|
@pytest.fixture |
using yield to setup and teardown test environment
|
|
@pytest.fixture(scope="function")
|
Pytest fixtures have scope. This scope controls how often the fixture is executed or, in other words, how often the setup and teardown of the fixture is performed.
|
|
@pytest.fixture(scope="module")
|
|
|
@pytest.fixture(scope="class")
|
|
|
@pytest.fixture(scope="session")
|
|
|
The builtin pytest.mark.parametrize decorator enables parametrization of arguments for a test function
|
|
Define parametrized tests
|
@pytest.mark.parametrize("num, output", |
|
Running specific tests of a parametrized test.
|
|
test_mod.py::test_multiplication_11[2-22]
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.