在另一个 Python 包中运行 pytest 测试 [英] Running pytest tests in another Python package

查看:38
本文介绍了在另一个 Python 包中运行 pytest 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我有一个 Python 包(我们称之为 mypackage),里面有一堆我用 pytest 运行的测试.一个特定功能可以有多种可能的实现,因此我使用了 funcarg 机制来运行这些带有参考实现的测试.

Right now, I have a Python package (let's call it mypackage) with a bunch of tests that I run with pytest. One particular feature can have many possible implementations, so I have used the funcarg mechanism to run these tests with a reference implementation.

# In mypackage/tests/conftest.py
def pytest_funcarg__Feature(request):
    return mypackage.ReferenceImplementation

# In mypackage/tests/test_stuff.py
def test_something(Feature):
    assert Feature(1).works

现在,我正在创建一个单独的 Python 包,其中包含更高级的实现 (fancypackage).是否可以在 mypackage 中运行包含 Feature funcarg 的所有测试,但仅使用不同的实现?

Now, I am creating a separate Python package with a fancier implementation (fancypackage). Is it possible to run all of the tests in mypackage that contain the Feature funcarg, only with different implementations?

如果我在 mypackage 中添加新测试,我想避免更改 fancypackage,因此显式导入并不理想.我知道我可以使用 pytest.main() 运行所有测试,但是由于我的功能有多个实现,我不想调用 pytest.main() 多次.理想情况下,它看起来像这样:

I would like to avoid having to change fancypackage if I add new tests in mypackage, so explicit imports aren't ideal. I know that I can run all of the tests with pytest.main(), but since I have several implementations of my feature, I don't want to call pytest.main() multiple times. Ideally, it would look like something like this:

# In fancypackage/tests/test_impl1.py
def pytest_funcarg__Feature(request):
    return fancypackage.Implementation1
## XXX: Do pytest collection on mypackage.tests, but don't run them

# In fancypackage/tests/test_impl2.py
def pytest_funcarg__Feature(request):
    return fancypackage.Implementation2
## XXX: Do pytest collection on mypackage.tests, but don't run them

然后,当我在 fancypackage 中运行 pytest 时,它会收集每个 mypackage.tests 测试两次,每个功能实现一次.我尝试使用显式导入来执行此操作,它似乎工作正常,但我不想显式导入所有内容.

Then, when I run pytest in fancypackage, it would collect each of the mypackage.tests tests twice, once for each feature implementation. I have tried doing this with explicit imports, and it seems to work fine, but I don't want to explicitly import everything.

一个额外的好处是只收集那些包含 Feature funcarg 的测试.这可能吗?

An additional nice bonus would be to only collect those tests that contain the Feature funcarg. Is that possible?

在切换到 py.test 之前,我使用标准库的 unittest 做到了这一点.其功能如下:

Before switching to py.test, I did this with the standard library's unittest. The function for that is the following:

def mypackage_test_suite(Feature):
    loader = unittest.TestLoader()
    suite = unittest.TestSuite()
    mypackage_tests = loader.discover('mypackage.tests')
    for test in all_testcases(mypackage_tests):
        if hasattr(test, 'Feature'):
            test.Feature = Feature
            suite.addTest(test)
    return suite

def all_testcases(test_suite_or_case):
    try:
        suite = iter(test_suite_or_case)
    except TypeError:
        yield test_suite_or_case
    else:
        for test in suite:
            for subtest in all_testcases(test):
                yield subtest

显然现在情况有所不同,因为我们正在处理测试函数和类而不仅仅是类,但似乎 py.test 中应该有一些等价物来构建测试套件并允许您对其进行迭代.

Obviously things are different now because we're dealing with test functions and classes instead of just classes, but it seems like there should be some equivalent in py.test that builds the test suite and allows you to iterate through it.

推荐答案

您可以参数化您的 Feature 装置:

You could parameterise your Feature fixture:

@pytest.fixture(params=['ref', 'fancy'])
def Feature(request):
    if request.param == 'ref':
        return mypackage.ReferenceImplementation
    else:
        return fancypackage.Implementation1

现在,如果您运行 py.test,它将同时测试两者.

Now if you run py.test it will test both.

AFAIK 不可能在他们使用的装置上选择测试,您可能可以使用 request.applymarker()-m 将一些东西拼凑在一起.然而.

Selecting tests on the fixture they use is not possible AFAIK, you could probably cobble something together using request.applymarker() and -m. however.

这篇关于在另一个 Python 包中运行 pytest 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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