Is there a way to specify which pytest tests to run from a file?

前端 未结 8 574
长情又很酷
长情又很酷 2020-12-12 11:05

Is there a way to select pytest tests to run from a file? For example, a file foo.txt containing a list of tests to be executed:

tes         


        
8条回答
  •  天命终不由人
    2020-12-12 11:44

    Specifying tests / selecting tests

    Pytest supports several ways to run and select tests from the command-line.

    Run tests in a module

    pytest test_mod.py

    Run tests in a directory

    pytest testing/

    Run tests by keyword expressions

    pytest -k "MyClass and not method"

    This will run tests which contain names that match the given string expression, which can include Python operators that use filenames, class names and function names as variables. The example above will run TestMyClass.test_something but not TestMyClass.test_method_simple.

    Run tests by node ids

    Each collected test is assigned a unique nodeid which consist of the module filename followed by specifiers like class names, function names and parameters from parametrization, separated by :: characters.

    To run a specific test within a module:

    pytest test_mod.py::test_func

    Another example specifying a test method in the command line:

    pytest test_mod.py::TestClass::test_method

    Run tests by marker expressions

    pytest -m slow

    Will run all tests which are decorated with the @pytest.mark.slow decorator.

    For more information see marks.

    Run tests from packages

    pytest --pyargs pkg.testing

    This will import pkg.testing and use its filesystem location to find and run tests from.

    Source: https://docs.pytest.org/en/latest/usage.html#specifying-tests-selecting-tests

提交回复
热议问题