用例运行级别
  模块级(setup_module/teardown_module)开始于模块始末,全局的
  函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
  类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
  方法级(setup_method/teardown_method)开始于方法始末(在类中)
  类里面的(setup/teardown)运行在调用方法的前后

函数级:function与module

setup_function/teardown_function 每个用例开始和结束调用一次

从结果可以看出用例执行顺序:
setup_function》用例 1》teardown_function,setup_function》用例2》teardown_function》setup_function》用例 3》teardown_function

import pytest

def setup_function():
print("setup_function:每个用例开始前都会执行") def teardown_function():
print("teardown_function:每个用例结束后都会执行") def test_one():
print("正在执行--test_one")
x = 'this'
assert 'h' in x def test_two():
print("正在执行--test_one")
x = 'hello'
assert hasattr(x, 'check') def test_three():
print("正在执行--test_one")
a = 'hello'
b = 'hello world'
assert a in b if __name__=='__main__':
pytest.main()

setup_module/teardown_module

setup_module 所有用例开始前执行一次,teardown_module 所有用例结束后执行一次

从运行结果可以看到 setup_module 和 teardown_module 整个.py 模块只执行了一次

import pytest

def setup_module():
print('setup_module:整个.py模块只执行一次')
print('比如:所有用例开始前只打开一次浏览器') def teardown_module():
print('setup_module:整个.py模块只执行一次')
print('比如:所有用例结束后关闭浏览器') def setup_function():
print("setup_function:每个用例开始前都会执行") def teardown_function():
print("teardown_function:每个用例结束后都会执行") def test_one():
print("正在执行--test_one")
x = 'this'
assert 'h' in x def test_two():
print("正在执行--test_one")
x = 'hello'
assert hasattr(x, 'check') def test_three():
print("正在执行--test_one")
a = 'hello'
b = 'hello world'
assert a in b if __name__=='__main__':
pytest.main()

类和方法级:

setup_class/teardown_class:
setup_class 和 teardown_class 等价于 unittest 里面的setupClass 和 teardownClass

从结果看出,运行的优先级:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class

import pytest

class TestCase():

    def setup(self):
print("setup:每个用例开始前都会执行") def teardown(self):
print("teardown:每个用例结束后都会执行") def setup_class(self):
print('setup_class:所有用例执行之前') def teardown_class(self):
print('setup_class:所有用例执行之后') def setup_method(self):
print('setup_method:每个用例开始前执行') def teardown_method(self):
print('teardown_method:每个用例结束后执行') def test_one(self):
print("正在执行--test_one")
x = 'this'
assert 'h' in x def test_two(self):
print("正在执行--test_one")
x = 'hello'
assert hasattr(x, 'check') def test_three(self):
print("正在执行--test_one")
a = 'hello'
b = 'hello world'
assert a in b if __name__=='__main__':
pytest.main()

函数和类的混合:

一个.py 的文件里面既有函数用例又有类和方法用例

从运行结果看出,setup_module/teardown_module 的优先级是最大的,然后函数里面用到的 setup_function/teardown_function与类里面的 setup_class/teardown_class 互不干涉

import pytest

def setup_module():
print('setup_module:整个.py模块只执行一次')
print('比如:所有用例开始前只打开一次浏览器') def teardown_module():
print('setup_module:整个.py模块只执行一次')
print('比如:所有用例结束后关闭浏览器') def setup_function():
print("setup_function:每个用例开始前都会执行") def teardown_function():
print("teardown_function:每个用例结束后都会执行") def test_one():
print("正在执行--test_one")
x = 'this'
assert 'h' in x def test_two():
print("正在执行--test_one")
x = 'hello'
assert hasattr(x, 'check') class TestCase(): def setup_class(self):
print('setup_class:所有用例执行之前') def teardown_class(self):
print('setup_class:所有用例执行之后') def test_one(self):
print("正在执行--test_one")
x = 'this'
assert 'h' in x def test_two(self):
print("正在执行--test_one")
x = 'hello'
assert hasattr(x, 'check') def test_three(self):
print("正在执行--test_one")
a = 'hello'
b = 'hello world'
assert a in b if __name__=='__main__':
pytest.main()

pytest二:setup和teardown的更多相关文章

  1. pytest的setup和teardown

    学过unittest的setup和teardown,前置和后置执行功能.pytest也有此功能并且功能更强大,今天就来学习一下吧. 用例运行级别: 模块级(setup_module/teardown_ ...

  2. python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)

    pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...

  3. Pytest测试框架(二):pytest 的setup/teardown方法

    PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性.pytest的setup/teardown方法包括:模 ...

  4. 【pytest】(十二)参数化测试用例中的setup和teardown要怎么写?

    还是一篇关于pytest的fixture在实际使用场景的分享. fixture我用来最多的就是写setup跟teardown了,那么现在有一个用例是测试一个列表接口,参数化了不同的状态值传参,来进行测 ...

  5. pytest 2.测试用例setup和teardown

    之前我写的unittest的setup和teardown,还有setupClass和teardownClass(需要配合@classmethod装饰器一起使用),接下来就介绍pytest的类似于这类的 ...

  6. python:pytest中的setup和teardown

    原文:https://www.cnblogs.com/peiminer/p/9376352.html 之前我写的unittest的setup和teardown,还有setupClass和teardow ...

  7. Pytest学习(三) - setup和teardown的使用

    一.前言 从文章标题可以看出,就是初始化和释放的操作,根据我的java习惯来学习pytest,个人感觉没差太多,理解上也不是很难. 哦,对了,差点跑题了,这个框架是基于Python语言的,在学习的时候 ...

  8. pytest自动化2:测试用例setup和teardown

    前言: pytest支持函数和类两种用例方式,针对每种情况都有不同的代码 pytest用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(s ...

  9. pytest文档4-测试用例setup和teardown

    前言 学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次. 当然还有更高级一点的setupClass和teardownClass,需 ...

  10. Pytest系列(3) - setup和teardown的详细使用

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 用过unittest的童鞋都 ...

随机推荐

  1. JavaSE学习总结(五)——封装,继承,多态很简单

    java面向对象的三大特性是:封装.继承与多态,是面向对象编程的核心. 一.封装 简单说封装就是将同一类事物的特性与功能包装在一起,对外暴露调用的接口. 封装:封装也称信息隐藏,是指利用抽象数据类型把 ...

  2. 在windows环境下实现开机延迟启动tomcat

    如果说我们的服务器断电了 开机之后还需要手动开下服务  还需要远程连接上  然后一个一个开启  是不是很麻烦  我们可以写一个bat脚本  然后设置开机5分钟之后启动tomcat 首先配置环境变量: ...

  3. centOS7环境下安装jdk1.8

    首先下载jdk1.8  去官网下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151. ...

  4. dubbo面试问题

    为什么用微服务: 为什么zookeeper能作为注册中心: 使用分布式碰到的bug, zookeeper有集群吗?怎么实现的: zookeeper宕机还能访问吗: 服务失效踢出zookeeper中临时 ...

  5. xshell访问Ubuntu16.04显示乱码(即使在xshell设置了utf8)解决方案

    一开始问题是:(无法显示,也无法输入) 然后使用: locale -a 查看服务器安装的全部编码: (且服务器使用的 LANG=C) 只要用: 即可显示中文.也可以输入中文.

  6. C#窗口防止闪烁两种方法

    public class PaintIncrease { public static void SetDoubleBuffered(object obj) { Type type = obj.GetT ...

  7. MAC洪水攻击

    MAC洪水攻击原理 传统的交换机在数据转发过程中依靠对CAM表的查询来确定正确的转发接口,一旦在查询过程中无法找到相关的目的MAC对应的条目,此数据帧将作为广播帧来处理,CAM表的容量有限,只能存储不 ...

  8. 复杂HTML解析

    面对页面解析难题时候,需要注意问题: 1.寻找“打印次页”的链接,或者看看网站有没有HTML样式更友好的移动版(把自己的请求头设置成处于移动设备的状态,然后接收网站移动版). 2.寻找隐藏在JavaS ...

  9. luogu 1052 过河

    神仙的博客,先copy了日后绝对删掉的,(因为我实在没耐心看懂啊..) 题解 step 1理解题意 在做这道题之前,一定要理解好题意,有一个需要特别注意注意的地方: 青蛙不是一定要跳到石头上[嗯... ...

  10. 第14月第17天 automaticallyAdjustsScrollViewInsets contentInsetAdjustmentBehavior

    1. automaticallyAdjustsScrollViewInsets self.edgesForExtendedLayout = UIRectEdgeNone; if ([self resp ...