Pytest series - detailed use of setup and teardown [front and rear fixtures]

foreword

Children's shoes who have used unittest know that there are two pre-methods and two post-methods; they are

  • setup()
  • setupClass()
  • teardown()
  • teardownClass()

Pytest also intimately provides methods similar to setup and teardown, and there are more than four, a total of ten

  • Module level:            setup_module, teardown_module
  • Function level:            setup_function, teardown_function, methods not in the class
  • Class level:                setup_class, teardown_class
  • Method level:             setup_method, teardown_method
  • Method refinement level:      setup, teardown

 the code

This pre- and post-method should be familiar, let’s look directly at the code and running results

import pytest


def setup_module():
    print("=====整个.py模块开始前只执行一次:打开浏览器=====")


def teardown_module():
    print("=====整个.py模块结束后只执行一次:关闭浏览器=====")


def setup_function():
    print("===每个函数级别用例开始前都执行setup_function===")


def teardown_function():
    print("===每个函数级别用例结束后都执行teardown_function====")


def test_one():
    print("one")


def test_two():
    print("two")


class TestCase():
    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_three(self):
        print("three")
def test_four(self):
        print("four")


if __name__ == '__main__':
    pytest.main(["-q", "-s", "-ra", "setup_teardown.py"])

Results of the 

Guess you like

Origin blog.csdn.net/qq_41663420/article/details/129797958