2013-09-01 23 views
15

因此,我刚刚失去了试图找出为什么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 ====================================================== 
+0

适合我。 http://ascii.io/a/5263 – falsetru

+0

是的,同时我发现这是一个已经修复的错误。 – Christoph

回答

9

在#pylib IRC频道一些帮助之后,原来这是一个已在py.test 2.3.4中修复的错误。

+1

这不适合我。我在'tests /'中有'conftest.py',但它没有效果。 – orome

+0

@raxacoricofallapatorius可能是最好的报告pytest的错误:https://github.com/pytest-dev/pytest/issues – Christoph

+0

其实,在我的情况下,它有点不同。我的软件包/项目的根目录不是'test /',而是包含在我的包中的(独立)模块的文件夹中。它不应该在那里吗?我的理解是每个模块都可以有自己的'test /'文件夹(并且测试放在这样的文件夹下运行)。情况并非如此。我应该有一个'测试/'文件夹(或我的'conftest。py')在我的包的根? – orome

相关问题