Pytest测试框架(二):pytest 的setup/teardown方法

系列文章目录

Pytest测试框架(一):pytest安装及用例执行
Pytest测试框架(二):pytest 的setup/teardown方法
Pytest测试框架(三):pytest fixture 用法
Pytest测试框架(四):pytest 参数化用例
Pytest测试框架(五):pytest + allure生成测试报告
Pytest插件开发



PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性。pytest的setup/teardown方法包括:模块级别(setup_module/teardown_module)、函数级别(setup_function/teardown_function)、类级别(setup_class/ teardown_class)、方法级别(setup_method/teardown_methond或者setup/teardown)。

模块级别

模块中的第一个测试用例开始前执行setup_module方法,模块中的最后一个测试用例结束后运行teardown_module方法。

import pytest

def setup_module():
    print("初始化。。。")

def teardown_module():
    print("清理。。。")

class Test_Demo():
    def test_case1(self):
        print("开始执行测试用例1")
        assert 1 + 1 == 2

    def test_case2(self):
        print("开始执行测试用例2")
        assert 2 + 8 == 10

    def test_case3(self):
        print("开始执行测试用例3")
        assert 99 + 1 == 100

结果:

模块初始化。。。
PASSED                     [ 33%]开始执行测试用例1
PASSED                     [ 66%]开始执行测试用例2
PASSED                     [100%]开始执行测试用例3
模块清理。。。

函数级别

setup_function/teardown_function在每个测试函数前后运行,只对函数用例生效,不在类中。

import pytest

def setup_function():
    print("初始化。。。")

def teardown_function():
    print("清理。。。")

def test_case1():
    print("开始执行测试用例1")
    assert 1 + 1 == 2

def test_case2():
    print("开始执行测试用例2")
    assert 2 + 8 == 10

def test_case3():
    print("开始执行测试用例3")
    assert 99 + 1 == 100

结果:

test_setup_teardown2.py::test_case1 初始化。。。
PASSED                               [ 33%]开始执行测试用例1
清理。。。

test_setup_teardown2.py::test_case2 初始化。。。
PASSED                               [ 66%]开始执行测试用例2
清理。。。

test_setup_teardown2.py::test_case3 初始化。。。
PASSED                               [100%]开始执行测试用例3
清理。。。

类级别

类级别函数 setup_class/teardown_class 对类有效,位于类中,在测试类中前后调用一次。

class Test_Demo():
    def setup_class(self):
        print("初始化。。。")

    def teardown_class(self):
        print("清理。。。")

    def test_case1(self):
        print("开始执行测试用例1")
        assert 1 + 1 == 2

    def test_case2(self):
        print("开始执行测试用例2")
        assert 2 + 8 == 10

    def test_case3(self):
        print("开始执行测试用例3")
        assert 99 + 1 == 100

结果:

初始化。。。
PASSED                    [ 33%]开始执行测试用例1
PASSED                    [ 66%]开始执行测试用例2
PASSED                    [100%]开始执行测试用例3
清理。。。

方法级别

方法级别函数 setup_method/teardown_method和setup/teardown对类有效,也位于类中,这两个效果一样,在测试类中每个测试方法前后调用一次。

class Test_Demo():
    def setup_method(self):
        print("初始化。。。")

    def teardown_method(self):
        print("清理。。。")

    def test_case1(self):
        print("开始执行测试用例1")
        assert 1 + 1 == 2

    def test_case2(self):
        print("开始执行测试用例2")
        assert 2 + 8 == 10

    def test_case3(self):
        print("开始执行测试用例3")
        assert 99 + 1 == 100

结果:

初始化。。。
PASSED                    [ 33%]开始执行测试用例1
清理。。。
初始化。。。
PASSED                    [ 66%]开始执行测试用例2
清理。。。
初始化。。。
PASSED                    [100%]开始执行测试用例3
清理。。。
--THE END--

文章标题:Pytest测试框架(二):pytest 的setup/teardown方法
本文作者:hiyo
本文链接:https://hiyong.gitee.io/posts/pytest-xunit-style-of-setup-teardown/
欢迎关注公众号:「测试开发小记」及时接收最新技术文章!

  • 14
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
pytest和unittest都是Python中常用的测试框架。其中,setup和teardown是两个常用的测试用例执行前和执行后的钩子函数。 在pytest中,setup和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函数。 在unittest中,setup和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函数,同时也提供了更多的扩展性和定制化选项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值