前言
大家之前使用过unitest框架的都知道执行用例前后会执行 setup,teardown方法,pytest框架为我们提供了更灵活的方法。
下面我们介绍一下这些方法的使用:
-
模块级(setup_module/teardown_module)在模块始末调用
-
函数级(setup_function/teardown_function)在函数始末调用(在类外部)
-
类级(setup_class/teardown_class)在类始末调用(在类中)
-
方法级(setup_method/teardown_methond)在方法始末调用(在类中)
-
方法级(setup/teardown)在方法始末调用(在类中)
模块级(setup_module/teardown_module)
1 # Author xuejie zeng 2 # encoding utf-8 3 # test.py
4 def setup_module(): 5 print("setup_module,整个test.py模块只执行一次") 6 7 def teardown_module(): 8 print("teardown_module,整个test.py模块只执行一次") 9 10 11 class TestPy01(): 12 13 def test_one(self): 14 print("测试用例test_one") 15 16 def test_two(self): 17 print("测试用例test_two") 18 19 20 class TestPy02(): 21 22 def test_a(self): 23 print("测试用例test_a") 24 25 def test_b(self): 26 print("测试用例test_b")
运行结果:
1 test_01.py::TestPy01::test_one setup_module,整个test.py模块只执行一次 2 测试用例test_one 3 PASSED 4 test_01.py::TestPy01::test_two 测试用例test_two 5 PASSED 6 test_01.py::TestPy02::test_a 测试用例test_a 7 PASSED 8 test_01.py::TestPy02::test_b 测试用例test_b 9 PASSEDteardown_module,整个test.py模块只执行一次
这个方法只作用于test.py模块,且只在模块始末执行一次,简单的理解就是会在所有用例执行前执行setup_module,在所有用例执行完之后执行teardown_module。
函数级(setup_function/teardown_function)
#注:红色部分为函数级新增内容
1 # Author xuejie zeng 2 # encoding utf-8 3 4 def setup_module(): 5 print("setup_module,整个test.py模块只执行一次") 6 7 def teardown_module(): 8 print("teardown_module,整个test.py模块只执行一次") 9 10 11 def setup_function(): 12 print("setup_function,在函数始末调用(在类外部)") 13 14 def teardown_function(): 15 print("teardown_function,在函数始末调用(在类外部)") 16 17 18 class TestPy01(): 19 20 def test_one(self): 21 print("测试用例test_one") 22 23 def test_two(self): 24 print("测试用例test_two") 25 26 27 class TestPy02(): 28 29 def test_a(self): 30 print("测试用例test_a") 31 32 def test_b(self): 33 print("测试用例test_b") 34 35 36 37 def test_alone(): 38 print('在类外部的函数')
运行结果:
1 test_01.py::TestPy01::test_one setup_module,整个test.py模块只执行一次 2 测试用例test_one 3 PASSED 4 test_01.py::TestPy01::test_two 测试用例test_two 5 PASSED 6 test_01.py::TestPy02::test_a 测试用例test_a 7 PASSED 8 test_01.py::TestPy02::test_b 测试用例test_b 9 PASSED 10 test_01.py::test_alone setup_function,在函数始末调用(在类外部) 11 在类外部的函数 12 PASSEDteardown_function,在函数始末调用(在类外部) 13 teardown_module,整个test.py模块只执行一次
从此次运行结果看,setup_function/teardown_function只会对类以后的函数起作用,作用域仅仅是类之后的函数。
类级(setup_class/teardown_class)
#注:红色部分为类级新增内容
1 # Author xuejie zeng 2 # encoding utf-8 3 4 def setup_module(): 5 print("setup_module,整个test.py模块只执行一次") 6 7 def teardown_module(): 8 print("teardown_module,整个test.py模块只执行一次") 9 10 11 def setup_function(): 12 print("setup_function,在函数始末调用(在类外部)") 13 14 def teardown_function(): 15 print("teardown_function,在函数始末调用(在类外部)") 16 17 class TestPy01(object): 18 @classmethod 19 def setup_class(cls): 20 print("setup_class,在类中执行,只执行一次") 21 @classmethod 22 def teardown_class(cls): 23 print("teardown_class,在类中执行,只执行一次") 24 25 def test_one(self): 26 print("测试用例test_one") 27 28 def test_two(self): 29 print("测试用例test_two") 30 31 32 class TestPy02(): 33 34 def test_a(self): 35 print("测试用例test_a") 36 37 def test_b(self): 38 print("测试用例test_b") 39 40 def test_alone(): 41 print('在类外部的函数')
运行结果:
1 test.py::TestPy01::test_one setup_module,整个test.py模块只执行一次 2 setup_class,在类中执行,只执行一次 3 测试用例test_one 4 PASSED 5 test.py::TestPy01::test_two 测试用例test_two 6 PASSEDteardown_class,在类中执行,只执行一次 7 8 test.py::TestPy02::test_a 测试用例test_a 9 PASSED 10 test.py::TestPy02::test_b 测试用例test_b 11 PASSED 12 test.py::test_alone setup_function,在函数始末调用(在类外部) 13 在类外部的函数 14 PASSEDteardown_function,在函数始末调用(在类外部) 15 teardown_module,整个test.py模块只执行一次
setup_class/teardown_class方法只用于类的始末,如上:只在测试类TestPy01中的始末进行执行,TestPy02不会执行。
方法级(setup_method/teardown_methond)
#注:红色部分为方法级新增内容
1 # Author xuejie zeng 2 # encoding utf-8 3 4 def setup_module(): 5 print("setup_module,整个test.py模块只执行一次") 6 7 def teardown_module(): 8 print("teardown_module,整个test.py模块只执行一次") 9 10 11 def setup_function(): 12 print("setup_function,在函数始末调用(在类外部)") 13 14 def teardown_function(): 15 print("teardown_function,在函数始末调用(在类外部)") 16 17 class TestPy01(object): 18 @classmethod 19 def setup_class(cls): 20 print("setup_class,在类中执行,只执行一次") 21 @classmethod 22 def teardown_class(cls): 23 print("teardown_class,在类中执行,只执行一次") 24 25 def setup_method(self): 26 print("setup_method_1,类中的每个方法都会执行一次") 27 28 def teardown_method(self): 29 print("teardown_method_1,类中的每个方法都会执行一次") 30 31 def test_one(self): 32 print("测试用例test_one") 33 34 def test_two(self): 35 print("测试用例test_two") 36 37 38 class TestPy02(): 39 def setup_method(self): 40 print("setup_method_2,类中的每个方法都会执行一次") 41 42 def teardown_method(self): 43 print("teardown_method_2,类中的每个方法都会执行一次") 44 def test_a(self): 45 print("测试用例test_a") 46 47 def test_b(self): 48 print("测试用例test_b") 49 50 def test_alone(): 51 print('在类外部的函数')
运行结果:
1 test.py::TestPy01::test_one setup_module,整个test.py模块只执行一次 2 setup_class,在类中执行,只执行一次 3 setup_method_1,类中的每个方法都会执行一次 4 测试用例test_one 5 PASSEDteardown_method_1,类中的每个方法都会执行一次 6 7 test.py::TestPy01::test_two setup_method_1,类中的每个方法都会执行一次 8 测试用例test_two 9 PASSEDteardown_method_1,类中的每个方法都会执行一次 10 teardown_class,在类中执行,只执行一次 11 12 test.py::TestPy02::test_a setup_method_2,类中的每个方法都会执行一次 13 测试用例test_a 14 PASSEDteardown_method_2,类中的每个方法都会执行一次 15 16 test.py::TestPy02::test_b setup_method_2,类中的每个方法都会执行一次 17 测试用例test_b 18 PASSEDteardown_method_2,类中的每个方法都会执行一次 19 20 test.py::test_alone setup_function,在函数始末调用(在类外部) 21 在类外部的函数 22 PASSEDteardown_function,在函数始末调用(在类外部) 23 teardown_module,整个test.py模块只执行一次
setup_method/teardown_methond和setup/teardown的使用方法是一样,setup/teardown其实和unitest里的是一样的,这两种选择哪种方式都可以。
调用优先级:
setup_module > setup_class >setup_method > setup > teardown > teardown_method > teardown_class > teardown_module
因为setup_function/teardown_function方法在类之外的函数才生效,所以暂不参与排序。
关注个人公众号:测试开发进阶之路