python - 在单元测试 python 中出现失败步骤时运行测试用例

标签 python python-unittest

我有一个 Python 测试套件,其中包含几个使用单元测试框架的测试用例。我正在为此使用导入单元测试

如果测试用例中的测试步骤失败,则测试用例进入拆解类。即使一个测试用例失败,我也想继续测试用例的其余部分。

单元测试的默认行为:如果任何测试步骤失败,它将拆除类并结束测试用例。

测试步骤:我的意思是 assert equal 并且喜欢这个定义它是失败还是通过的内置过程

最佳答案

考虑到你有这样的事情:

class TestFoo(unittest.TestCase):
    def test_case_1(self):
        self.assertEqual(1, 2)
        self.assertEqual(1, 3)
        self.assertEqual(1, 1)

第一个断言将失败,随后的断言将不会运行,但测试将结束并调用 self.tearDown 方法。

当您断言时,您的意思是该条件强制为真,或者继续下去没有任何意义。你的意思是这样的:“如果一不等于二,那么生活就没有意义了......”:)

大多数时候都是这样。例如,如果结果代码不是预期的,您就不想检查 rest API 的结果主体。

您应该一步测试所有等式,以确保它们都将被检查。您可以将它们分组为一个元组,例如:

class TestFoo(unittest.TestCase):
    def test_case_1(self):
        self.assertEqual((1, 1, 1), (2, 3, 1))

然后你会得到这样的输出:

AssertionError: Tuples differ: (1, 1, 1) != (2, 3, 1)

First differing element 0:
1
2

- (1, 1, 1)
?  ^  ^

+ (2, 3, 1)
?  ^  ^

来自 python 文档:

assertEqual(first, second, msg=None) Test that first and second are equal. If the values do not compare equal, the test will fail.

In addition, if first and second are the exact same type and one of list, tuple, dict, set, frozenset or str or any type that a subclass registers with addTypeEqualityFunc() the type-specific equality function will be called in order to generate a more useful default error message (see also the list of type-specific methods).

https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertEqual https://docs.python.org/3.6/library/unittest.html#unittest.TestCase.assertEqual

[编辑]——在对这个问题进行了一些澄清之后评论了自己,但还没有代码示例。

考虑到您有不同的东西,例如:

class TestBar(unittest.TestCase):
    def test_rest_api_foo(self):
        r = request("http://dummyurl")
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.text, "hello world")

你可以做很多(hacky)事情,比如计算失败次数而不是在最后断言和断言计数器值:

class TestBar(unittest.TestCase):
    def test_rest_api_foo(self):
        r = request("http://dummyurl")
        errors = (r.status_code != 200)
        errors += (r.text != "hello world")
        self.assertEqual(errors, 0)

或者创建一个辅助函数来断言、记录、计数和吞下异常,然后在最后断言计数器。有很多可能性。

但是,一般来说,最好的(最简单、清晰、无聊的)方式应该是评论中已经建议的方式:

class TestBar(unittest.TestCase):
    def test_rest_api_status_code_foo(self):
        r = request("http://dummyurl")
        self.assertEqual(r.status_code, 200)

    def test_rest_api_text_foo(self):
        r = request("http://dummyurl")
        self.assertEqual(r.text, "hello world")

请注意,在此特定示例中,单个测试用例会更好。

无论如何,这会为您提供单独的错误消息,同时保持代码简单且易于维护。

否则,如果您的问题是因为在每个测试用例之后调用了 tearDown 方法,那么您可以改用 tearDownClass 进行清理(python3 或来自 unittest2)。

我确定我没有在这里涵盖您的实际问题的所有可能性,但也许这有助于显示您的未决问题有多少种可能性。所以,请澄清你的问题。

关于python - 在单元测试 python 中出现失败步骤时运行测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48072486/

相关文章:

python - cython:将 2D numpy 数组传递给 cdef 函数

python - 在python列表中查找连续整数

python - 在整个测试套件或 unittest/pytest 中的 at_exit 之后运行命令的 Hook

python - 修补从模拟类调用的类函数

python - 如何为 python 包中的可选依赖项编写单元测试?

python - Python Unittest 能否返回单个测试的结果代码?

python - 是否可以从 python unittest.TestCase 运行单个测试方法并引用该方法?

python - 模块未找到错误 : No module named 'Cython' when installing pyarrow

python - 使用 pywinauto 获取窗口的属性

python - 如何乘以 Python Xarray 数据集?