我正在用 Python 研究 BDD。结果的验证是一种拖累,因为正在验证的结果不会在失败时打印。
比较行为输出:
AssertionError:
File "C:\Python27\lib\site-packages\behave\model.py", line 1456, in run
match.run(runner.context)
File "C:\Python27\lib\site-packages\behave\model.py", line 1903, in run
self.func(context, *args, **kwargs)
File "steps\EcuProperties.py", line 28, in step_impl
assert vin == context.driver.find_element_by_xpath("//table[@id='infoTable']/tbody/tr[4]/td[2]").text
到 SpecFlow+NUnit 输出:
Scenario: Verify VIN in Retrieve ECU properties -> Failed on thread #0
[ERROR] String lengths are both 16. Strings differ at index 15.
Expected: "ABCDEFGH12345679"
But was: "ABCDEFGH12345678"
--------------------------^
使用 SpecFlow 输出查找故障原因要快得多。要获取错误的变量内容,必须手动将它们放入字符串中。
从生菜教程:
assert world.number == expected, \
"Got %d" % world.number
从行为教程:
if text not in context.response:
fail('%r not in %r' % (text, context.response))
将此与Python unittest进行比较:
self.assertEqual('foo2'.upper(), 'FOO')
导致:
Failure
Expected :'FOO2'
Actual :'FOO'
<Click to see difference>
Traceback (most recent call last):
File "test.py", line 6, in test_upper
self.assertEqual('foo2'.upper(), 'FOO')
AssertionError: 'FOO2' != 'FOO'
但是,Python unittest 中的方法不能在TestCase
实例外部使用。
有没有一种好方法可以将 Python unittest 的所有优点集成到 Behave 或 Lettuce 中?