2 setup and teardown in the test script class

Posted by sbourdon on Sun, 19 Dec 2021 19:30:49 +0100

catalogue

text

Setup and teardown are very common preconfiguration and cleaning configurations in test scripts, that is, some preset processing before the execution of the use case and some cleaning configuration for environment recovery after the execution of the use case. Because the level of the use case is different, setup and teardown are also divided into several types, That is, using these setup and teardown, we can make an automated test framework for a small and medium-sized project. In other words, if you deploy the automation of unit test or function test in the development team, you don't need to pay attention to other advanced functions of pytest, because everything has two sides. Using advanced functions does seem very professional and tall, but at the same time, the cost of operation and maintenance increases a lot.

Write the following test script

# test_module.py
import pytest

class TestClass(object):
    def setup_class(self):
        print("\nin TestClass.setup_class......")

    def teardown_class(self):
        print("\nin TestClass.teardown_class......")

    def test_method1(self):
        print("\nin TestClass.test_method1......")
        assert 1==1

    def test_method2(self):
        print("\nin TestClass.test_method2......")
        assert 1==1

The execution results are as follows. You can see that setup_class will be executed before all use cases of the test class, teardown_class will be executed after all test cases of the test class

$ pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

test_module.py
in TestClass.setup_class......

in TestClass.test_method1......
.
in TestClass.test_method2......
.
in TestClass.teardown_class......


========================================================================== 2 passed in 0.03s ===========================================================================

$

Write the following test script;

# test_module.py
import pytest

class TestClass(object):
    def setup_class(self):
        print("\nin TestClass.setup_class......")

    def teardown_class(self):
        print("\nin TestClass.teardown_class......")

    def setup_method(self):
        print("\nin TestClass.setup_method......")

    def teardown_method(self):
        print("\nin TestClass.teardown_method......")

    def test_method1(self):
        print("\nin TestClass.test_method1......")
        assert 1==1

    def test_method2(self):
        print("\nin TestClass.test_method2......")
        assert 1==1

The execution results are as follows. As you can see, setup_method will be executed before each test method in the test class is executed, teardown_method will be executed after the execution of each test method in the test class is completed

$ pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

test_module.py
in TestClass.setup_class......

in TestClass.setup_method......

in TestClass.test_method1......
.
in TestClass.teardown_method......

in TestClass.setup_method......

in TestClass.test_method2......
.
in TestClass.teardown_method......

in TestClass.teardown_class......


========================================================================== 2 passed in 0.03s ===========================================================================

$

Write the test script as follows:

# test_module.py
import pytest

class TestClass(object):
    def setup_class(self):
        print("\nin TestClass.setup_class......")

    def teardown_class(self):
        print("\nin TestClass.teardown_class......")

    def setup_method(self):
        print("\nin TestClass.setup_method......")

    def teardown_method(self):
        print("\nin TestClass.teardown_method......")
        
    def setup(self):
        print("\nin TestClass.setup......")
        
    def teardown(self):
        print("\nin TestClass.teardown......")

    def test_method1(self):
        print("\nin TestClass.test_method1......")
        assert 1==1

    def test_method2(self):
        print("\nin TestClass.test_method2......")
        assert 1==1

The execution results are as follows. The functions of setup and teardown are exactly the same as those of method level setup and teardown, that is, setup is executed before the execution of each test method of the class, and teardown is executed after the execution of each test method of the class. The difference is that setup is in setup_method is executed after teardown_method is executed before, that is, their priorities are different

$ pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\src\blog\tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 2 items                                                                                                                                                       

test_module.py
in TestClass.setup_class......

in TestClass.setup_method......

in TestClass.setup......

in TestClass.test_method1......
.
in TestClass.teardown......

in TestClass.teardown_method......

in TestClass.setup_method......

in TestClass.setup......

in TestClass.test_method2......
.
in TestClass.teardown......

in TestClass.teardown_method......

in TestClass.teardown_class......


========================================================================== 2 passed in 0.03s ===========================================================================

$ 

Topics: Python pytest