当前位置:网站首页>2-setup and teardown

2-setup and teardown

2021-06-22 00:02:58 RockChe

Preface

When we do Automation , There's always a need :

  • When executing each use case , Restart the browser once

  • At the end of each use case execution , All clear test data

stay unittest in , We can use setUp() and tearDown() Two ways to achieve the above requirements , among setUp() Method is used to initialize the test firmware ; and tearDown() Method is used to destroy test firmware . The program will run each test case ( With test_ Opening method ) Automatic execution before setUp() Method to initialize the test firmware , Well in each test case ( With test_ Opening method ) Automatically execute after running tearDown() Method to destroy the test firmware .

So how to start the browser only once , The requirement to clear data after all test cases are executed ?

  • unittest Provides setUpClass() and tearDownClass() Two methods , coordination @classmethod The decorator can be used

As compared to unittest A stronger framework ,pytest Naturally, there are similar methods

pytest Of setup/teardown Methods include :

  • Module level (setup_module/teardown_module)
  • Function level (setup_function/teardown_function)
  • Class level (setup_class/ teardown_class)
  • The method level (setup_method/teardown_methond perhaps setup/teardown)

Method statement

Module level

Once before the first use case in the module starts setup_module Method , Once after the end of the last test case in the module teardown_module Method

import pytest


def setup_module():
    print(" perform setup_module")


def teardown_module():
    print(" perform teardown_module")


class TestDemo(object):
    def test_case1(self):
        print(" Execute test case 1")
        assert 1 + 1 == 2

    def test_case2(self):
        print(" Execute test case 2")
        assert 1 + 3 == 4

    def test_case3(self):
        print(" Execute test case 3")
        assert 1 + 5 == 6

The operation results are as follows :

image-20210621231421826

Function level

Run once before each test function setup_function Method , Run once at the end of each test function teardown_function Method , Only for function use cases , Not in class .

import pytest


def setup_function():
    print(" perform setup_function")


def teardown_function():
    print(" perform teardown_function")


def test_case1():
    print(" Execute test case 1")
    assert 1 + 1 == 2


def test_case2():
    print(" Execute test case 2")
    assert 1 + 3 == 4


def test_case3():
    print(" Execute test case 3")
    assert 1 + 5 == 6

The operation results are as follows :

image-20210621231355291

Class level

setup_class/teardown_class Valid for class , In class , Called before and after the test class is executed

import pytest


class TestDemo(object):
    def setup_class(self):
        print(" perform setup_class")

    def teardown_class(self):
        print(" perform teardown_class")

    def test_case1(self):
        print(" Execute test case 1")
        assert 1 + 1 == 2

    def test_case2(self):
        print(" Execute test case 2")
        assert 1 + 3 == 4

    def test_case3(self):
        print(" Execute test case 3")
        assert 1 + 5 == 6

The operation results are as follows :

image-20210621231310442

The method level

setup_method/teardown_method and setup/teardown, In the test class, each test method is called back and forth. . The effect of these two methods is the same

import pytest


class TestDemo(object):
    def setup_method(self):
        print(" perform setup_method")

    def teardown_method(self):
        print(" perform teardown_method")

    def test_case1(self):
        print(" Execute test case 1")
        assert 1 + 1 == 2

    def test_case2(self):
        print(" Execute test case 2")
        assert 1 + 3 == 4

    def test_case3(self):
        print(" To perform a test 3")
        assert 1 + 5 == 6

The operation results are as follows :

image-20210621231121966

Mixed use of four levels

If you mix these four levels of methods , What's the order of operation ?

import pytest


def setup_module():
    print(" At the beginning of the module , perform setup_module")


def teardown_module():
    print(" At the end of the module , perform teardown_module")


def setup_function():
    print(" At the beginning of the function use case , perform setup_function")


def teardown_function():
    print(" At the end of the function use case , perform teardown_function")


def test_a():
    print(" Execute test function a")


def test_b():
    print(" Execute test function b")


class TestDemo(object):
    def setup_class(self):
        print(" At the beginning of the test class , perform setup_class")

    def teardown_class(self):
        print(" At the end of the test class , perform teardown_class")

    def setup_method(self):
        print(" When a method in a class starts , perform setup_method")

    def teardown_method(self):
        print(" At the end of a method in a class , perform teardown_method")

    def test_case1(self):
        print(" Execute test case 1")
        assert 1 + 1 == 2

    def test_case2(self):
        print(" Execute test case 2")
        assert 1 + 3 == 4

    def test_case3(self):
        print(" Execute test case 3")
        assert 1 + 5 == 6

The operation results are as follows :

image-20210621233905952

summary

  • Module level (setup_module/teardown_module) Start at the beginning and end of the module , Overall
  • Function level (setup_function/teardown_function) Only for function use cases ( Not in class )
  • Class level (setup_class/teardown_class) Run back and forth in the class only once ( In class )
  • Method level (setup_method/teardown_method or setup/teardown) Start from the beginning and end of the method ( In class )

版权声明
本文为[RockChe]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/06/20210622000029010s.html