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没有为它提供大量的文档,但是代码几乎是自文档化的。在

python如何测试仪器_如何测试pytest设备本身?相关推荐

  1. python交互式程序设计导论答案第五周_学堂在线_计算机科学与Python编程导论_章节测试答案...

    学堂在线_计算机科学与Python编程导论_章节测试答案 更多相关问题 素描的三种表现形式是:(). 运行下列程序:Private Sub form_Click()For i = 1 To 2x = ...

  2. python如何测试仪器_使用python检测一个设备是否ping的通

    标签: 使用python检测一个设备是否ping的通 一,subprocess以及常用的封装函数 运行python的时候,我们都是在创建并运行一个进程.像Linux进程那样,一个进程可以fork一个子 ...

  3. python 标准输入设备_实时获取MIDI设备的输入(Python)

    I've got a trigger finger (MIDI tablet) and I want to be able to read its input live and make python ...

  4. 测试python第二周_姓名测试打分

    卜易居名字测试打分,为最早开发的姓名测试程序,网络上其它姓名打分网站大都仿照本站,下次访问搜索卜易居即可. 姓名测试打分简介 卜易居姓名测试,按照姓名学五格数理,并结合周易五行相生相克的理念,通过分析 ...

  5. python开源考试_可能是 Python 中最火的第三方开源测试框架 pytest

    作者:HelloGitHub-Prodesire 一.介绍 本篇文章是<聊聊 Python 的单元测试框架>的第三篇,前两篇分别介绍了标准库 unittest 和第三方单元测试框架 nos ...

  6. python编程控制示波器设备_测试普源设备控制软件 RglComm

    测试普源设备控制软件 RglComm [复制链接] 本帖最后由 dcexpert 于 2019-8-18 17:08 编辑 前几天给大家介绍了开源的USB普源设备控制软件 RglComm,正好有普源的 ...

  7. 用python玩转数据测试与作业_大学mooc用Python玩转数据章节测试答案

    大学mooc用Python玩转数据章节测试答案 更多相关问题 某班有学生40人,女生人数与总人数右比是3:8,则男生有()人.A.15B.25C.30 某班有学生40人,女生人数与总人数右比是3:8, ...

  8. python语言是开源的现拥有十几万个第三方函数库_智慧树知到《Python程序设计基础》章节测试答案...

    智慧树知到<Python程序设计基础>章节测试答案 绪论 1.学好Python程序设计要注意的几个问题是 A:多看多练 B:多想多整理 C:多交流多请教 D:只看视频,不思考.不练习 答案 ...

  9. 用python玩转数据慕课答案第四周_大学慕课用Python玩转数据章节测试答案

    大学慕课用Python玩转数据章节测试答案 更多相关问题 渗透泵型片剂控释的基本原理是A.减小溶出B.减慢扩散C.片剂膜外渗透压大于片剂膜内,将片内药物从 语义学批评是什么? As usual, __ ...

最新文章

  1. python selenium error “Geckodriver executable needs to be in PATH”
  2. BCH推出BCH粉丝互动平台
  3. nginx重定向到其他url方法_高级开发必须掌握Nginx之四,if、set、return
  4. Android动态权限申请工具类非常好用包含9组危险权限
  5. 延边大学c语言题库,延边大学-SPOC官方网站
  6. 机器学习导论 与数学分析
  7. 阿里宜搭助力服务中枢升级,提升10倍开发效率
  8. Ubuntu 20.04 LTS(Focal Fossa)发布
  9. 最长回文子串--动态规划
  10. Kotlin 势必取代 Java?
  11. 计算机三级信息安全笔记(知识点)
  12. 什么是 CDN 边缘服务器 - Edge Server
  13. 华为云牛客网之学生华为云服务器体验之路_1云服务器的购买篇
  14. 肖 sir_就业课__014mysql讲解
  15. 软工大牛Collin McMillan及其顶会论文解读
  16. 串口调试助手 Alien V0.238
  17. 同步以太网-SyncE介绍
  18. 计算机网络节点分为三类,计算机网络技术1—2——3章
  19. linux 光功率 模块_光模块及调整光模块输入光功率的方法_2
  20. 我对高考作弊的一些看法

热门文章

  1. echarts 默认显示图例_echarts图例组件点击显示功能(默认功能点击消失)
  2. mysql错误修改数据_使用正则表达式快速修改mysql中错误的varchar类型数据
  3. 十大经典排序算法之插入排序及其二分优化
  4. LeetCode 1114 按序打印
  5. Linux——查看硬件配置命令
  6. android小闹钟程序,Android实现闹钟小程序.pdf
  7. java程序设计实例教程 刘志成_Java程序设计实例教程教学课件作者刘志成章节05_Java图形用户界面技术.PPT...
  8. ashx获取客户端excel文件_Spring Boot实现导出Excel功能
  9. 面试再也不怕问到HashMap(一)
  10. HTML/CSS学习笔记02【表单标签】