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的更多相关文章

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

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

  2. Pytest学习笔记2-setup和teardown

    前言 我们在做自动化的时候,常常有这样的需求: 执行每一条用例时,都重新启动一次浏览器 每一条用例执行结束时,都清除测试数据 在unittest中,我们可以使用 setUp() 和 tearDown( ...

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

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

  4. pytest的setup和teardown

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

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

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

  6. Pytest权威教程16-经典xUnit风格的setup/teardown

    目录 经典xUnit风格的setup/teardown 模块级别setup/teardown 类级别setup/teardown 方法和函数级别setup/teardown 返回: Pytest权威教 ...

  7. 《带你装B,带你飞》pytest修仙之路3 - setup/teardown

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

  8. Pytest单元测试框架之setup/teardown模块示例操作

    """模块级(setup_module/teardown_module)开始于模块始末,全局的函数级(setup_function/teardown_function)只 ...

  9. spring框架学习(三)junit单元测试

    spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...

随机推荐

  1. echarts+json笔记

    echart_test.html <!DOCTYPE html> <head> <meta charset="utf-8"> <scrip ...

  2. java多线程系列1:Sychronized关键字

    1.Synchronized使用范围: 同步普通方法:锁的是当前对象 //包含synchronized修饰的同步方法的类addCountClass public class addCountClass ...

  3. sed 删除最后几行 和删除指定行 awk使用

    sed 删除最后几行 和删除指定行   转载原文链接:http://blog.51cto.com/lspgyy/1305489 sed 想删除文件中的指定行,是可以用行号指定也可以用RE来匹配的. 删 ...

  4. 乌班图docker版本18.04升级到19.03

    # 关闭docker sudo systemctl stop docker # 卸载旧版本: sudo apt-get purge docker-ce # 安装新版本 sudo apt update ...

  5. mingw下的msys显示与输入乱码

    一直很喜欢gcc+vim这个貌似已经不用在强调了,好了,我只是想说明下我的问题是首先从gcc编译出错提示开始的 正如上面所说,安装完MinGW后使用gcc一编译,这程序没有错误还好,这一有错误发现输入 ...

  6. Windows10无法打开NVIDA控制面板

    今天开机,突然发现NVIDA控制面板无法打开,在桌面右键启动nView Desktop Manager,点击启动,发现也无法启动控制面板,并且提示[您当前未使用连接到nvidia gpu的显示器].查 ...

  7. Android共享元素场景切换动画的实现

    安卓5.0系统引入了共享元素,能做出非常炫酷的场景切换效果,这让人非常兴奋同时非常蛋疼,因为低版本没法使用啊,所以今天就跟大家分享一下自己写的一个库,其实只有2个文件而已就可以兼容安卓5.0以下的版本 ...

  8. Delphi DBGridEh导出Excel

    unit Unit_DBGridEhToExcel; interface uses SysUtils, Variants, Classes, Graphics, Controls, Forms, Ex ...

  9. springboot-elasticsearch项目启动报错:'elasticsearchTemplate' that could not be found

    解决: 将elasticsearch的相关配置加入到application.yml配置文件中就可以解决

  10. 应用安全 - 工具 - 中间件 - Apache - Apache Tika - 漏洞汇总

    CVE-2016-6809 Date2016 类型远程代码执行 影响范围Apache Tika 1.6-1.13 CVE-2018-1335 Date2018 类型命令注入 影响范围Tika-serv ...