179

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.

simonzack
  • 19,729
  • 13
  • 73
  • 118

3 Answers3

215

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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Bryant
  • 3,011
  • 1
  • 18
  • 26
  • 5
    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 '16 at 23:37
  • 7
    Apparently `addopts` in setup.cfg is giving problems if the path is added there. – simonzack Jan 16 '16 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. – Anurag Sinha Jun 20 '17 at 05: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 02 '22 at 00: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
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • 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 []) – Nam G VU Oct 21 '17 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. – Paul D Smith Jul 18 '18 at 07: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 '20 at 14:42
27

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
JonDoe297
  • 1,601
  • 1
  • 15
  • 21
  • 1
    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 '20 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 '21 at 12:52