1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Python测试框架pytest(03)setup和teardown

Python测试框架pytest(03)setup和teardown

时间:2021-09-12 04:41:02

相关推荐

Python测试框架pytest(03)setup和teardown

Python测试框架pytest系列可以查看下列

Python测试框架pytest(01)简介、安装、快速入门_编程简单学的博客-CSDN博客

​​​​​​Python测试框架pytest(02)PyCharm设置运行pytest、pytest.main()_编程简单学的博客-CSDN博客

软件测试资料领取方式 (#1) · Issue · 可可爱爱的程序员 / 软件测试资料合集 · GIT CODE

unittest 提供了两个前置方法和两个后置方法。

分别是:

setup()setupClass()teardown()teardownClass()

pytest 也提供了类似 setup、teardown 的方法。

分别是:

模块级(开始于模块始末,全局的):setup_module()、teardown_module()函数级(只对函数用例生效,不在类中):setup_function()、teardown_function()类级(只在类中前后运行一次,在类中):setup_class()、teardown_class()方法级(开始于方法始末,在类中):setup_method()、teardown_method()方法细化级(运行在调用方法的前后):setup()、teardown()

1、创建test_setup_teardown.py文件

脚本代码:

#!/usr/bin/env python# -*- coding: utf-8 -*-"""微信公众号:AllTests软件测试"""import pytestdef setup_module():print("===== 整个.py模块开始前只执行一次 setup_module 例如:打开浏览器 =====")def teardown_module():print("===== 整个.py模块结束后只执行一次 teardown_module 例如:关闭浏览器 =====")def setup_function():print("===== 每个函数级别用例开始前都执行 setup_function =====")def teardown_function():print("===== 每个函数级别用例结束后都执行 teardown_function =====")def test_one():print("one")def test_two():print("two")class TestCase():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 setup(self):print("===== 类里面每个用例执行前都会执行 setup =====")def teardown(self):print("===== 类里面每个用例结束后都会执行 teardown =====")def test_three(self):print("three")def test_four(self):print("four")if __name__ == '__main__':pytest.main(["-q", "-s", "-ra", "test_setup_teardown.py"])复制代码

2、运行结果:

按顺序依次执行test_one函数、test_two函数,之后执行TestCase类里的test_three方法、test_four方法。

整体全部的顺序:

setup_module->setup_function->test_one->teardown_function->setup_function->test_two->teardown_function->setup_class->setup_method->setup->test_three->teardown->teardown_method->setup_method->setup->test_four->teardown->teardown_method->teardown_class->teardown_module

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。