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. Cascader 级联选择器

    当一个数据集合有清晰的层级结构时,可通过级联选择器逐级查看并选择. 基础用法 有两种触发子菜单的方式 只需为 Cascader 的options属性指定选项数组即可渲染出一个级联选择器. 通过expa ...

  2. 阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造

    循环打印 工厂了的打印先注释掉 打印出来了5次对象. 打印数字i同时,让i++操作.为了看他被常见了几次实例 调用保存的方法 没个都想都有一个唯一的实例.在创建对象的时候,重新初始化了i的值.所以i每 ...

  3. RAC FAILover详解(转载)

    Oracle  RAC 同时具备HA(High Availiablity) 和LB(LoadBalance). 而其高可用性的基础就是Failover(故障转移). 它指集群中任何一个节点的故障都不会 ...

  4. W3C验证工具

    HTML验证工具:http://validator.w3.org/ CSS验证工具:http://jigsaw.w3.org/css-validator/

  5. java:Oracle(table的增删改查,data的增删改查)

    1.oracle命名规范:和Java的命名规范很像 1.严格区分大小写 2.所有的sql语句都要以';'结尾 3.所有的sql 都要使用空格区分:sqlplus空格/空格as空格sysdba回车 4. ...

  6. 采用WPF技术开发截图程序

    前言  QQ.微信截图功能已很强大了,似乎没必要在开发一个截图程序了.但是有时QQ热键就是被占用,不能快速的开启截屏:有时,天天挂着QQ,领导也不乐意.既然是程序员,就要自己开发截屏工具,功能随心所欲 ...

  7. Redis 入门 3.2.4 命令拾遗

    Redis 入门 3.2 字符串类型 3.2.4 命令拾遗 1. 增加指定的整数 INCRBY key increment   INCRBY命令与INCR命令基本一样,只不过前者可以通过increme ...

  8. CF140C New Year Snowmen(贪心+优先队列)

    CF140C 贪心+优先队列 贪心策略:每次取出数量最多的三种球,合成一个答案,再把雪球数都-1再插回去,只要还剩下三种雪球就可以不断地合成 雪球数用优先队列维护 #include <bits/ ...

  9. 详解Vue 如何监听Array的变化

    详解Vue 如何监听Array的变化:https://www.jb51.net/article/162584.htm

  10. Vue.js官方文档学习笔记(一)起步篇

    Vue.js起步 Vue.js介绍 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库 ...