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

def setup_module():
print("setup_module:整个*.py只执行一次")
print('比如浏览器打开的动作')

def teardown_module():
print("teardown_module:整个*.py只执行一次")
print('比如浏览器关闭的动作')

def test_one():
print("正在执行------test_one")
AA = 'this'
assert 'i' in AA

def test_two():
print("正在执行------test_two")
BB = 'Str'
AA = 'Str'
assert AA == BB

def test_three():
print("正在执行------test_three")
AA = 'one'
assert 'i' != AA

if __name__ == '__main__':
pytest.main()

运行代码结果:

# setup_function及tear_function 函数级模块,代码示例如下:

import pytest
def setup_function():
print("setup_function:每个用例开始前都会执行")

def teardown_function():
print("teardown_function:每个用例结束前都会执行")

def test_one():
print("正在执行------test_one")
AA = 'this'
assert 'i' in AA

def test_two():
print("正在执行------test_two")
BB = 'Str'
AA = 'Str'
assert AA == BB

def test_three():
print("正在执行------test_three")
AA = 'one'
assert 'i' != AA

if __name__ == '__main__':
pytest.main()

# 运行结果如下:

优先级划分: setUp_class  》 setUp_module 》setUp

# 类和方法
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 'hello' == x

def test_three(self):
print("正在执行----test_three")
a = "hello"
b = "hello world"
assert a in b

if __name__ == '__main__':
pytest.main()

代码执行结果如下:

先执行函数 --> 方法 --> 再执行class类里面的类方法

#函数和类混合
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 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

if __name__ == "__main__":
pytest.main()

Pytest单元测试框架之setup/teardown模块示例操作的更多相关文章

  1. Pytest单元测试框架之简单操作示例

    前言: Pytest是第三方单元格测试框架,更加简单,灵活,而且提供了更多丰富的扩展: Pytest与UnitTest框架的区别 UnitTest测试用例执行顺序是依照ascii码执行,而Pytest ...

  2. Pytest单元测试框架-测试用例运行规则

    1.Pytest测试用例运行规则 在pytest单元测试框架下面执行用例,需要满足以下几个特点: 1. 文件名以test_*.py开头或者*_test.py 2. 测试类.测试函数以test开头 3. ...

  3. Pytest单元测试框架-Pytest环境安装

    unittest是python自带的单元测试框架,它封装好了一些校验返回的结果方法和一些用例执行前的初始化操作,使得单元测试易于开展,因为它的易用性,很多同学也拿它来做功能测试和接口测试,只需简单开发 ...

  4. Pytest单元测试框架:插件-allure-pytest环境搭建并在本地生成一个测试报告

    之前写了allure-pytest的官方文档啃的内容,有些交流的朋友,实践起来没什么头绪,所以就有了这篇文章,也给自己填个坑 第一步:搭建Allure.JDK环境 1. 搭建JDK环境 不装jdk你会 ...

  5. Pytest单元测试框架-学习

    pytest: Python的一个单元测试框架,基于UnitTest二次开发,语法上更加简洁,可以用来做Python开发项目的单元测试,UI自动化.接口自动化测试等,有很多的插件访问Pytest插件汇 ...

  6. Pytest单元测试框架之FixTure基本使用

    前言: 在单元测试框架中,主要分为:测试固件,测试用例,测试套件,测试执行及测试报告: 测试固件不难理解,也就是我们在执行测试用例前需要做的动作和测试执行后的需要做的事情: 比如在UI自动化测试中,我 ...

  7. 学习-Pytest(三)setup/teardown

    1. 用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)只对函数用例生效 ...

  8. Pytest单元测试框架——Pytest+Allure+Jenkins的应用

    一.简介 pytest+allure+jenkins进行接口测试.生成测试报告.结合jenkins进行集成. pytest是python的一种单元测试框架,与python自带的unittest测试框架 ...

  9. Pytest 单元测试框架

    1.pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效 2.安装 pytest pip install pytest 3.验证 pytest 是否安装成功 p ...

随机推荐

  1. NVIDIA GPU卷积网络的自动调谐

    NVIDIA GPU卷积网络的自动调谐 针对特定设备和工作负载的自动调整对于获得最佳性能至关重要.这是关于如何为NVIDIA GPU调整整个卷积网络. NVIDIA GPU在TVM中的操作实现是以模板 ...

  2. 基于ARM Cortex-M的SoC存储体系结构和实战

    基于ARM Cortex-M的SoC存储体系结构和实战 System on Chip Architecture Tutorial Memory Architecture for ARM Cortex- ...

  3. CUDA刷新器:CUDA编程模型

    CUDA刷新器:CUDA编程模型 CUDA Refresher: The CUDA Programming Model CUDA,CUDA刷新器,并行编程 这是CUDA更新系列的第四篇文章,它的目标是 ...

  4. 重新整理 mysql 基础篇————— mysql 事务[三]

    前言 简单整理一下事务. 正文 事务有四大特性: 1.原子性(atomicity) 一个事务必须被视为一个不可分割的最小单元. 2.一致性(consistency) 数据库总是从一个一致性的状态转换到 ...

  5. 【NX二次开发】Block UI 枚举

    属性: 常规         类型 描述     BlockID     String 控件ID     Enable     Logical 是否可操作     Group     Logical ...

  6. 【NX二次开发】Block UI 线宽

    属性说明 常规         类型 描述     BlockID     String 控件ID     Enable     Logical 是否可操作     Group     Logical ...

  7. Nexus 安装配置教程

    目录 为什么使用 Nexus Docker 模式安装 Nexus 使用 data volume 使用本地目录 Nexus 配置 配置 Blob Stores Nexus 使用 包下载 包上传 参考 为 ...

  8. 『无为则无心』Python基础 — 4、Python代码常用调试工具

    目录 1.Python的交互模式 2.IDLE工具使用说明 3.Sublime3工具的安装与配置 (1)Sublime3的安装 (2)Sublime3的配置 4.使用Sublime编写并调试Pytho ...

  9. LockSupport中的park()与unpark()

    类注释原文:Basic thread blocking primitives for creating locks and other synchronization classes.意思就是Lock ...

  10. 【模拟8.03】斐波那契(fibonacci) (规律题)

    就是找规律,发现每个父亲和孩子的差值都是距儿子最大的fibonacc 也是可证的 f[i]表示当前月的兔子总数 f[i]=f[i-1]+f[i-2](f[i-2]是新生的,f[i-1]是旧有的) 然后 ...