Jake Trent

Run Single pytest Test

How to run just one test with pytest.

After you make your first test, you’ll make hundreds more. But to run them all takes a lot of time. So if you’re working in one corner of the code, just run the tests that you care about.

Run a single file

To run a single test file, such as pie_test.py, run:

pytest path/to/pie_test.py

If you want to run all the tests related to pie, such as pie_service_test.py and pie_model_test.py at the same time, run the same command with the substring:

pytest path/to/pie*

You can put multiple paths on that command, space-delimited.

Run a single test in the file

What if you have many different tests in the pie_test.py file, such as:

def test_one():
    # ...

def test_two():
    # ...

def test_three():
    # ...

And you only want to run test_two, you can use the -k parameter to match on function name substring:

pytest path/to/pie_test.py -k two

Now you can narrow down and run only the test you want to. Next, watch the source and re-run the test on change.