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, run:

pytest path/to/

If you want to run all the tests related to pie, such as and 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 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/ -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.