第三:Pytest-setup/teardown的类型种类
一.简介1.unittest里面用前置和后置setup和teardown非常好用在每次用例开始前和结束后都去执行一次2.当然还有更高级的setupClass和teardownClass需配合classmethod装饰器一起使用2.1.在做selenium自动化的时候它的效率尤为突出可以只启动一次浏览器执行多个用例3.Pytest框架也有类似于setup和teardown的语法并且还不止这四个二.用例运行级别1.模块级setup_module/teardown_module开始于模块始末全局的2.函数级setup_function/teardown_function只对函数用例生效不在类中3.类级setup_class/teardown_class只在类中前后运行一次(在类中)4.方法级setup_method/teardown_method开始于方法始末在类中5.类里面的setup/teardown运行在调用方法的前后三.函数式(没有类)1.模块级:setup_module/teardown_module所有用例开始和结束时调用一次1.1.test_bjhg_class1.pyimportpytest# 函数式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)xthisasserthinx def test_two(): print(正在执行----test_two)xhelloassert hasattr(x,check)def test_three(): print(正在执行----test_three)ahellobhello worldassert ainbif__name____main__:pytest.main([-s,test_bjhg_class1.py])1.2.运行结果2.函数级setup_function/teardown_function 每个用例开始和结束时调用一次2.1.test_bjhg_class1.pyimportpytest# 函数式def setup_function(): print(setup_function每个用例开始前都会执行)def teardown_function(): print(teardown_function每个用例结束后都会执行)def test_one(): print(正在执行----test_one)xthisasserthinx def test_two(): print(正在执行----test_two)xhelloassert hasattr(x,check)def test_three(): print(正在执行----test_three)ahellobhello worldassert ainbif__name____main__:pytest.main([-s,test_bjhg_class1.py])2.2.运行结果四.类和方法(有类)1.类级setup_class和teardown_class等价于Unittest里面的setupClass和teardownClass2.方法级setup_method/teardown_method3.类里面的setup/teardown和Unittest里面的setup/teardown是一样的功能4.test_bjhg_class1.pyimportpytest# 类和方法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)xthisasserthinx def test_two(self): print(正在执行----test_two)xhelloassert hasattr(x,check)def test_three(self): print(正在执行----test_three)ahellobhello worldassert ainbif__name____main__:pytest.main([-s,test_bjhg_class1.py])4.1.运行结果这里setup_method/teardown_method和setup/teardown功能是一样一般二者用其中一个即可4.1.1.建议使用setup_method/teardown_method五.函数和类混合(上面两种结合一起)1.如果一个.py的文件里面既有函数用例又有类和方法用例运行顺序又是怎样的呢2.test_bjhg_class1.pyimportpytest# 类和方法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)xthisasserthinx def test_two(): print(正在执行----test_two)xhelloassert 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)xthisasserthinx def test_four(self): print(正在执行----test_four)xhelloassert hasattr(x,check)if__name____main__:pytest.main([-s,test_bjhg_class1.py])3.其中setup_module/teardown_module的优先级是最大的4.然后函数里面用到的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉