当前位置:网站首页>pytest接口自动化测试框架 | pytest的setup和teardown函数
pytest接口自动化测试框架 | pytest的setup和teardown函数
2022-07-26 11:40:00 【COCOgsta】
视频来源:B站《冒死上传!pytest接口自动化测试框架(基础理论到项目实战及二次开发)教学视频【软件测试】》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:pytest接口自动化测试框架 | 汇总_COCOgsta的博客-CSDN博客
test_setup01.py
import pytest
def multiply(a, b):
return a * b
"""
第一批次:setup_module/teardown_module:在当前文件中,在所有测试用例执行之前与之后执行
第二批次:setup_function/teardown_function:在每个测试函数之前与之后执行
第三批次:setup/teardown:在每个测试函数之前与之后执行,这2个方法同样适用于类方法
PS: 方法的定义顺序调整了,也还是按照既定规则执行
"""
def setup_module(module):
print("setup_module===============================>")
def teardown_module(module):
print("teardown_module======================>")
def setup_function(function):
print("setup_function------------------------------>")
def teardown_function(function):
print("teardown_function------------------------------>")
def setup():
print("setup--->")
def teardown():
print("teardown--->")
# 测试用例
def test_mul_01():
print('test_3_4')
assert multiply(3,4) == 12
def test_mul_02():
print('test_6_8')
assert multiply(6,8) == 48main.py,实际执行时setup和teardown并没有打印
import pytest
if __name__ == '__main__':
pytest.main(["-s", "test_setup01.py"])test_setup02.py
import pytest
# 功能函数
def multiply(a, b):
return a * b
"""
pytest是支持使用测试类的,必须以“Test”开头,注意首字母大写
"""
class TestMultiply:
# =======fixtures=======
"""
第一批次:setup_ class / teardown_ class:
# 在当前测试类的开始与结束时执行。
第二批次:setup_ method / teardown_ method:
# 在每个测试方法开始与结束时执行。
第三批次:setup / teardown:
# 在每个测试方法开始与结束时执行,同样可以作用于测试函数
PS:执行顺序按批次顺序来,即使改变方法位置,也是一样
"""
@classmethod
def setup_class(cls):
print("setup_class==============>")
@classmethod
def teardown_class(cls):
print("teardown_class==============>")
def setup_method(self, method):
print("setup_method----->>")
def teardown_method(self, method):
print("teardown_method---->>")
def setup(self):
print("setup---->")
def teardown(self):
print("teardown---->")
# =======测试用例===========
def test_numbers_5_6(self):
print('test_numbers_5_6')
assert multiply(5,6) == 30
def test_strings_b_2(self):
print('test_strings_b_2')
assert multiply('b', 2) == "bb"main.py,实际运行时也是没有打印setup和teardown
import pytest
if __name__ == '__main__':
pytest.main(["-s", "test_setup02.py"])边栏推荐
- Didi was fined 8billion! The era of making money from user data is over
- 数据数据湖(十九):SQL API 读取Kafka数据实时写入Iceberg表
- 一些实用、常用、效率越来越高的 Kubernetes 别名
- PostgreSQL in Linux and windows installation and introductory basic tutorial
- 武林头条-建站小能手争霸赛
- Some practical, commonly used and increasingly efficient kubernetes aliases
- Database composition indexes and constraints
- 数据库组成索引和约束
- Which is faster to open a file with an absolute path than to query a database?
- 科目三 掉头与转弯
猜你喜欢

。。。。。。
![[download attached] a powerful web automated vulnerability scanning tool - Xray](/img/5e/1db72ce9bf758b1e68e8d6d2026302.png)
[download attached] a powerful web automated vulnerability scanning tool - Xray

Swagger2.9.2 tutorial and swagger3.0.0 tutorial

Ga-rpn: recommended area network for guiding anchors

Pytoch -- error based on mmseg/mmdet training: runtimeerror: expected to have finished reduction in the priority iteration

Hal library IIC simulation in punctual atom STM32 `define SDA_ IN() {GPIOB->MODER&=~(3<<(9*2));GPIOB->MODER|=0<<9*2;}` // PB9 input mode

Miccai2022 paper | evolutionary multi-objective architecture search framework: application in covid-19 3D CT classification

Application scheme of ankerui residual pressure monitoring system in residential quarter

Flink 在 讯飞 AI 营销业务的实时数据分析实践

Data type of SQL Server database
随机推荐
Wulin headlines - station building expert competition
【通信原理】第三章 -- 随机过程[上]
Didi was fined 8billion! The era of making money from user data is over
Understanding useref is enough
GA-RPN:引导锚点的建议区域网络
。。。。。。
Data type of SQL Server database
Acwing727.菱形图案
Database composition indexes and constraints
Flink 在 讯飞 AI 营销业务的实时数据分析实践
查询进阶 别名
Is it easy to find a job after programmer training?
零基础小白也能懂的 Redis 数据库,手把手教你易学易用!
Hashtable
网络协议:TCP/IP协议
Esp8266 Arduino programming example - development environment construction (based on platformio)
Practice of microservice in solving Library Download business problems
pytest接口自动化测试框架 | conftest.py
What is per title encoding?
安科瑞余压监控系统在住宅小区的应用方案