英文:
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 `"if"` is not required, but if I remove the "if" statement an error occurs.
I'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 = {"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)
On the other hand, if you are using pytest
, you can just 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
答案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]
先前的代码使用了您问题编辑部分中定义的列表l1
和l2
。
此外,我认为使用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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论