Skip to content

Pytest Commands

Source: Very Academy

This guide outlines various commands to run tests using pytest.

  • Run pytest:
    pytest
    
  • Run pytest specify markers:

    pytest -m <marker>
    pytest -m "<marker> and <marker>"
    

  • Run all tests in a directory:

    pytest path/to/directory
    

  • Run tests in a specific file:

    pytest path/to/test_file.py
    

  • Run a specific test function:

    pytest path/to/test_file.py::test_function
    

  • Run tests that match a specific substring:

    pytest -k "substring"
    

  • Run tests based on markers:

    pytest -m "marker_name"
    pytest -m "model and model_structure"
    pytest -m "model or model_structure"
    

  • Run tests and generate code coverage report:

    pytest --cov=path/to/package
    

  • Run tests and display print statements:

    pytest -s
    

  • Run tests and stop at the first failure:

    pytest --exitfirst
    

  • Run tests in parallel:

    pytest -n num_workers
    

  • Run tests with verbose output:

    pytest -v
    

  • Run tests and ignore warnings:

    pytest -p no:warnings
    

  • Run tests and rerun failed tests:

    pytest --reruns num_reruns
    

  • Run tests and mark them as slow:

    pytest --slow
    

  • Run tests and mark them as xfail (expected to fail):

    pytest --xfail