【发布时间】:2016-04-22 08:39:36
【问题描述】:
如何在 pytest 中测试单个文件?我只能在文档中找到忽略选项,而没有“仅测试此文件”选项。
最好这将在命令行而不是setup.cfg 上工作,因为我想在 ide 中运行不同的文件测试。整个套件耗时太长。
【问题讨论】:
如何在 pytest 中测试单个文件?我只能在文档中找到忽略选项,而没有“仅测试此文件”选项。
最好这将在命令行而不是setup.cfg 上工作,因为我想在 ide 中运行不同的文件测试。整个套件耗时太长。
【问题讨论】:
只需使用文件路径运行pytest
类似
pytest tests/unit/some_test_file.py
【讨论】:
addopts 会出现问题。
这很简单:
$ 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
【讨论】:
path/to/test.py::test_method,我收到错误> 错误:未找到:/home/namgivu/NN/code/myproject/tests/models/test_bill.py::test_generate_for_today_normal_cycle(没有名称'/home/ [-k my_test[a test id here] 不起作用,到目前为止我管理的最好的格式是 -k "my_test and a and test and id and here",这几乎不是一种友好的格式。
这对我有用:
python -m pytest -k some_test_file.py
这也适用于单个测试功能:
python -m pytest -k test_about_something
【讨论】:
pytest directory_to_tests/some_test_file.py 对我不起作用。我在 windows python 3.8.2 和 pytest 6.0.1