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. what we regret most 国外的调查结果: 一生中最后悔的事情

    http://v.163.com/movie/2013/4/U/9/M93FDHRBE_M93FFFNU9.html 来自为知笔记(Wiz)

  2. Dark 类与对象

    1 类的声明 使用class声明一个类 使用new 创建一个对象,new可省略 dark文件名和声明的类名可以不一样,与java不同 void main() { var function = Pers ...

  3. Vs2019+openjdk12 本地Debug环境搭建过程

    1. VS2019下载和安装 这个就不写了 2. cygwin安装: https://jingyan.baidu.com/article/455a99507c0b78a166277809.html 需 ...

  4. 方差分析(python代码实现)

    python机器学习-乳腺癌细胞挖掘(欢迎关注博主主页,学习python视频资源,还有大量免费python经典文章) https://study.163.com/course/introduction ...

  5. Ubuntu系统下各种报错杂烩(持续更新)

    在Github时报Permanently added the RSA host key for IP address '192.30.252.128' to the list of known hos ...

  6. Selenium-WebDriverApi接口

    webdriver获取元素的18种方式: 1.单数模式8 种 id定位模式 element = driver.find_element_by_id('i1') element.send_keys('U ...

  7. 解决Prism中Region的GetView不起作用问题

    通常情况下在Region中添加View时我们需要先判断View是否在Region中已存在,但如果我们在Region.Add的方法调用不当时,我们在GetView中始终返回Null,原因自然是Add时出 ...

  8. Cinder LVM backend cinder-volume service down

    目录 文章目录 目录 问题 调查 解决 问题 [stack@manager ~]$ cinder service-list +------------------+------------------ ...

  9. Elastic Stack学习

    原文链接 Elastic Stack简称ELK,在本教程你将学习如何快速搭建并运行Elastic Stack. 首先你要安装核心开源产品: Elasticsearch: Kibana: Beats: ...

  10. 使用xUnits来实现单元测试

    目录 前言 单元测试 xUnit 小结 附录 前言 从开始敲代码到现在,不停地都是在喊着记得做测试,记得自测,测试人员打回来扣你money之类的,刚开始因为心疼钱(当然还是为了代码质量),就老老实实自 ...