如何将多个断言语句转换或合并为一个

huangapple go评论63阅读模式
英文:

How to convert or merge multiple assert statements into one

问题

Here's the translated code for your request:

我在python测试函数中有多个assert语句如下所示
```python
def test_function():
    assert A.a == 1
    assert B.b == 2
    assert C.c == 3
    assert D.d == 4
    assert E.e == 5
    assert F.f == 6
    assert G.g == 7

我只想以一种合适的动态方式编写此测试函数,所以我尝试了以下代码:

import unittest
def test_function():
    x = {"a": A.a, "b": B.b, "c": C.c, "d": D.d, "e": E.e, "f": F.f, "g": G.g}
    y = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7}
    for k, v in x.items():
        for k1, v1 in y.items():
            if k == k1 and v == v1:
                assert v == v1

但我发现这也不是一个好方法,因为不需要if语句,但如果我删除if语句会出错。

我正在寻找一种更好的方法来合并这些assert语句。


编辑: 简单地说,我可以将所有的A.a, B.a....1, 2, ..放在列表中,如下所示:

l1 = [A.a, B.b, C.c, D.d, E.e, F.f, G.g]
l2 = [1, 2, 3, 4, 5, 6, 7]

然后希望生成assert语句,如下所示:

assert l1[0] == l2[0]
assert l1[1] == l2[1]
# 以此类推,无需使用任何内置函数

<details>
<summary>英文:</summary>

I have a multiple assert statement in a python test function as below:

def test_function():
assert A.a == 1
assert B.b == 2
assert C.c == 3
assert D.d == 4
assert E.e == 5
assert F.f == 6
assert G.g == 7

I just want to write this test function in a proper dynamic way, so I have tried:

import unittest
def test_function():
x = {"a": A.a, "b": B.b, "c": C.c, "d": D.d, "e": E.e, "f":F.f, "g":G.g}
y = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f":6, "g":7}
for k, v in x.items():
for k1, v1 in y.items():
if k == k1 and v==v1:
assert v == v1

But I have found that is also not a good way, because `&quot;if&quot;` is not required, but if I remove the &quot;if&quot; statement an error occurs.

I&#39;m looking for a better way to merge this `assert` statements.
___
**EDIT:** Simply I can put all `A.a, B.a....` and `1, 2, ..` in the list like below, 

    l1 = [A.a, B.b, C.c, D.d, E.e, F.f, G.g]
    l2 = [1, 2, 3, 4, 5, 6, 7]

And from it want to make assert statement, 

    assert l1[0] == l2[0]
    assert l1[1] == l2[1].......
**without any inbuilt function**

</details>


# 答案1
**得分**: 2

如果您正在使用 `unittest`,您需要将测试放在一个继承自 `unittest.TestCase` 的类中。然后,您可以使用 `unittest.TestCase` 中的 `assertEqual()` 方法:

```python
import unittest

class MyTests(unittest.TestCase):
    def test_function(self):
        x = {"a": A.a, "b": B.b, "c": C.c, "d": D.d, "e": E.e, "f": F.f, "g": G.g}
        y = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7}
        self.assertEqual(x, y)

另一方面,如果您正在使用 pytest,您可以简单地使用 assert x == y

def test_function():
    x = {"a": A.a, "b": B.b, "c": C.c, "d": D.d, "e": E.e, "f": F.f, "g": G.g}
    y = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7}
    assert x == y
英文:

If you are using unittest, you need to put your test in a class which extends unittest.TestCase. Then you can use the assertEqual() method from unittest.TestCase:

import unittest

def MyTests(unittest.TestCase):
    def test_function(self):
        x = {&quot;a&quot;: A.a, &quot;b&quot;: B.b, &quot;c&quot;: C.c, &quot;d&quot;: D.d, &quot;e&quot;: E.e, &quot;f&quot;:F.f, &quot;g&quot;:G.g}
        y = {&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3, &quot;d&quot;: 4, &quot;e&quot;: 5, &quot;f&quot;:6, &quot;g&quot;:7}
        self.assertEqual(x, y)

On the other hand, if you are using pytest, you can just assert x == y:

def test_function():
    x = {&quot;a&quot;: A.a, &quot;b&quot;: B.b, &quot;c&quot;: C.c, &quot;d&quot;: D.d, &quot;e&quot;: E.e, &quot;f&quot;:F.f, &quot;g&quot;:G.g}
    y = {&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3, &quot;d&quot;: 4, &quot;e&quot;: 5, &quot;f&quot;:6, &quot;g&quot;:7}
    assert x == y

答案2

得分: 0

以下是翻译好的部分:

如果您想以适当的动态方式执行测试,而不使用Python的unittest,您可以使用以下的test_function()

def test_function():
    l1 = [A.a, B.b, C.c, D.d, E.e, F.f, G.g]
    l2 = [1, 2, 3, 4, 5, 6, 7]
    for i in range(0, len(l1)):
        assert l1[i] == l2[i]

先前的代码使用了您问题编辑部分中定义的列表l1l2

此外,我认为使用unittest模块更可取,在这种情况下,您可以参考@Code-Apprentice的答案。

英文:

If you want to execute the test in a proper dynamic way without using the Python unittest you can use the following test_function():

def test_function():
    l1 = [A.a, B.b, C.c, D.d, E.e, F.f, G.g]
    l2 = [1, 2, 3, 4, 5, 6, 7]
    for i in range(0, len(l1)):
        assert l1[i] == l2[i]

Previous code use the list l1 and l2 defined in the edited part of your question.

Furthermore I think that use of the unittest module is preferable and in this case you can refer to the @Code-Apprentice 's answer.

huangapple
  • 本文由 发表于 2023年4月17日 21:00:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定