如何让 py.test 识别子目录中的 conftest.py? [英] How do I get py.test to recognize conftest.py in a subdirectory?

查看:62
本文介绍了如何让 py.test 识别子目录中的 conftest.py?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我只是失去了一个的的试图找出为什么<代码> py.test 的没有执行我的autouse,会话范围的安装和拆卸灯具.最后,我偶然发现了(此评论的帽子提示!)插件文档:

So, I just lost a day trying to find out why py.test isn't executing my autouse, session-scoped setup and teardown fixtures. In the end I stumbled over (hat tip to this SO comment!) this little tidbit in the plugins documentation:

请注意,默认情况下,不会在工具启动时加载来自子目录的 conftest.py 文件.

Note that conftest.py files from sub directories are by default not loaded at tool startup.

在我的项目中,我的 py.test 文件(conftest.py 和测试文件)位于 tests/ 子目录中,这似乎是一个非常标准的设置.如果我在测试目录中运行 py.test ,一切运行正常.如果我在项目根目录中运行 py.test,测试仍然运行,但 setup/teardown 例程永远不会执行.

In my project, I got my py.test files (conftest.py and tests files) in a tests/ subdirectory, which seems like a pretty standard setup. If I run py.test in the tests directory, everything runs correctly. If I run py.test in the project root directory, the tests still run, but the setup/teardown routines never get executed.

问题:

  • 使用户能够从项目根目录正确运行测试的规范"方式是什么?将 conftest.py 放在根目录对我来说感觉很奇怪,因为我觉得所有与测试相关的文件都应该保留在 tests 子目录中.
  • 为什么(在设计方面)子目录中的 conftest.py 不是默认加载的?我觉得这种行为至少可以说很奇怪,考虑到默认情况下会发现子目录中的 tests,因此在查找 conftest 文件方面似乎也不需要额外的努力.
  • 最后,如何加载子目录中的 conftest.py(即更改默认设置)?我在文档中找不到这个.我想避免额外的控制台如果可能的话,我可以把任何东西放在配置文件中,或者怎么办?
  • What's the "canonical" way to enable users to correctly run tests from the project root dir? Putting conftest.py in the root directory feels strange to me, cause I feel all tests-related files should remain in the tests subdirectory.
  • Why (design-wise) aren't conftest.py's in subdirectories not loaded by default? I find this behaviour curious to say the least, considering that tests in subdirectories are discovered by default, so there seems to be very little additional effort involved in finding conftest files, too.
  • Lastly, how can I have conftest.py in subdirectories load (i.e. change away from the default)? I couldn't find this in the docs. I'd like to avoid additional console arguments if possible, so can I put anything in a config file or whatnot?

非常感谢任何见解和提示,我觉得当我可以为我的项目编写测试时,我失去/浪费了很多时间来诊断这个问题.:-(

Any insight and tips are much appreciated, I feel that I lost/wasted wayy to much time diagnosing this when I could have written tests for my project. :-(

最小示例:

# 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 ======================================================

推荐答案

在#pylib IRC 频道上获得一些帮助后,事实证明这是一个已在 py.test 2.3.4.

After some help on the #pylib IRC channel, it turns out that this was a bug that has been fixed in py.test 2.3.4.

这篇关于如何让 py.test 识别子目录中的 conftest.py?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆