python如何测试仪器_如何测试pytest设备本身?

pytest有一个^{}插件,它是为了测试pytest本身和插件而设计的;它在一个独立的运行中执行不影响当前测试运行的测试。示例:# conftest.py

import pytest

pytest_plugins = ['pytester']

@pytest.fixture

def spam(request):

yield request.param

fixture spam有一个问题,它只能与参数化测试一起工作;一旦在非参数化测试中请求它,它将引发一个AttributeError。这意味着我们不能通过这样的常规测试进行测试:

^{pr2}$

相反,我们使用testdir插件提供的testdirfixture在独立的测试运行中执行测试:import pathlib

import pytest

# an example on how to load the code from the actual test suite

@pytest.fixture

def read_conftest(request):

return pathlib.Path(request.config.rootdir, 'conftest.py').read_text()

def test_spam_fixture(testdir, read_conftest):

# you can create a test suite by providing file contents in different ways, e.g.

testdir.makeconftest(read_conftest)

testdir.makepyfile(

"""

import pytest

@pytest.mark.parametrize('spam', ('eggs', 'bacon'), indirect=True)

def test_spam_parametrized(spam):

assert spam in ['eggs', 'bacon']

def test_spam_no_params(spam):

assert True

""")

result = testdir.runpytest()

# we should have two passed tests and one failed (unarametrized one)

result.assert_outcomes(passed=3, error=1)

# if we have to, we can analyze the output made by pytest

assert "AttributeError: 'SubRequest' object has no attribute 'param'" in ' '.join(result.outlines)

为测试加载测试代码的另一个方便的方法是testdir.copy_example方法。在pytest.ini中设置根路径,例如:[pytest]

pytester_example_dir = samples_for_fixture_tests

norecursedirs = samples_for_fixture_tests

现在创建包含以下内容的文件samples_for_fixture_tests/test_spam_fixture/test_x.py:import pytest

@pytest.mark.parametrize('spam', ('eggs', 'bacon'), indirect=True)

def test_spam_parametrized(spam):

assert spam in ['eggs', 'bacon']

def test_spam_no_params(spam):

assert True

(这与之前作为字符串传递给testdir.makepyfile的代码相同)。上述试验变更为:def test_spam_fixture(testdir, read_conftest):

testdir.makeconftest(read_conftest)

# pytest will now copy everything from samples_for_fixture_tests/test_spam_fixture

testdir.copy_example()

testdir.runpytest().assert_outcomes(passed=3, error=1)

这样,您就不必在测试中将Python代码维护为字符串,还可以通过使用pytester来重用现有的测试模块。也可以通过pytester_example_path标记配置测试数据根:@pytest.mark.pytester_example_path('fizz')

def test_fizz(testdir):

testdir.copy_example('buzz.txt')

将查找与项目根目录相关的文件fizz/buzz.txt。在

对于更多的例子,一定要查看pytest文档中的Testing plugins部分;而且,您可能会发现my other answer对问题How can I test if a pytest fixture raises an exception?很有帮助,因为它包含了该主题的另一个工作示例。我还发现直接研究^{} code非常有帮助,因为遗憾的是,pytest没有为它提供大量的文档,但是代码几乎是自文档化的。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值