跳到内容

pytest-unittest-raises-assertion (PT027)

源自 flake8-pytest-style linter。

有时提供修复。

作用

检查 unittest 模块中与异常相关的断言方法的使用。

为什么这不好?

为了强制执行 pytest 推荐的断言风格,相比于 unittest 中与异常相关的断言方法,例如 assertRaises,更推荐使用 pytest.raises

示例

import unittest


class TestFoo(unittest.TestCase):
    def test_foo(self):
        with self.assertRaises(ValueError):
            raise ValueError("foo")

建议改为

import unittest
import pytest


class TestFoo(unittest.TestCase):
    def test_foo(self):
        with pytest.raises(ValueError):
            raise ValueError("foo")

参考