gpt4 book ai didi

python - 在独立的 pytest 测试中使用 unittest 类断言(如 assertNotIn)的最佳实践?

转载 作者:太空宇宙 更新时间:2023-11-03 14:00:19 26 4
gpt4 key购买 nike

在独立的 pytest 测试用例中,我想使用像 unittest.TestCase.assertNotIn (和 unittests other asserts )这样的断言,而不依赖于 uniittest 模块。最佳做法是使用像 python-assert 这样的库?

最佳答案

最佳实践是使用语言assert 语句,这会产生不必要的unittestassert* 方法。比较

self.assertNotIn(a, b, 'element a is not found in sequence b')

assert a not in b, 'element a is not found in sequence b'

后者更具 Python 风格。它也是 pytestdocs 中命名的关键特性之一。 :

Detailed info on failing assert statements (no need to remember self.assert* names)

如果您缺少任何 unittest 方法,pytest 旨在为它们提供替代品:

with self.assertRaises(Exception):
spam()

成为

with pytest.raises(Exception):
spam()

,

self.assertAlmostEqual(a, b)

成为

assert a == pytest.approx(b)

等等

关于python - 在独立的 pytest 测试中使用 unittest 类断言(如 assertNotIn)的最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50058667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com