python - 如何对包含列表的元组进行单元测试(断言)?
问题描述
我有一个元组:
expected = (list, string)
第一个参数是一个列表,第二个是字符串。
列表项可以有任何顺序。通常在声明一个列表时,我会assertCountEqual()
检查项目而不考虑或顺序。如何对包含列表的元组进行单元测试?
解决方案
def assertMyTupleEqual(self, expected, actual):
self.assertEqual(type(expected), type(actual)) # check they are the same type
self.assertEqual(len(expected), len(actual)) # check they are the same length
self.assertEqual(expected[1], actual[1]) # check they have the same string
self.assertCountEqual(expected[0], actual[0]) # check they have the same list
推荐阅读
- python - 如何在python中合并两个不规则的数据框
- python - 需要检查json值是否为空python
- arrays - Json对象到数组
- elixir - 测试 Phoenix 控制器但跳过插头
- django - 如何在`django rest_framework test`的`APIClient`的标头中添加身份验证令牌
- c++ - 在覆盖方法之前自动执行父方法,而不使用构造函数/析构函数或在派生方法中显式调用它
- java - Kafka 流:使用时间窗口的聚合结果?
- python-3.x - TypeError: 'MatMul' Op 的输入'b' 的 float32 类型与参数 'a' 的 int32 类型不匹配 - 即使在强制转换之后
- c++ - 为 ibm i ILE C/C++ 指定控制语句
- bootstrap-4 - 悬停时的 Bootstrap 4 Mega 菜单