学习-Pytest(三)setup/teardown
1. 用例运行级别
模块级(setup_module/teardown_module)开始于模块始末,全局的
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
方法级(setup_method/teardown_method)开始于方法始末(在类中)
类里面的(setup/teardown)运行在调用方法的前后
2. 函数式
2.1 setup_function/teardown_function (每个用例开始和结束时调用一次)
# test_fixt.py # coding:utf-8
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_two")
x = "hello"
assert hasattr(x, 'check') def test_three():
print("正在执行----test_three")
a = "hello"
b = "hello world"
assert a in b if __name__ == "__main__":
pytest.main(["-s", "test_fixt.py"])
运行结果
2.2 setup_module/teardown_module(所有用例开始和结束时调用一次)
# test_fixt.py # coding:utf-8
import pytest
# 函数式 def setup_module():
print("setup_module:整个.py模块只执行一次")
print("比如:所有用例开始前只打开一次浏览器") def teardown_module():
print("teardown_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_two")
x = "hello"
assert hasattr(x, 'check') def test_three():
print("正在执行----test_three")
a = "hello"
b = "hello world"
assert a in b if __name__ == "__main__":
pytest.main(["-s", "test_fixt.py"])
运行结果
2.3 类和方法
1.setup/teardown和unittest里面的setup/teardown是一样的功能,setup_class和teardown_class等价于unittest里面的setupClass和teardownClass
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("teardown_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_two")
x = "hello"
assert hasattr(x, 'check') def test_three(self):
print("正在执行----test_three")
a = "hello"
b = "hello world"
assert a in b if __name__ == "__main__":
pytest.main(["-s", "test_fixtclass.py"])
运行结果
备注:这里setup_method和teardown_method的功能和setup/teardown功能是一样的,一般二者用其中一个即可
2.4 函数和类混合
1.如果一个.py的文件里面既有函数用例又有类和方法用例,运行顺序又是怎样的呢?
# coding:utf-8
import pytest
# 类和方法
def setup_module():
print("setup_module:整个.py模块只执行一次")
print("比如:所有用例开始前只打开一次浏览器") def teardown_module():
print("teardown_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_two")
x = "hello"
assert hasattr(x, 'check') class TestCase(): def setup_class(self):
print("setup_class:所有用例执行之前") def teardown_class(self):
print("teardown_class:所有用例执行之前") def test_three(self):
print("正在执行----test_three")
x = "this"
assert 'h' in x def test_four(self):
print("正在执行----test_four")
x = "hello"
assert hasattr(x, 'check') if __name__ == "__main__":
pytest.main(["-s", "test_fixtclass.py"])
运行结果
2.从运行结果看出,setup_module/teardown_module的优先级是最大的,然后函数里面用到的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉
学习-Pytest(三)setup/teardown的更多相关文章
- Pytest测试框架(二):pytest 的setup/teardown方法
PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性.pytest的setup/teardown方法包括:模 ...
- Pytest学习笔记2-setup和teardown
前言 我们在做自动化的时候,常常有这样的需求: 执行每一条用例时,都重新启动一次浏览器 每一条用例执行结束时,都清除测试数据 在unittest中,我们可以使用 setUp() 和 tearDown( ...
- Pytest学习(三) - setup和teardown的使用
一.前言 从文章标题可以看出,就是初始化和释放的操作,根据我的java习惯来学习pytest,个人感觉没差太多,理解上也不是很难. 哦,对了,差点跑题了,这个框架是基于Python语言的,在学习的时候 ...
- pytest的setup和teardown
学过unittest的setup和teardown,前置和后置执行功能.pytest也有此功能并且功能更强大,今天就来学习一下吧. 用例运行级别: 模块级(setup_module/teardown_ ...
- pytest进阶使用【fixture(一)fixture与setup/teardown区别】
fixture翻译为装置. 我觉得名字是很贴合功能的,可以自由给函数装置上自己想要的功能. 当在说pytest比unitest灵活时,fixture肯定是其中的一个理由. 测试数据的准备和执行以后的数 ...
- Pytest权威教程16-经典xUnit风格的setup/teardown
目录 经典xUnit风格的setup/teardown 模块级别setup/teardown 类级别setup/teardown 方法和函数级别setup/teardown 返回: Pytest权威教 ...
- 《带你装B,带你飞》pytest修仙之路3 - setup/teardown
1. 简介 学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次.当然还有更高级一点的setupClass和teardownClass ...
- Pytest单元测试框架之setup/teardown模块示例操作
"""模块级(setup_module/teardown_module)开始于模块始末,全局的函数级(setup_function/teardown_function)只 ...
- spring框架学习(三)junit单元测试
spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...
随机推荐
- Php mysql 常用代码、CURD操作以及简单查询
C/S:Client ServerB/S:Brower Server php主要实现B/S LAMP :Linux系统 A阿帕奇服务器 Mysql数据库 Php语言 mysql常用代码 ...
- leetcode-easy-string-14 Longest Common Prefix
mycode 91.59% class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if not str ...
- leetcode 121买卖股票的最佳时机I
从下标1开始,维护两个变量,一个是0~i-1中的最低价格low,一个是当前的最高利润res;先更新最高利润,在更新最低价格:应用了贪心算法的基本思想,总是选择买入价格最低的股票,代码如下: 具有最优子 ...
- if-else判断语句
<1>if-else的使用格式 if 条件: 满足条件时要做的事情1 满足条件时要做的事情2 满足条件时要做的事情3 ...(省略)... else: 不满足条件时要做的事情1 不满足条件 ...
- Storm之WordCount初探
刚接触Strom,记录下执行过程 1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <proj ...
- PRISM 4 - RegisterViewWithRegion & Custom Export Attributes
5down votefavorite I am using Prism 4 with MEF Extensions and the MVVM pattern. During initializat ...
- Selenium 2自动化测试实战14(定位一组元素)
一.定位一组元素 WebDriver还提供了与前面所对应的8钟用于定位一组元素的方法.定位一组元素的方法与定位单个元素的方法类似,唯一的区别是在单词element后面多了一个S表示复数.定位一组元素一 ...
- 阶段3 2.Spring_03.Spring的 IOC 和 DI_7 spring中bean的细节之作用范围
bean的作用范围调整. 我们的bean通常情况下都是一个单例的模式 Spring是否也知道这些都是单例 构造函数只走了一次.也就是spring这个对象默认情况就是单例的 scope属性 定义bean ...
- java之 Mybatis框架
1.三层框架: 表现层: 是用于展示数据 业务层: 是处理业务需求 持久层: 是和数据库交互 注:MyBatis在持久层 2.JDBC操作数据库 public static void main(Str ...
- 什么是token?你是怎么理解token?
1.Token的引入: Token是在客户端频繁向服务端请求数据,服务端频繁的去数据库查询用户名和密码并进行对比,判断用户名和密码正确与否,并作出相应提示,在这样的背景下,Token便应运而生. 2. ...