Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test single file under pytest

Tags:

python

pytest

People also ask

How do I run multiple files in pytest?

Suppose we have multiple files , say test_sample2.py , test_sample3.py. To run all the tests from all the files in the folder and subfolders we need to just run the pytest command. This will run all the filenames starting with test_ and the filenames ending with _test in that folder and subfolders under that folder.

How does pytest know what tests to run?

Pytest supports several ways to run and select tests from the command-line. This will run tests which contain names that match the given string expression (case-insensitive), which can include Python operators that use filenames, class names and function names as variables.

What files pytest use?

Running pytest without mentioning a filename will run all files of format test_*. py or *_test.py in the current directory and subdirectories. Pytest automatically identifies those files as test files.


simply run pytest with the path to the file

something like

pytest tests/unit/some_test_file.py


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

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