我有一个像
这样的完全的测试用例def test_material_search_name(self):
"""
Tests for `LIKE` condition in searches.
For both name and serial number.
"""
material_one = MaterialFactory(name="Eraenys Velinarys", serial_number="SB2341")
material_two = MaterialFactory(name="Nelaerla Velnaris", serial_number="TB7892")
response = self.client.get(reverse('material-search'), {'q': 'vel'})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], 2)
self.assertEqual(response.data['results'][0]['name'], material_one.name)
self.assertEqual(response.data['results'][1]['name'], material_two.name)
我的错误信息是:
line 97, in test_material_search_name
self.assertEqual(response.data['results'][0]['name'], material_one.name)
AssertionError: 'Nelaerla Velnaris' != 'Eraenys Velinarys'
- Nelaerla Velnaris
+ Eraenys Velinarys
然后当我重新运行而不更改任何代码时,它就会成功。
偶尔会发生此错误。
我想知道是否有更好的方法来实现测试用例的目标,而不会偶尔出现这种奇怪的故障。
发生此错误的频率在我运行测试时每50次发生一次。
我使用的典型测试命令:
python manage.py test app_name.tests --keepdb
答案 0 :(得分:1)
以下是一些选项:
assertEqual
self.assertIn(name, names)