How to test single file under pytest
Asked Answered
P

3

201

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.

Pensionary answered 16/1, 2016 at 23:29 Comment(0)
C
244

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.

Cordovan answered 16/1, 2016 at 23:33 Comment(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.Pensionary
Apparently addopts in setup.cfg is giving problems if the path is added there.Pensionary
@Pensionary 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.Schnapps
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.Brothel
M
107

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
Midsection answered 11/10, 2017 at 13:2 Comment(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'>])Imperil
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.Explicable
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.Titulary
A
28

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
Ashcan answered 17/1, 2019 at 20:39 Comment(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.1Hunnish
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_nameCosh

© 2022 - 2024 — McMap. All rights reserved.