前言

今天我们再说一下pytest框架和unittest框架相同的fixture的使用, 了解unittest的同学应该知道我们在初始化环境和销毁工作时,unittest使用的是setUp,tearDown方法,那么在pytest框架中同样存在类似的方法,今天我们就来具体说明。

先附上官方文档的一段说明

1.每个级别的setup/teardown都可以多次复用

2.如果相应的初始化函数执行失败或者被跳过则不会执行teardown方法

3.在pytest4.2之前,xunit fixture 不遵循fixture的作用规则的,因此可以在一个session级别且参数auto=True的fixture前执行setup_method方法

但是到目前为止,所有的xunit fixture已经遵循了fixture执行的规则

function级别

实例

 def setup_function(function):
print('\n--------------------')
print('函数执行前所做的操作')
print('\n--------------------') def teardown_function(function):
print('\n--------------------')
print('函数执行后所做的操作')
print('\n--------------------') def test_function_1():
print('\n测试函数1') def test_function_2():
print('\n测试函数2') if __name__ == '__main__':
import pytest
pytest.main(['-sq', 'functionLevel.py'])

输出结果

functionLevel.py
--------------------
函数执行前所做的操作
--------------------
测试函数1
--------------------
函数执行后所做的操作
--------------------
--------------------
函数执行前所做的操作
--------------------
测试函数2
--------------------
函数执行后所做的操作
--------------------
[100%] ========================== 2 passed in 0.03 seconds ===========================

说明

通过输出结果我们可以总结:setup_function会在每一个测试函数前执行初始化操作;teardown_function会在每一个测试函数执行后执行销毁工作

method级别

实例

 class TestMethod(object):

     def setup_method(self, method):
print('\n--------------------')
print('方法执行前所做的操作')
print('\n--------------------') def teardown_method(self, method):
print('\n--------------------')
print('方法执行后所做的操作')
print('\n--------------------') def test_method_1(self):
print('\n测试方法1') def test_method_2(self):
print('\n测试方法2')
if __name__ == '__main__':
import pytest
pytest.main(['-sq', 'methodLevel.py'])

输出结果

methodLevel.py
--------------------
方法执行前所做的操作
--------------------
测试方法1
--------------------
方法执行后所做的操作
--------------------
--------------------
方法执行前所做的操作
--------------------
测试方法2
--------------------
方法执行后所做的操作
--------------------
[100%]
========================== 2 passed in 0.03 seconds ===========================

说明

通过输出结果我们可以总结:setup_method会在每一个测试方法前执行初始化操作;teardown_method会在每一个测试方法执行后执行销毁工作,且方法级别的fixture是作用在测试类中的方法上的

class级别

实例

 class TestClass(object):

     @classmethod
def setup_class(cls):
print('\nsetup_class() for {}'.format(cls.__name__)) @classmethod
def teardown_class(cls):
print('\nteardown_class() for {}'.format(cls.__name__)) def test_1(self):
print('self.test_1()') def test_2(self):
print('self.test_2()') if __name__ == '__main__':
import pytest
pytest.main(['-sq', 'classLevel.py'])

输出结果

classLevel.py
setup_class() for TestClass
.self.test_1()
.self.test_2()
teardown_class() for TestClass
[100%] ========================== 2 passed in 0.06 seconds ===========================

说明

通过输出结果我们可以总结:setup_class会在测试类执行前执行一次初始化操作;teardown_class会在测试类执行后执行一次销毁工作,且class级别的fixture需要使用@classmethod装饰

module级别

实例

 def setup_module(module):
print('\nsetup_module() for {}'.format(module.__name__)) def teardown_module(module):
print('\nteardown_module() for {}'.format(module.__name__)) def test_1():
print('test_1()') def test_2():
print('test_2()') class TestClass(object): def test_3(self):
print('self.test_3()') def test_4(self):
print('self.test_4()') if __name__ == '__main__':
import pytest
pytest.main(['-sq', 'moduleLevel.py'])

输出结果

moduleLevel.py
setup_module() for moduleLevel
.test_1()
.test_2()
.self.test_3()
.self.test_4()
teardown_module() for moduleLevel
[100%] ========================== 4 passed in 0.04 seconds ===========================

说明

总结

setup_module会在整个测试文件也就是模块中的测试类或者测试函数,测试方法执行前执行一次初始化操作;teardown_module会在整个测试文件也就是模块中的测试类或者测试函数,方法执行后执行一次销毁工作

以上就是xunit fixture的4个级别,实际工作中该如何使用还需多练习,深入理解才能得心应手!

附上官方文档做参考虽是英文但是很详细 https://buildmedia.readthedocs.org/media/pdf/pytest/latest/pytest.pdf

最后呢,我计划后期结尾时会出个pytest框架编写的实战项目,关注我不迷路,敬请期待!

pytest进阶之xunit fixture的更多相关文章

  1. pytest进阶使用【fixture(一)fixture与setup/teardown区别】

    fixture翻译为装置. 我觉得名字是很贴合功能的,可以自由给函数装置上自己想要的功能. 当在说pytest比unitest灵活时,fixture肯定是其中的一个理由. 测试数据的准备和执行以后的数 ...

  2. pytest进阶之fixture

    前言 学pytest就不得不说fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一样,如果不学fixture那么使用pytest和使用unit ...

  3. pytest进阶之fixture函数

    fixture函数存在意义 与python自带的unitest测试框架中的setup.teardown类似,pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.但是相 ...

  4. Pytest - 进阶功能fixture

    1. 概述 Pytest的fixture功能灵活好用,支持参数设置,便于进行多用例测试,简单便捷,颇有pythonic.如果要深入学习pytest,必学fixture. fixture函数的作用: 完 ...

  5. Pytest(3)fixture的使用

    fixture的优势 Pytest的fixture相对于传统的xUnit的setup/teardown函数做了显著的改进: 命名方式灵活,不局限于 setup 和teardown 这几个命名 conf ...

  6. pytest进阶

    参考文章 使用 pytest pytest 这个 库是一个第三方库,严格来说,它的设计思路不属于 xUnit 系列.但它使用起来比较方便,同时他又兼容 unittest 的用例:用 unittest ...

  7. pytest进阶之html测试报告

    前言 Pytest系列已经写了几篇文章了,也不知道对多少人有帮助,总之对于我自己来说该掌握的都已经掌握了,那么今天我们再来说说pytest如何生成一个完整的html测试报告,让你在吹牛逼的路上再多一份 ...

  8. pytest进阶之conftest.py

    前言 前面几篇随笔基本上已经了解了pytest 命令使用,收集用例,finxture使用及作用范围,今天简单介绍一下conftest.py文件的作用和实际项目中如是使用此文件! 实例场景 首先们思考这 ...

  9. pytest自动化3:fixture之conftest.py实现setup

    出处:https://www.cnblogs.com/yoyoketang/p/9390073.html 前言: 前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作 ...

随机推荐

  1. navicat for mysql 破解方法

    https://www.cnblogs.com/da19951208/p/6403607.html  破解教程

  2. remove方法

    1.jQuery的remove()方法 http://www.365mini.com/page/jquery-remove.htm ①返回值是jquery对象本身 所以可以做删除再添加的操作 // 移 ...

  3. scrapy顺序执行多个爬虫

    # -*- coding:utf-8 -*- from scrapy import cmdline from scrapy.cmdline import execute import sys,time ...

  4. Python数据运算

    身份运算 is is是判断两个标识符是不是引用自一个对象 x is y, 如果id(x)等于id(y), is 返回结果1 is not is not 是判断两个标识符是不是引用自不同对象 x is ...

  5. selenium相关技术研究(从1.0-3.0)

    注: 以下内容引自http://www.cnblogs.com/hhudaqiang/p/6550135.html Selenium相关技术研究(从1.0-3.0) 好吧,最近看wxpython有点多 ...

  6. Ordering犀利的比较器

    Ordering是Guava类库提供的一个犀利强大的比较器工具,Guava的Ordering和JDK Comparator相比功能更强.它非常容易扩展,可以轻松构造复杂的comparator,然后用在 ...

  7. 向combobox控件中添加元素

    函数定义: bool FillComboBox(CComboBox* pc, CStringList& slValues, bool bOnlyUniqueValues = false); 函 ...

  8. 【最小生成树】UVA1494Qin Shi Huang's National Road System秦始皇修路

    Description During the Warring States Period of ancient China(476 BC to 221 BC), there were seven ki ...

  9. i春秋----Misc

    好久没有写 博客今天更新多了一些 解题思路:题目做多了,自然能够想到是凯撒解密: 查看得到答案; flag{4c850c5b3b2756e67a91bad8e046dda} 2: 解题思路:是我想太多 ...

  10. jackson json转对象 json转集合 对大小写支持

    @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibi ...