How to test single file under pytest

后端 未结 3 839
星月不相逢
星月不相逢 2020-12-23 16:05

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 comman

3条回答
  •  梦毁少年i
    2020-12-23 16:17

    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
    

提交回复
热议问题