Python - Some useful Pytest Commands

March 03, 2021


In this post, we will explore some useful command line options for running python unit tests.

Running Selective Test File

Assumming you have lot of tests written, and is in seperate files.

To run only tests within a particular file, you just pass the path to that file only.

pytest <path_to_pytest_file>

Running Selective Test case across

Many times, you may want tp run only one test case across your pool of test cases. You would want to run it by name.

pytest -k <test_name>

Note: It will run all those test cases where this test_name is occurring. It works kind of `string contains’.

So if your test names are:


And, if you run

pytest -k test_check1

It will run following test cases:


You may have some print statements in between and want to know the output.

pytest -s

You can use it with -k option as well.

pytest -k check_1 -s

Enable Logging in Pytest

Often times, the Python logging does not come in pytest. To enable this,

pytest --log-cli-level=INFO 

pytest --log-cli-level=INFO -k check_1 -s

Similar Posts

Latest Posts