Fra*_*ers 5 python lettuce python-behave
我正在用 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
Run Code Online (Sandbox Code Playgroud)
到 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"
--------------------------^
Run Code Online (Sandbox Code Playgroud)
使用 SpecFlow 输出可以更快地找到失败原因。要获取出错的变量内容,必须手动将它们放入字符串中。
来自生菜教程:
assert world.number == expected, \
"Got %d" % world.number
Run Code Online (Sandbox Code Playgroud)
从行为教程:
if text not in context.response:
fail('%r not in %r' % (text, context.response))
Run Code Online (Sandbox Code Playgroud)
将此与Python unittest进行比较:
self.assertEqual('foo2'.upper(), 'FOO')
Run Code Online (Sandbox Code Playgroud)
导致:
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'
Run Code Online (Sandbox Code Playgroud)
但是,Python unittest 中的方法不能在TestCase
实例之外使用。
有没有一种好方法可以将 Python unittest 的所有优点集成到 Behave 或 Lettuce 中?
鼻子包括一个包,它接受所有基于类的断言,unittest
提供并将它们转换为普通函数,该模块的文档指出:
ose.tools 模块提供了 [...] 中找到的所有相同
assertX
方法(仅以PEP 8#function-namesunittest.TestCase
方式拼写,因此而不是)。assert_equal
assertEqual
例如:
from nose.tools import assert_equal
@given("foo is 'blah'")
def step_impl(context):
assert_equal(context.foo, "blah")
Run Code Online (Sandbox Code Playgroud)
您可以将自定义消息添加到断言中,就像.assertX
使用unittest
.
这就是我使用 Behave 运行的测试套件所使用的。