Python 单元测试两个列表,如果有差异,打印/显示
我昨天开始用Python进行单元测试。我在网上看到了使用assertEqual进行测试的例子,assertEqual会自动打印出两个列表之间的差异。然而,在我的剧本中似乎没有发生。我怎样才能得到呢 以下代码包含在从unittest.TestCase派生的类中:Python 单元测试两个列表,如果有差异,打印/显示,python,unit-testing,Python,Unit Testing,我昨天开始用Python进行单元测试。我在网上看到了使用assertEqual进行测试的例子,assertEqual会自动打印出两个列表之间的差异。然而,在我的剧本中似乎没有发生。我怎样才能得到呢 以下代码包含在从unittest.TestCase派生的类中: def test_compare_two_lists_should_fail(self): a = [1,2,3] # In my actual code this is generated by a module's funct
def test_compare_two_lists_should_fail(self):
a = [1,2,3] # In my actual code this is generated by a module's function
b = [1,2,4]
self.assertListEqual(a, b, 'lists are inequal')
def test_compare_two_lists_should_not_fail(self):
a = [1,2,3] # In my actual code this is generated by a module's function
b = [1,2,3]
self.assertListEqual(a ,b, 'lists are inequal')
运行此测试时,将产生以下输出:
测试比较两个列表不应该失败(main.TestCase)。。。好啊
测试\u比较两个\u列表\u应该\u失败(main.TestCase)。。。失败
======================================================================
失败:测试\u比较\u两个\u列表\u应该\u失败(main.TestCase)
回溯(最近一次呼叫最后一次):
文件“C:/some\u dir/TestCase.py”,第34行,在测试比较两个列表中,如果失败
self.AssertListQual(a,b,‘列表不相等’)
AssertionError:列表是非水的
在0.001s内运行了2次测试
失败(failures=1)问题是您在对
AssertListQual
的两次调用中指定的消息
从
Args:
列表1:要比较的第一个列表
列表2:要比较的第二个列表
msg:失败时使用的可选消息,而不是差异列表
因此,如果要查看列表之间的差异,请避免传递消息:
self.assertListEqual(a ,b)
顺便说一句,您在两个测试中都有相同的
列表不相等的消息。问题在于您在对AssertListQual
的两个调用中指定的消息
从
Args:
列表1:要比较的第一个列表
列表2:要比较的第二个列表
msg:失败时使用的可选消息,而不是差异列表
因此,如果要查看列表之间的差异,请避免传递消息:
self.assertListEqual(a ,b)
顺便说一下,在两个测试中都有相同的列表是不相等的消息。如果您从Python中的单元测试开始,我建议您使用pytest
。总之,它比xUnit更易于使用,失败消息也更智能
使用pytest
可以得到如下结果:
def testFoo():
assert [1, 2, 3] == [1, 2, 4], 'lists are inequal'
因此:
================================== FAILURES ===================================
___________________________________ testFoo ___________________________________
def testFoo():
> assert [1, 2, 3] == [1, 2, 4]
E AssertionError: lists are inequal
E assert [1, 2, 3] == [1, 2, 4]
E At index 2 diff: 3 != 4
E Use -v to get the full diff
File "foo.py", line 2
AssertionError
========================== 1 failed in 0.07 seconds ===========================
写得很简单,信息也很明显。试试看 如果您从Python中的单元测试开始,我建议您使用pytest
。总之,它比xUnit更易于使用,失败消息也更智能
使用pytest
可以得到如下结果:
def testFoo():
assert [1, 2, 3] == [1, 2, 4], 'lists are inequal'
因此:
================================== FAILURES ===================================
___________________________________ testFoo ___________________________________
def testFoo():
> assert [1, 2, 3] == [1, 2, 4]
E AssertionError: lists are inequal
E assert [1, 2, 3] == [1, 2, 4]
E At index 2 diff: 3 != 4
E Use -v to get the full diff
File "foo.py", line 2
AssertionError
========================== 1 failed in 0.07 seconds ===========================
写得很简单,信息也很明显。试试看 正如Pynchia所指出的,两个位置都有AssertListQual,这将为您提供一个预期的输出。正如Pynchia所指出的,两个位置都有AssertListQual,这将为您提供一个预期的输出。有时候,它可能很简单,比如省略msg参数。通过深入单元测试忽略了这一点。多谢各位!有时,它可以是如此简单,如省略msg参数。通过深入单元测试忽略了这一点。多谢各位!谢谢你,这可能也是我应该研究的。而且它很有魅力!与此同时,我从WIndows转到了Ubuntu,这使命令行工具的使用变得更加容易:-)再次感谢。谢谢,这可能也是我应该研究的问题。它工作起来很有魅力!与此同时,我从WIndows迁移到Ubuntu,这使得使用命令行工具变得更加容易:-)再次感谢。