5

我正在用 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 中?

4

1 回答 1

4

鼻子包含一个包,该包接受所有基于类的断言,这些断言unittest提供并将它们转换为普通函数,该模块的文档指出

nose.tools 模块提供了 [...] 中assertX找到的所有相同方法unittest.TestCase(仅以PEP 8#function-names方式拼写,assert_equal而不是assertEqual)。

例如:

from nose.tools import assert_equal

@given("foo is 'blah'")
def step_impl(context):
    assert_equal(context.foo, "blah")

您可以将自定义消息添加到断言中,就像.assertX使用unittest.

这就是我使用 Behave 运行的测试套件所使用的。

于 2016-02-09T11:51:47.997 回答