pytest框架结构2

1.与unittest类似,执行用例前后会执行setup,teardown来增加用例的前置和后置条件。pytest框架中使用setup,teardown更灵活,按照用例运行级别可以分为以下几类。

  • 模块级(setup_module/teardown_module)在模块始末调用
  • 函数级 (setup_function/teardown_function)在函数始末调用(在类外)
  • 类级 (setup_class/teardown_class)在类始末调用(在类中)
  • 方法级 (setup_method/teardown_method)在方法始末调用(在类中)
  • 方法级 (setup/teardown)在方法始末调用(在类中)
    调用顺序:setup_module->setup_class->setup_method>setup>teardown>teardown_method>teardown_class>teardown_module
    创建文件test_run_step.py验证上面的顺序
def setup_module():
    print("\nsetup_module,只执行一次,当有多个测试类的时候使用")
def teardown_module():
    print("\nteardown_module,只执行一次,当有多个测试类的时候使用")
class TestPytest1(object):
    @classmethod
    def setup_class(cls):
        print("\nsetup_class1,只执行一次")
    @classmethod
    def teardown_class(cls):
        print("\nteardown_class1,只执行一次")
    def setup_method(self):
        print("\nsetup_method1,每个测试方法都执行一次")
    def teardown_method(self):
        print("\nteardown_method1,每个测试方法都执行一次")
    def test_three(self):
        print("test_three,测试用例")
    def test_four(self):
        print("test_four,测试用例")

class TestPytest2(object):
    @classmethod
    def setup_class(cls):
        print("\nsetup_class1,只执行一次")
    @classmethod
    def teardown_class(cls):
        print("\nteardown_class1,只执行一次")
    def setup_method(self):
        print("\nsetup_method1,每个测试方法都执行一次")
    def teardown_method(self):
        print("\nteardown_method1,每个测试方法都执行一次")
    def test_two(self):
        print("test_three,测试用例")
    def test_one(self):
        print("test_four,测试用例")
E:\pycharm_test\qiyeweixin\test_chart\.pytest_cache\Test0918>pytest -vs  test_add.py
============================================================== test session starts ===============================================================
platform win32 -- Python 3.8.1, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- d:\python3\python.exe
cachedir: .pytest_cache
rootdir: E:\pycharm_test\qiyeweixin\test_chart\.pytest_cache\Test0918
plugins: allure-pytest-2.8.16
collected 4 items                                                                                                                                 

test_add.py::TestPytest1::test_three
setup_module,只执行一次,当有多个测试类的时候使用

setup_class1,只执行一次

setup_method1,每个测试方法都执行一次
test_three,测试用例
PASSED
teardown_method1,每个测试方法都执行一次

test_add.py::TestPytest1::test_four
setup_method1,每个测试方法都执行一次
test_four,测试用例
PASSED
teardown_method1,每个测试方法都执行一次

teardown_class1,只执行一次

test_add.py::TestPytest2::test_two
setup_class1,只执行一次

setup_method1,每个测试方法都执行一次
test_three,测试用例
PASSED
teardown_method1,每个测试方法都执行一次

test_add.py::TestPytest2::test_one
setup_method1,每个测试方法都执行一次
test_four,测试用例
PASSED
teardown_method1,每个测试方法都执行一次

teardown_class1,只执行一次

teardown_module,只执行一次,当有多个测试类的时候使用


=============================================================== 4 passed in 0.05s ================================================================

从上面的结果看出setup_module和teardown_module在整个模块只执行一次,setup_class和teardown_class在类里面只执行一次,setup_method和teardown_method在每个方法前后都会调用。

posted @ 2022-09-18 21:41  当时只道是寻常呀  阅读(43)  评论(0)    收藏  举报