如何在pytes下测试单个文件

2024-10-06 10:34:22 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在pytest中测试单个文件?我只能在文档中找到ignore选项和no“testthisfileonly”选项。

最好在命令行而不是setup.cfg上运行,因为我希望在ide中运行不同的文件测试。整个套间太长了。


Tags: 文件no命令行文档pytest选项setupide
3条回答

这很简单:

$ pytest -v /path/to/test_file.py

-v标志用于增加详细程度。如果要在该文件中运行特定测试:

$ pytest -v /path/to/test_file.py::test_name

如果要运行测试,可以使用以下模式的名称:

$ pytest -v -k "pattern_one or pattern_two" /path/to/test_file.py

您还可以选择标记测试,因此可以使用-m标志来运行标记测试的子集。

测试文件.py

def test_number_one():
    """Docstring"""
    assert 1 == 1


@pytest.mark.run_these_please
def test_number_two():
    """Docstring"""
    assert [1] == [1]

要运行标记为run_these_please的测试:

$ pytest -v -m run_these_please /path/to/test_file.py

只需使用文件的路径运行pytest

有点像

pytest tests/unit/some_test_file.py

这对我有效:

python -m pytest -k some_test_file.py

这也适用于单独的测试功能:

python -m pytest -k test_about_something

相关问题 更多 >