pytest学习总结2.18 - 如何在unittest基础上使用pytest?

2.18 如何在 unittest 的基础上使用 pytest

要使用pytest运行现有的单元测试式测试套件,请键入:pytest tests
pytest 将自动收集单位测试 test_*.py*_test.py 文件中的测试案例子类及其测试方法,支持unittest的特性:
• @unittest.skip style decorators;
• setUp/tearDown;
• setUpClass/tearDownClass;
• setUpModule/tearDownModule;

2.18.1 拿来即用

通过使用pytest运行测试套件,您可以使用几个特性,在大多数情况下不需要修改现有的代码:

  • 获取更多信息的回溯;
  • 标准输出和标准捕获;
  • 使用-k和-m标志的测试选择选项;maxfail;
  • -pdb命令行选项调试测试失败(见下文注);
  • 使用pytest-xdist插件将测试分发给多个cpu;
  • 使用简单的断言语句代替自我断言*函数(统一测试2个在这方面非常有帮助);

2.18.2 测试用例子类的特征

unittest.TestCase 以它为基类的,pytest支持:

  • Marks: skip, skipif, xfail;
  • Auto-use fixtures;
    不支持:
  • Fixtures
  • Parametrization;
  • Custom hooks;

2.18.3 使用标记将 pytest 固定装置混合到 unittest.TestCase 子类中

使用 pytest 运行单元测试允许您将其夹具机制与 unittest.TestCase 样式测试一起使用。

# content of conftest.py
import pytest
@pytest.fixture(scope="class")
def db_class(request):
    class DummyDB:
        pass
    # 在调用测试上下文中设置类属性
    request.cls.db = DummyDB()
# content of test_unittest_db.py
import unittest
import pytest
@pytest.mark.usefixtures("db_class")
class MyTest(unittest.TestCase):
    def test_method1(self):
        assert hasattr(self, "db")
        assert 0, self.db # fail for demo purposes
    def test_method2(self):
        assert 0, self.db # fail for demo purposes

2.18.4 使用 autouse 固定装置和访问其他固定装置

测试用例中不能直接接受固定装置,initdir函数在每个测试用例中执行一次,相当于这个类的测试前置,
比如登陆是全局变量,但是进入某个界面这个操作需要适配各个类,就可以写在这里:

# content of test_unittest_cleandir.py

import pytest
import unittest
class MyTest(unittest.TestCase):

    @pytest.fixture(autouse=True)
    def initdir(self, tmp_path, monkeypatch):
        monkeypatch.chdir(tmp_path) # 更改为 pytest 提供的临时目录
        tmp_path.joinpath("samplefile.ini").write_text("# testdata")

    def test_method(self):
        with open("samplefile.ini") as f:
            s = f.read()
        assert "testdata" in s

2.19 如何运行鼻子测试【不深入】

2.20 如何是实现 xunit-style 的 set-up

2.20.1 模块级别的 setup、teardown

如果您在单个模块中有多个测试功能和测试类,您可以选择实现以下夹具,通常会为所有函数调用一次的方法:

def setup_module(module):
    """ setup any state specific to the execution of the given module."""
def teardown_module(module):
    """teardown any state that was previously setup with a setup_module
    method.
    """

2.20.2 类级别的 setup、teardown

类似地,在调用类的所有测试方法之前和之后,会在类级别调用以下方法:

@classmethod
def setup_class(cls):
    """setup any state specific to the execution of the given class (which
    usually contains tests).
    """
@classmethod
def teardown_class(cls):
    """teardown any state that was previously setup with a call to
    setup_class.
    """

2.20.3 方法级别的 setup、teardown

类似地,将围绕每个方法调用来调用以下方法:

def setup_method(self, method):
    """setup any state tied to the execution of the given method in a
    class. setup_method is invoked for every test method of a class.
    """
def teardown_method(self, method):
    """teardown any state that was previously setup with a setup_method
    call.
    """

2.21 【不深入】

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿_焦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值