pytest之setup和teardown

  单元测试框架基本上都具有setup和teardown的功能,setup用来实现用例执行前的一些操作(如:数据准备、打开浏览器等操作),而teardown用来实现用例执行完成之后的一些操作(如:数据清理、关闭浏览器等操作)。pytest作为一款强大的测试框架同样也有setup和teardown语法。

setup和teardown的运行级别:

  • 模块级(setup_module和teardown_module):全局的,作用于模块的开始和结束
  • 函数级(setup_function和teardown_function):只对函数用例生效(这种是不在类里面的)
  • 类级(setup_class和teardown_class):只在类中前后运行一次(在类里面)
  • 方法级(setup_method和teardown_method):开始于方法的始末(在类里面)
  • 类里面的(setup和teardown):运行在调用方法的前后

 

函数级:

setup_function和teardown_function:每个用例开始和结束调用一次

pytest框架支持的用例方式有两种:函数、类

我们先来看下用例方式为函数的 前置和后置用法

# file_name: test_fixt1.py

import pytest


def setup_function():
    print("setup_function:每个用例开始前都会执行一次")


def teardown_function():
    print("teardown_function:每个用例结束后都会执行一次")


def test_one():
    print("正在执行 test_one")
    a = "hello"
    b = "hello world"
    assert a in b


def test_two():
    print("正在执行 test_two")
    a = "hello"
    assert hasattr(a, "check")


def test_three():
    print("正在执行 test_three")
    a = "hello"
    b = "hello world"
    assert a == b


if __name__ == "__main__":
    pytest.main(["-s", "test_fixt1.py"])

运行结果:

  从结果可以看出用例执行顺序:setup_function》test_one 》teardown_function, setup_function》test_two 》teardown_function, setup_function》test_three 》teardown_function

setup_module和teardown_module:所有用例执行前和执行后调用一次

# file_name:test_fixt1.py

import pytest


def setup_module():
    print("setup_module:整个.py模块只执行一次")
    print("例如:数据准备")


def teardown_module():
    print("teardown_module:整个.py模块只执行一次")
    print("例如:数据清理")


def setup_function():
    print("setup_function:每个用例开始前都会执行一次")


def teardown_function():
    print("teardown_function:每个用例结束后都会执行一次")


def test_one():
    print("正在执行 test_one")
    a = "hello"
    b = "hello world"
    assert a in b


def test_two():
    print("正在执行 test_two")
    a = "hello"
    assert hasattr(a, "check")


def test_three():
    print("正在执行 test_three")
    a = "hello"
    b = "hello world"
    assert a == b


if __name__ == "__main__":
    pytest.main(["-s", "test_fixt1.py"])

运行结果:

 从结果中可以看到setup_module和teardown_module只执行了一次。

类和方法

# file_name: test_fixtclass.py

import pytest


class TestClass:

    def setup(self):
        print("setup:每个用例执行前调用一次")

    def teardown(self):
        print("teardown:每个用例执行后调用一次")

    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 test_one(self):
        print("正在执行 test_one")
        a = "hello"
        b = "hello world"
        assert a in b

    def test_two(self):
        print("正在执行 test_two")
        a = "hello"
        assert hasattr(a, "check")

    def test_three(self):
        print("正在执行 test_three")
        a = "hello"
        b = "hello world"
        assert a == b


if __name__ == '__main__':
    pytest.main(['-s', 'test_fixtclass.py'])

运行结果:

 从运行结果中可以看到,运行的优先级为:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class

注:setup和teardown的功能和setup_method和teardown_method的功能是一样的,在使用过程中用其中一种就可以了

 

posted @ 2021-02-20 17:00  lwjnicole  阅读(694)  评论(0编辑  收藏  举报