Pytest learn and use setup, teardown and other methods in 4-pytest and Unittest (the most complete)

Posted by Adam W on Thu, 09 Sep 2021 20:22:29 +0200

1 Unittest two pre and two post methods

  • When using Unittest framework and selenium to do Web UI automation test, we often encounter when to open and close the browser. At this time, we use two pre and post methods of Unittest;
  • What are the four methods? See the table below:
methodexplain
setup()Execute once before executing a use case. For example, open the browser once before running a use case
teardown()Execute once after each use case is executed. For example, close the browser every time a use case is run
setupClass()Execute once before executing a use case set, for example, open the browser once before running a testcase
teardownClass()Execute once after executing a use case set, for example, close the browser once after running a testcase
  • The setupClass() and teardownClass() methods are used in conjunction with the @ classmethod method method.

1.1 Unittest: examples of setup and teardown methods

  • Create a script test_unittest_setup_teardown.py, write the following code:
# -*- coding:utf-8 -*-
# By noama Nelson
# Date: September 25, 2021
# File name: test_unittest_setup_teardown.py
# Function: verify the setup and teardown methods of unittest
# Contact: VX (noama Nelson)
# Blog: https://blog.csdn.net/NoamaNelson


import unittest


class TestOne(unittest.TestCase):
    def setUp(self) -> None:
        print("One per run case Before, open the browser once")

    def tearDown(self) -> None:
        print("One per run case After, close the browser once")

    def test_one(self):
        print("Run the first use case")

    def test_two(self):
        print("Run the second use case")


if __name__ == "__main__":
    unittest.main()
  • After operation:
Ran 2 tests in 0.003s

OK
Launching unittests with arguments python -m unittest F:/pytest_study/test_case/test_b/test_unittest_setup_teardown.py in F:\pytest_study\test_case\test_b


The process has ended with exit code 0
 One per run case Before, open the browser once
 Run the first use case
 One per run case After, close the browser once
 One per run case Before, open the browser once
 Run the second use case
 One per run case After, close the browser once

1.2 Unittest: examples of setupClass and teardownClass methods

  • Create a script test_unittest_setupclass_teardownclass.py, write the following code:
# -*- coding:utf-8 -*-
# By noama Nelson
# Date: 9:50, 2021 / 9 / 9
# File name: test_unittest_setupclass_teardownclass.py
# Function: verify the setupclass and teardownclass methods of unittest
# Contact: VX (noama Nelson)
# Blog: https://blog.csdn.net/NoamaNelson


import unittest


class TestTwo(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        print("Before running a use case set, open the browser, that is, this class only opens the browser once")

    @classmethod
    def tearDownClass(cls) -> None:
        print("After each use case set is run, close the browser, that is, this class closes the browser only once")

    def test_one(self):
        print("Run the first use case")

    def test_two(self):
        print("Run the second use case")


if __name__ == "__main__":
    unittest.main()
  • After operation:
Ran 2 tests in 0.002s


OK
 Before running a use case set, open the browser, that is, this class only opens the browser once
 Run the first use case
 Run the second use case
 After each use case set is run, close the browser, that is, this class closes the browser only once

The process has ended with exit code 0
  • Note that these two methods need to be modified with @ classmethod. If they are not added, an error will be reported.

2 ten pre and post methods of pytest

  • It is similar to unittest, but there are more methods, up to ten. See the following table for details:
methodRun levelexplain
setup_module()Module levelThe whole. py module is executed only once before starting, such as opening the browser once
teardown_module()Module levelThe whole. py module is executed only once, such as closing the browser once
setup_function()Function levelEach function level use case is executed before it starts, and this method is not in the class
teardown_function()Function levelEach function level use case is executed at the end, and this method is not in the class
setup_class()Class levelThe whole test class is executed only once before starting, which is basically the same as Unittest
teardown_class()Class levelAfter the completion of the entire test class, it is only executed once, which is basically the same as Unittest
setup_method()Method levelEach use case in the class will be executed before execution
teardown_method()Method levelClass will be executed after the end of each use case
setup()Method refinement levelEach use case in the class will be executed before execution
teardown()Method refinement levelClass will be executed after the end of each use case

2.1 Pytest: setup_module,teardown_module method example

  • Create test_pytest_setup_teardown_module.py, the code is as follows:
# -*- coding:utf-8 -*-
# By noama Nelson
# Date: 10:18, September 9, 2021
# File name: test_pytest_setup_teardown_module.py
# Function: verify the setup of pytest_ Module and teardown_module method
# Contact: VX (noama Nelson)
# Blog: https://blog.csdn.net/NoamaNelson


import pytest


def setup_module():
    print("whole.py The module is executed only once before it starts")


def teardown_module():
    print("whole.py Execute only once after the module is finished")


def test_one():
    print("Use case 1")


def test_two():
    print("Use case 2")


class TestOne():
	# @staticmethod
    # def setup_module():
    #     print("the entire. py module is executed only once before it starts")
    #
    # @staticmethod
    # def teardown_module():
    #     print("the whole. py module is executed only once after it is completed")
    def test_thr(self):
        print("Use case 3")

    def test_fo(self):
        print("Use case 4")


if __name__ == "__main__":
    pytest.main()
  • Operation results:
(venv) F:\pytest_study\test_case\test_c>pytest -s -q
 whole.py The module is executed only once before it starts
 Use case 1
.Use case 2
.Use case 3
.Use case 4
.whole.py Execute only once after the module is finished

4 passed in 0.02s
  • To write these two methods into a class, you need to use the @ staticmethod method method to modify them, otherwise the syntax is wrong, but if you write them into a class, these two methods should not run.

2.2 Pytest: setup_function,teardown_function method example

  • Create test_pytest_setup_teardown_function.py, the code is as follows:
# -*- coding:utf-8 -*-
# By noama Nelson
# Date: 10:41, September 9, 2021
# File name: test_pytest_setup_teardown_function.py
# Function: verify the setup of pytest_ function,teardown_function method
# Contact: VX (noama Nelson)
# Blog: https://blog.csdn.net/NoamaNelson


import pytest


def setup_module():
    print("whole.py The module is executed only once before it starts")


def teardown_module():
    print("whole.py Execute only once after the module is finished")


def setup_function():
    print("Each function level use case is executed before it starts")


def teardown_function():
    print("Each function level use case is executed at the end")


def test_one():
    print("Use case 1")


def test_two():
    print("Use case 2")


class TestOne():

    def test_thr(self):
        print("Use case 3")

    def test_fo(self):
        print("Use case 4")


if __name__ == "__main__":
    pytest.main()

  • The operation is as follows:
(venv) F:\pytest_study\test_case\test_c>pytest -s -q test_pytest_setup_teardown_function.py
 whole.py The module is executed only once before it starts
 Each function level use case is executed before it starts
 Use case 1
.Each function level use case is executed at the end
 Each function level use case is executed before it starts
 Use case 2
.Each function level use case is executed at the end
 Use case 3
.Use case 4
.whole.py Execute only once after the module is finished

4 passed in 0.42s
  • If you also write these two methods into a class, you need to use the @ staticmethod method method to modify them, otherwise the syntax is wrong, but if you write them into a class, these two methods should not run.

2.3 Pytest: setup_class,teardown_class method example

  • Create test_ setup_ teardoen_ The class.py code is as follows:
# -*- coding:utf-8 -*-
# By noama Nelson
# Date: 11:28, September 9, 2021
# File name: test_pytest_setup_teardoen_class.py
# Function: xxx
# Contact: VX (noama Nelson)
# Blog: https://blog.csdn.net/NoamaNelson


import pytest


def setup_module():
    print("whole.py The module is executed only once before it starts")


def teardown_module():
    print("whole.py Execute only once after the module is finished")


def test_one():
    print("Use case 1")


def test_two():
    print("Use case 2")


class TestOne():

    def setup_class(self):
        print("The entire test class is executed only once before it starts")

    def teardown_class(self):
        print("The entire test class is executed only once")


    def test_thr(self):
        print("Use case 3")

    def test_fo(self):
        print("Use case 4")


if __name__ == "__main__":
    pytest.main()
  • The operation result is:
(venv) F:\pytest_study\test_case\test_c>pytest -s -q test_setup_teardoen_class.py
 whole.py The module is executed only once before it starts
 Use case 1
.Use case 2
.The entire test class is executed only once before it starts
 Use case 3
.Use case 4
.The entire test class is executed only once
 whole.py Execute only once after the module is finished

4 passed in 0.02s

2.4 Pytest: setup_method,teardown_method method example

  • Create test_pytest_setup_teardown_method.py, the code is as follows:
# -*- coding:utf-8 -*-
# By noama Nelson
# Date: 12:28, September 9, 2021
# File name: test_pytest_setup_teardown_method.py
# Function: verify the setup of pytest_ method,teardown_method method
# Contact: VX (noama Nelson)
# Blog: https://blog.csdn.net/NoamaNelson


import pytest


def setup_module():
    print("whole.py The module is executed only once before it starts")


def teardown_module():
    print("whole.py Execute only once after the module is finished")


def test_one():
    print("Use case 1")


def test_two():
    print("Use case 2")


class TestOne():

    def setup_class(self):
        print("The entire test class is executed only once before it starts")

    def teardown_class(self):
        print("The entire test class is executed only once")

    def setup_method(self):
        print("1 Each use case in the class will be executed before execution")

    def teardown_method(self):
        print("1 Class will be executed after the end of each use case")

    def test_thr(self):
        print("Use case 3")

    def test_fo(self):
        print("Use case 4")


if __name__ == "__main__":
    pytest.main()
  • The operation result is:
(venv) F:\pytest_study\test_case\test_c>pytest -s -q test_pytest_setup_teardown_method.py
 whole.py The module is executed only once before it starts
 Use case 1
.Use case 2
.The entire test class is executed only once before it starts
1 Each use case in the class will be executed before execution
 Use case 3
.1 Class will be executed after the end of each use case
1 Each use case in the class will be executed before execution
 Use case 4
.1 Class will be executed after the end of each use case
 The entire test class is executed only once
 whole.py Execute only once after the module is finished

4 passed in 0.14s

2.5 Pytest: examples of setup and teardown methods

  • Create test_pytest_setup_teardown.py, the code is as follows:
# -*- coding:utf-8 -*-
# By noama Nelson
# Date: 12:28, September 9, 2021
# File name: test_pytest_setup_teardown.py
# Function: verify the setup and teardown methods of pytest
# Contact: VX (noama Nelson)
# Blog: https://blog.csdn.net/NoamaNelson


import pytest


def setup_module():
    print("setup_module: whole.py The module is executed only once before it starts")


def teardown_module():
    print("teardown_module: whole.py Execute only once after the module is finished")


def setup_function():
    print("setup_function: Each function level use case is executed before it starts")


def teardown_function():
    print("teardown_function: Each function level use case is executed at the end")

def test_one():
    print("Use case 1")


def test_two():
    print("Use case 2")


class TestOne():

    def setup_class(self):
        print("setup_class: The entire test class is executed only once before it starts")

    def teardown_class(self):
        print("teardown_class: The entire test class is executed only once")

    def setup_method(self):
        print("setup_method: Each use case in the class will be executed before execution")

    def teardown_method(self):
        print("teardown_method: Class will be executed after the end of each use case")

    def setup(self):
        print("setup: Each use case in the class will be executed before execution")

    def teardown(self):
        print("teardown: Class will be executed after the end of each use case")

    def test_thr(self):
        print("Use case 3")

    def test_fo(self):
        print("Use case 4")


if __name__ == "__main__":
    pytest.main()
  • The operation results are as follows:
(venv) F:\pytest_study\test_case\test_c>pytest -s -q test_pytest_setup_teardown.py
setup_module: whole.py The module is executed only once before it starts
setup_function: Each function level use case is executed before it starts
 Use case 1
.teardown_function: Each function level use case is executed at the end
setup_function: Each function level use case is executed before it starts
 Use case 2
.teardown_function: Each function level use case is executed at the end
setup_class: The entire test class is executed only once before it starts
setup_method: Each use case in the class will be executed before execution
setup: Each use case in the class will be executed before execution
 Use case 3
.teardown: Class will be executed after the end of each use case
teardown_method: Class will be executed after the end of each use case
setup_method: Each use case in the class will be executed before execution
setup: Each use case in the class will be executed before execution
 Use case 4
.teardown: Class will be executed after the end of each use case
teardown_method: Class will be executed after the end of each use case
teardown_class: The entire test class is executed only once
teardown_module: whole.py Execute only once after the module is finished

4 passed in 0.14s

"Full stack testing technology, sharing, mutual encouragement, progress and improvement"

Topics: pytest