170

How do you test a single file in pytest? I could only find ignore options and no "test this file only" option in the docs.

Preferably this would work on the command line instead of setup.cfg, as I would like to run different file tests in the ide. The entire suite takes too long.

3 Answers 3

194

simply run pytest with the path to the file

something like

pytest tests/test_file.py

Use the :: syntax to run a specific test in the test file:

pytest test_mod.py::test_func

Here test_func can be a test method or a class (e.g.: pytest test_mod.py::TestClass).

For more ways and details, see "Specifying which tests to run" in the docs.

4
  • 4
    Ok, this appears to be what pycharm is doing but it's still running the whole suite. Running py.test on the commandline is giving segfaults for some reason. I suppose this is outside the scope of the original question so I'll accept your answer if I can get it to work.
    – simonzack
    Jan 16, 2016 at 23:37
  • 7
    Apparently addopts in setup.cfg is giving problems if the path is added there.
    – simonzack
    Jan 16, 2016 at 23:53
  • 1
    @simonzack I guess you want to run a single test case out of multiple test case present in the file. Try this: py.test test_basic.py -k test_first here test_first is a test case present in my test_basic.py file. Jun 20, 2017 at 5:55
  • Use the :: syntax to run a single test in a test file, e.g.: pytest tests/test_models.py::TestMyModel. TestMyModel is a class that contains a subset of tests.
    – FisNaN
    Mar 2, 2022 at 0:34
103

This is pretty simple:

$ pytest -v /path/to/test_file.py

The -v flag is to increase verbosity. If you want to run a specific test within that file:

$ pytest -v /path/to/test_file.py::test_name

If you want to run test which names follow a patter you can use:

$ pytest -v -k "pattern_one or pattern_two" /path/to/test_file.py

You also have the option of marking tests, so you can use the -m flag to run a subset of marked tests.

test_file.py

def test_number_one():
    """Docstring"""
    assert 1 == 1


@pytest.mark.run_these_please
def test_number_two():
    """Docstring"""
    assert [1] == [1]

To run test marked with run_these_please:

$ pytest -v -m run_these_please /path/to/test_file.py
3
  • 3
    for path/to/test.py::test_method, I got error > ERROR: not found: /home/namgivu/NN/code/myproject/tests/models/test_bill.py::test_generate_for_today_normal_cycle (no name '/home/namgivu/NN/code/myproject/tests/models/test_bill.py::test_generate_for_today_normal_cycle' in any of [<Module 'tests/models/test_bill.py'>])
    – Nam G VU
    Oct 21, 2017 at 19:40
  • 1
    What about parameterised tests, with IDs? -k my_test[a test id here] does not work and the best I've managed thus far is -k "my_test and a and test and id and here" which is hardly a friendly format. Jul 18, 2018 at 7:32
  • 1
    The examples given in this example (in particular the one mixing path and node selection syntax) should be added to the pytest documentation, as it's really not obvious as is IMO.
    – cjauvin
    Apr 11, 2020 at 14:42
23

This worked for me:

python -m pytest -k some_test_file.py

This works for individual test functions too:

python -m pytest -k test_about_something
2
  • this pytest directory_to_tests/some_test_file.py does not work for me. I'm on windows python 3.8.2 and pytest 6.0.1
    – minghua
    Aug 14, 2020 at 16:12
  • 4
    i dont recommend -k, because pytest will try to search for that function. image you have 10000 tests in your project. its better to just specify the path and the function with pytest path/path/.../path.py::function_name
    – alexzander
    Dec 29, 2021 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.