1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > pytest测试框架系列 - setup和teardown的详细讲解

pytest测试框架系列 - setup和teardown的详细讲解

时间:2022-08-10 00:59:28

相关推荐

pytest测试框架系列 - setup和teardown的详细讲解

简介

前面使用过unittest的都知道,setup和teardown是用来处理用例的前置工作和清理工作,其中还有setupclass和teardownclass是保证执行所以的用例都只执行1次前置和后置,使用起来非常方便,对于强大的pytest测试框框,给我们提供了10中类似方法。

模块级别:setup_module、teardown_module函数级别:setup_function、teardown_function类级别:setup_class、teardown_class类方法级别:setup_method、teardown_method函数和方法级别:setup、teardown

详细用法讲解

模块级别:setup_module、teardown_module

说明:该方法表示只能类外面执行用例过程中,只执行1次。相当于unittest中的setupclass和teardownclass方法

示例如下:

# _*_coding:utf-8 _*_# @Time:/7/2 22:18# @Author : king# @File :test_demo01.py# @Software :PyCharm# @blog:/u010454117# @WeChat Official Account: 【测试开发知识库】import pytestdef setup_module():print('这是setup_module的前置')def teardown_module():print('这是setup_module的后置')def test_01():print('我是test_01用例')class TestDemo:def test_one(self):print("我是类里面方法 test_one")def test_two(self):print("我是类里面方法 test_two")if __name__ == '__main__':pytest.main()

命令行输入:pytest -s test_demo01.py执行结果如下:

函数级别:setup_function、teardown_function

说明:该方法表示在类外面执行用例过程中,每次都会执行前置和后置。

示例如下:

# _*_coding:utf-8 _*_# @Time:/7/2 22:18# @Author : king# @File :test_demo01.py# @Software :PyCharm# @blog:/u010454117# @WeChat Official Account: 【测试开发知识库】import pytestdef setup_function():print('这是 setup_function 的前置')def teardown_function():print('这是 teardown_function 的后置')def test_01():print('我是类外面的 test_01 用例')class TestDemo:def test_one(self):print("我是类里面方法 test_one")def test_two(self):print("我是类里面方法 test_two")if __name__ == '__main__':pytest.main()

命令行输入:pytest -s test_demo01.py执行结果如下:

类级别:setup_class、teardown_class

说明:该方法表示在类中执行测试用例前,只执行1次测试前置和测试后置,注意:放在类外面不生效

示例如下:

# _*_coding:utf-8 _*_# @Time:/7/2 22:18# @Author : king# @File :test_demo01.py# @Software :PyCharm# @blog:/u010454117# @WeChat Official Account: 【测试开发知识库】import pytestdef test_01():print('我是类外面的 test_01 用例')class TestDemo:def setup_class(self):print("我是 setup_class 方法")def teardown_class(self):print("我是 teardown_class 方法")def test_one(self):print("我是类里面方法 test_one")def test_two(self):print("我是类里面方法 test_two")if __name__ == '__main__':pytest.main()

命令行输入:pytest -s test_demo01.py执行结果如下:

方法级别:setup_method、teardown_method

说明:该方法表示在类中每次执行测试用例前,测试前置和测试后置都会执行一次,注意:放在类外面不生效

示例如下:

# _*_coding:utf-8 _*_# @Time:/7/2 22:18# @Author : king# @File :test_demo01.py# @Software :PyCharm# @blog:/u010454117# @WeChat Official Account: 【测试开发知识库】import pytestdef test_01():print('我是类外面的 test_01 用例')class TestDemo:def setup_method(self):print("我是 setup_method 方法")def teardown_method(self):print("我是 teardown_method 方法")def test_one(self):print("我是类里面方法 test_one")def test_two(self):print("我是类里面方法 test_two")if __name__ == '__main__':pytest.main()

命令行输入:pytest -s test_demo01.py执行结果如下:

函数和方法级别:setup、teardown

说明:该方法这个可以在类中使用,也可以在类外进行使用,大家最熟悉

示例如下:

# _*_coding:utf-8 _*_# @Time:/7/2 22:18# @Author : king# @File :test_demo01.py# @Software :PyCharm# @blog:/u010454117# @WeChat Official Account: 【测试开发知识库】import pytestdef setup():print("我是类外面 setup 方法")def teardown():print("我是类外面 teardown 方法")def test_01():print('我是类外面的 test_01 用例')class TestDemo:def setup(self):print("我是类里面 setup 方法")def teardown(self):print("我是类里面 teardown 方法")def test_one(self):print("我是类里面方法 test_one")def test_two(self):print("我是类里面方法 test_two")if __name__ == '__main__':pytest.main()

命令行输入:pytest -s test_demo01.py执行结果如下:

多个方法组合使用

说明:直接查看示例,查看各个方法执行优先级,非常重要

示例如下:

# _*_coding:utf-8 _*_# @Time:/7/2 22:18# @Author : king# @File :test_demo01.py# @Software :PyCharm# @blog:/u010454117# @WeChat Official Account: 【测试开发知识库】import pytestdef setup_module():print("我是类外面的 setup_module 方法")def teardown_module():print("我是类外面的 teardown_module 方法")def setup_function():print("我是类外面的 setup_function 方法")def teardown_function():print("我是类外面的 teardown_function 方法")def setup():print("我是类外面 setup 方法")def teardown():print("我是类外面 teardown 方法")def test_one():print("我是类外面的 test_one 用例")def test_two():print("我是类外面的 test_two 用例")class TestDemo():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_one(self):print("我是类里面的 test_one 用例")def test_two(self):print("我是类里面的 test_two 用例")if __name__ == '__main__':pytest.main()

命令行输入:pytest -s test_demo01.py执行结果如下:

我们根据执行结果看见方法执行顺序

setup_module、teardown_module 整个测试只执行一次setup_function、teardown_function 类外面方法每个用例都执行setup_class、teardown_class 类里面只执行一次setup_method、teardown_method 类里面每个用例都执行setup、teardown 类里面和外面每个用例都执行

优先级为:

setup_module>setup_function>setup>用例>teardown>teardown_function>teardown_modulesetup_module>setup_class>setup_method>setup>用例>teardown>teardown_method>teardown_class>teardown_module

以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处!

如果觉得文章不错,欢迎关注微信公众号,微信公众号每天推送相关测试技术文章

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