1 Unittest两种前置和两种后置办法

  • 应用Unittest框架联合selenium做webUI自动化测试的时候,常常会遇到什么时候关上和敞开浏览器,这个时候就应用到了Unittest两种前置和两种后置办法;
  • 那具体这四种办法是什么呢?看下表:
办法阐明
setup()每执行一个用例之前执行一次,比方每次运行某个用例前关上一次浏览器
teardown()每执行一个用例之后执行一次,比方每次运行某个用例后敞开一次浏览器
setupClass()每执行一个用例集之前执行一次,比方每运行一个testcase前关上一次浏览器
teardownClass()每执行一个用例集之后执行一次,比方每运行一个testcase后敞开一次浏览器
  • 而setupClass()和teardownClass() 办法用配合@classmethod办法应用。

1.1 Unittest:setup、teardown办法举例

  • 创立一个脚本test_unittest_setup_teardown.py,写入以下代码:
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2021/9/9 9:25# 文件名称:test_unittest_setup_teardown.py# 作用:验证unittest的setup、teardown办法# 分割:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport unittestclass TestOne(unittest.TestCase):    def setUp(self) -> None:        print("每运行一个case前,关上一次浏览器")    def tearDown(self) -> None:        print("每运行一个case后,敞开一次浏览器")    def test_one(self):        print("运行第一个用例")    def test_two(self):        print("运行第二个用例")if __name__ == "__main__":    unittest.main()
  • 运行后如下:
Ran 2 tests in 0.003sOKLaunching unittests with arguments python -m unittest F:/pytest_study/test_case/test_b/test_unittest_setup_teardown.py in F:\pytest_study\test_case\test_b过程已完结,退出代码 0每运行一个case前,关上一次浏览器运行第一个用例每运行一个case后,敞开一次浏览器每运行一个case前,关上一次浏览器运行第二个用例每运行一个case后,敞开一次浏览器

1.2 Unittest:setupClass、teardownClass办法举例

  • 创立一个脚本test_unittest_setupclass_teardownclass.py,写入以下代码:
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2021/9/9 9:50# 文件名称:test_unittest_setupclass_teardownclass.py# 作用:验证unittest的setupclass、teardownclass办法# 分割:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport unittestclass TestTwo(unittest.TestCase):    @classmethod    def setUpClass(cls) -> None:        print("每运行一个用例集前,关上浏览器,即这个类只关上一次浏览器")    @classmethod    def tearDownClass(cls) -> None:        print("每运行一个用例集后,敞开浏览器,即这个类只敞开一次浏览器")    def test_one(self):        print("运行第一个用例")    def test_two(self):        print("运行第二个用例")if __name__ == "__main__":    unittest.main()
  • 运行后如下:
Ran 2 tests in 0.002sOK每运行一个用例集前,关上浏览器,即这个类只关上一次浏览器运行第一个用例运行第二个用例每运行一个用例集后,敞开浏览器,即这个类只敞开一次浏览器过程已完结,退出代码 0
  • 留神这两个办法须要应用@classmethod润饰办法,如果不加的话会报错。

2 Pytest十种前置和后置办法

  • 和unittest相似,然而办法更多,达到了十种,具体看下表:
办法运行级别阐明
setup_module()模块级别整个.py模块开始前只执行一次,如关上一次浏览器
teardown_module()模块级别整个.py模块完结后只执行一次,如敞开一次浏览器
setup_function()函数级别每个函数级别用例开始前都执行,此办法不在类中
teardown_function()函数级别每个函数级别用例完结后都执行,此办法不在类中
setup_class()级别整个测试类开始前只执行一次,和Unittest根本一样
teardown_class()级别整个测试类完结后只执行一次,和Unittest根本一样
setup_method()办法级别外面每个用例执行都会执行
teardown_method()办法级别外面每个用例完结都会执行
setup()办法细化级别外面每个用例执行都会执行
teardown()办法细化级别外面每个用例完结都会执行

2.1 Pytest:setup_module、teardown_module办法举例

  • 创立test_pytest_setup_teardown_module.py,代码如下:
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2021/9/9 10:18# 文件名称:test_pytest_setup_teardown_module.py# 作用:验证pytest的setup_module和teardown_module办法# 分割:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport pytestdef setup_module():    print("整个.py模块开始前只执行一次")def teardown_module():    print("整个.py模块完结后只执行一次")def test_one():    print("用例1")def test_two():    print("用例2")class TestOne():    # @staticmethod    # def setup_module():    #     print("整个.py模块开始前只执行一次")    #    # @staticmethod    # def teardown_module():    #     print("整个.py模块完结后只执行一次")    def test_thr(self):        print("用例3")    def test_fo(self):        print("用例4")if __name__ == "__main__":    pytest.main()
  • 运行后果:
(venv) F:\pytest_study\test_case\test_c>pytest -s -q整个.py模块开始前只执行一次用例1.用例2.用例3.用例4.整个.py模块完结后只执行一次4 passed in 0.02s
  • 把这两个办法写入类中呢,那须要应用@staticmethod办法润饰,不然语法就不对,然而写入类中的话,这两个办法应该是不会运行的。

2.2 Pytest:setup_function、teardown_function办法举例

  • 创立test_pytest_setup_teardown_function.py,代码如下:
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2021/9/9 10:41# 文件名称:test_pytest_setup_teardown_function.py# 作用:验证pytest的setup_function、teardown_function办法# 分割:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport pytestdef setup_module():    print("整个.py模块开始前只执行一次")def teardown_module():    print("整个.py模块完结后只执行一次")def setup_function():    print("每个函数级别用例开始前都执行")def teardown_function():    print("每个函数级别用例完结后都执行")def test_one():    print("用例1")def test_two():    print("用例2")class TestOne():    def test_thr(self):        print("用例3")    def test_fo(self):        print("用例4")if __name__ == "__main__":    pytest.main()
  • 运行如下:
(venv) F:\pytest_study\test_case\test_c>pytest -s -q test_pytest_setup_teardown_function.py整个.py模块开始前只执行一次每个函数级别用例开始前都执行用例1.每个函数级别用例完结后都执行每个函数级别用例开始前都执行用例2.每个函数级别用例完结后都执行用例3.用例4.整个.py模块完结后只执行一次4 passed in 0.42s
  • 同样把这两个办法写入类中呢,那须要应用@staticmethod办法润饰,不然语法就不对,然而写入类中的话,这两个办法应该是不会运行的。

2.3 Pytest:setup_class、teardown_class办法举例

  • 创立test_setup_teardoen_class.py代码如下:
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2021/9/9 11:28# 文件名称:test_pytest_setup_teardoen_class.py# 作用:xxx# 分割:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport pytestdef setup_module():    print("整个.py模块开始前只执行一次")def teardown_module():    print("整个.py模块完结后只执行一次")def test_one():    print("用例1")def test_two():    print("用例2")class TestOne():    def setup_class(self):        print("整个测试类开始前只执行一次")    def teardown_class(self):        print("整个测试类完结后只执行一次")    def test_thr(self):        print("用例3")    def test_fo(self):        print("用例4")if __name__ == "__main__":    pytest.main()
  • 运行后果为:
(venv) F:\pytest_study\test_case\test_c>pytest -s -q test_setup_teardoen_class.py整个.py模块开始前只执行一次用例1.用例2.整个测试类开始前只执行一次用例3.用例4.整个测试类完结后只执行一次整个.py模块完结后只执行一次4 passed in 0.02s

2.4 Pytest:setup_method、teardown_method办法举例

  • 创立test_pytest_setup_teardown_method.py,代码如下:
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2021/9/9 12:28# 文件名称:test_pytest_setup_teardown_method.py# 作用:验证pytest的setup_method、teardown_method办法# 分割:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport pytestdef setup_module():    print("整个.py模块开始前只执行一次")def teardown_module():    print("整个.py模块完结后只执行一次")def test_one():    print("用例1")def test_two():    print("用例2")class TestOne():    def setup_class(self):        print("整个测试类开始前只执行一次")    def teardown_class(self):        print("整个测试类完结后只执行一次")    def setup_method(self):        print("1类外面每个用例执行前都会执行")    def teardown_method(self):        print("1类外面每个用例完结后都会执行")    def test_thr(self):        print("用例3")    def test_fo(self):        print("用例4")if __name__ == "__main__":    pytest.main()
  • 运行后果为:
(venv) F:\pytest_study\test_case\test_c>pytest -s -q test_pytest_setup_teardown_method.py整个.py模块开始前只执行一次用例1.用例2.整个测试类开始前只执行一次1类外面每个用例执行前都会执行用例3.1类外面每个用例完结后都会执行1类外面每个用例执行前都会执行用例4.1类外面每个用例完结后都会执行整个测试类完结后只执行一次整个.py模块完结后只执行一次4 passed in 0.14s

2.5 Pytest:setup、teardown办法举例

  • 创立test_pytest_setup_teardown.py,代码如下:
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2021/9/9 12:28# 文件名称:test_pytest_setup_teardown.py# 作用:验证pytest的setup、teardown办法# 分割:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport pytestdef setup_module():    print("setup_module:整个.py模块开始前只执行一次")def teardown_module():    print("teardown_module:整个.py模块完结后只执行一次")def setup_function():    print("setup_function:每个函数级别用例开始前都执行")def teardown_function():    print("teardown_function:每个函数级别用例完结后都执行")def test_one():    print("用例1")def test_two():    print("用例2")class TestOne():    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_thr(self):        print("用例3")    def test_fo(self):        print("用例4")if __name__ == "__main__":    pytest.main()
  • 运行后果如下:
(venv) F:\pytest_study\test_case\test_c>pytest -s -q test_pytest_setup_teardown.pysetup_module:整个.py模块开始前只执行一次setup_function:每个函数级别用例开始前都执行用例1.teardown_function:每个函数级别用例完结后都执行setup_function:每个函数级别用例开始前都执行用例2.teardown_function:每个函数级别用例完结后都执行setup_class:整个测试类开始前只执行一次setup_method:类外面每个用例执行前都会执行setup:类外面每个用例执行前都会执行用例3.teardown:类外面每个用例完结后都会执行teardown_method:类外面每个用例完结后都会执行setup_method:类外面每个用例执行前都会执行setup:类外面每个用例执行前都会执行用例4.teardown:类外面每个用例完结后都会执行teardown_method:类外面每个用例完结后都会执行teardown_class:整个测试类完结后只执行一次teardown_module:整个.py模块完结后只执行一次4 passed in 0.14s