因此,我刚刚失去了天试图找出为什么py.test
不执行我的汽车,会话范围设置和拆卸夹具。最后,我绊倒了(帽尖到this SO comment!)在这plugins documentation小珍闻:如何让py.test识别子目录中的conftest.py?
注意,从子目录conftest.py文件是通过工具启动时不加载默认。
在我的项目,我在tests/
子目录,这似乎是一个相当标准的建立得到了我的py.test文件(conftest.py
和测试文件)。如果我在测试目录中运行py.test
,所有内容都可以正常运行。如果我在项目根目录下运行py.test
,测试仍在运行,但安装/拆卸例程从未得到执行。
问题:
- 什么是“规范”的方式,使正常运行从项目的根目录的测试用户?将
conftest.py
放在根目录下会让我觉得很奇怪,因为我觉得所有与测试有关的文件都应该保留在tests
子目录中。 - 为什么(设计明智)不
conftest.py
的子目录中的默认不加载?我觉得这种行为很好奇,至少可以这么说,考虑到测试在子目录中是默认发现的,所以在查找conftest文件中似乎也只有很少的额外努力。 - 最后,我怎么能在子目录负荷
conftest.py
(即更改默认的距离)?我无法在文档中找到它。如果可能的话,我想避免额外的控制台 参数,所以我可以把任何东西放在配置文件或 whatnot?
任何洞察力和技巧都非常赞赏,我觉得当我可以为我的项目编写测试时,我很失望/浪费时间去诊断这个问题。 :-(
最少例如:
# content of tests/conftest.py
# adapted from http://pytest.org/latest/example/special.html
import pytest
def tear_down():
print "\nTEARDOWN after all tests"
@pytest.fixture(scope="session", autouse=True)
def set_up(request):
print "\nSETUP before all tests"
request.addfinalizer(tear_down)
测试文件:
# content of tests/test_module.py
class TestClassA:
def test_1(self):
print "test A1 called"
def test_2(self):
print "test A2 called"
class TestClassB:
def test_1(self):
print "test B1 called"
控制台输出:
pytest_experiment$ py.test -s
======================================================== test session starts =========================================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.2
plugins: cov
collected 3 items
tests/test_module.py test A1 called
.test A2 called
.test B1 called
.
====================================================== 3 passed in 0.02 seconds ======================================================
pytest_experiment$ cd tests/
pytest_experiment/tests$ py.test -s
======================================================== test session starts =========================================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.2
plugins: cov
collected 3 items
test_module.py
SETUP before all tests
test A1 called
.test A2 called
.test B1 called
.
TEARDOWN after all tests
====================================================== 3 passed in 0.02 seconds ======================================================
适合我。 http://ascii.io/a/5263 – falsetru
是的,同时我发现这是一个已经修复的错误。 – Christoph