繁体   English   中英

如何在pytest下测试单个文件

[英]How to test single file under pytest

你如何测试pytest中的单个文件? 我只能在文档中找到忽略选项而不“仅测试此文件”选项。

最好这可以在命令行而不是setup.cfg ,因为我想在ide中运行不同的文件测试。 整个套房需要很长时间。

只需使用文件路径运行py.test即可

就像是

py.test tests/unit/some_test_file.py

这很简单:

$ 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标志来运行标记测试的子集。

test_file.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

这对我有用:

python -m pytest -k some_test_file.py

这也适用于各个测试功能:

python -m pytest -k test_about_something

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM