pytest系列教程——3、setup和teardown

setupteardown是用来处理用例的开始前工作和结束后的工作,pytest提供了多种类型的前置和后置,其中包括:

  • setup_module / teardown_module
  • setup_function / teardown_function
  • setup_class / teardown_class
  • setup_method / teardown_method
  • setup / teardown

代码解析

import pytest


def setup():
    print("[->]setup  类外前置")
    print("类外的setup,在所有的类外函数执行前执行\n")


def teardown():
    print("[->]teardown   类外后置")
    print("类外的teardown,在所有的类外函数执行后执行\n")


def setup_function():
    print("[->]setup_function")
    print("只对函数用例(不在class里面的)生效,有多少条函数用例执行多少次前置\n")


def teardown_function():
    print("[->]teardown_function")
    print("只对函数用例(不在class里面的)生效,有多少条函数用例执行多少次后置\n")


def setup_module():
    print("[->]setup_module")
    print("最先执行的前置,且在整个 *.py 文件执行时只执行一次\n")


def teardown_module():
    print("[->]teardown_module")
    print("最后执行的后置,且在整个 *.py 文件执行时只执行一次\n")


def test_add_01():
    print("执行类外用例test_add_01")
    assert 1+1 == 2


def test_add_02():
    print("执行类外用例test_add_02")
    assert 2+2 == 4


class TestAdd():
    def setup(self):
        print("[->]setup 类中前置")
        print("类中的setup,在所有的类外函数执行前执行\n")

    def teardown(self):
        print("[->]teardown 类中后置")
        print("类中的teardown,在所有的类外函数执行前执行\n")

    def setup_class(self):
        print("[->]setup_class")
        print("在类中运行的前置,只运行一次")

    def teardown_class(self):
        print("[->]teardown_class")
        print("在类中运行的后置,只运行一次")

    def setup_method(self):
        print("[->]setup_method")
        print("在类中每条case前运行的前置,每条case分别运行一次")

    def teardown_method(self):
        print("[->]teardown_method")
        print("在类中每条case后运行的后置,每条case分别运行一次")

    def test_add_03(self):
        print("执行类中用例test_add_03")
        assert 3+3 == 6

    def test_add_04(self):
        print("执行类中用例test_add_04")
        assert 4+4 == 8


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

运行结果:

collected 4 items

MyPytest.py 

[->]setup_module
最先执行的前置,且在整个 *.py 文件执行时只执行一次

[->]setup_function
只对函数用例(不在class里面的)生效,有多少条函数用例执行多少次前置

[->]setup  类外前置
类外的setup,在所有的类外函数执行前执行

执行类外用例test_add_01
.[->]teardown   类外后置
类外的teardown,在所有的类外函数执行后执行

[->]teardown_function
只对函数用例(不在class里面的)生效,有多少条函数用例执行多少次后置

[->]setup_function
只对函数用例(不在class里面的)生效,有多少条函数用例执行多少次前置

[->]setup  类外前置
类外的setup,在所有的类外函数执行前执行

执行类外用例test_add_02
.[->]teardown   类外后置
类外的teardown,在所有的类外函数执行后执行

[->]teardown_function
只对函数用例(不在class里面的)生效,有多少条函数用例执行多少次后置

[->]setup_class
在类中运行的前置,只运行一次
[->]setup_method
在类中每条case前运行的前置,每条case分别运行一次
[->]setup 类中前置
类中的setup,在所有的类外函数执行前执行

执行类中用例test_add_03
.[->]teardown 类中后置
类中的teardown,在所有的类外函数执行前执行

[->]teardown_method
在类中每条case后运行的后置,每条case分别运行一次
[->]setup_method
在类中每条case前运行的前置,每条case分别运行一次
[->]setup 类中前置
类中的setup,在所有的类外函数执行前执行

执行类中用例test_add_04
.[->]teardown 类中后置
类中的teardown,在所有的类外函数执行前执行

[->]teardown_method
在类中每条case后运行的后置,每条case分别运行一次
[->]teardown_class
在类中运行的后置,只运行一次
[->]teardown_module
最后执行的后置,且在整个 *.py 文件执行时只执行一次



============================== 4 passed in 0.07s ==============================

***Repl Closed***

执行顺序

通过上述执行发现类中的执行顺序优先级:

setup_class > setup_method > setup

类外的执行顺序:

setup_module > setup_function > setup

对应的teardown反之,遵循先进后出的原则

前置\后置执行结论

前置后置作用
setup_moduleteardown_module最先执行的前置或者后置,且在整个 *.py 文件执行时只执行一次
setup_functionteardown_function只对函数用例(不在class里面的)生效,有多少条函数用例执行多少次
setup_classteardown_class在类中运行的前置或者后置,只运行一次
setup_methodteardown_method在类中每条用例前后运行的前置或后置,每条case分别运行一次
setupteardown既可以在类中运行,也可以在类外运行,每条用例前后分别执行一次
pytest和unittest都是Python常用的测试框架。其setup和teardown是两个常用的测试用例执行前和执行后的钩子函数。 在pytestsetup和teardown可以通过pytest.fixture装饰器来使用。装饰器可以被附加到函数、方法或类上,以标记其为fixture。当测试函数需要使用fixture时,它们可以将fixture名称作为输入参数,pytest将自动查找和运行fixture函数,并将其输出值传递给测试函数。例如: ```python import pytest @pytest.fixture def setup(): print("执行setup操作") yield print("执行teardown操作") def test_example(setup): print("执行测试操作") ``` 在这个例子setup函数被标记为fixture,test_example函数接收setup作为输入参数。在运行test_example函数之前,pytest将自动运行setup函数,然后运行测试函数,最后再运行teardown函数。 在unittestsetup和teardown可以通过setUp和tearDown方法来实现。这些方法被定义在unittest.TestCase类,并在每次运行测试方法之前和之后自动调用。例如: ```python import unittest class TestExample(unittest.TestCase): def setUp(self): print("执行setup操作") def tearDown(self): print("执行teardown操作") def test_example(self): print("执行测试操作") ``` 在这个例子,TestExample类继承自unittest.TestCase类,并覆盖了setUp和tearDown方法。在运行test_example方法之前,unittest将自动调用setUp方法,然后运行测试方法,最后再调用tearDown方法。 总体而言,pytest和unittest都提供了简单易用的setup和teardown机制来帮助测试人员编写可靠的测试用例。但是,pytest相对于unittest更加灵活,可以通过fixture装饰器来定义setup和teardown函数,同时也提供了更多的扩展性和定制化选项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软件测试技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值