编程语言
首页 > 编程语言> > 将行为或生菜与Python unittest集成

将行为或生菜与Python unittest集成

作者:互联网

我正在用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输出可以更快地找到故障原因.要获得错误的变量内容,必须将它们手动放入字符串中.

Lettuce tutorial开始:

assert world.number == expected, \
    "Got %d" % world.number

Behave tutorial

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单元测试的所有优点集成到Behave或Lettuce中?

解决方法:

nose包含一个程序包,该程序包接受unittest提供的所有基于类的断言并将其转换为简单函数,即模块的documentation states

The nose.tools module provides […] all of the same assertX methods found in unittest.TestCase (only spelled in 07002 fashion, so assert_equal rather than assertEqual).

例如:

from nose.tools import assert_equal

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

您可以像使用unittest的.assertX方法一样,将自定义消息添加到断言中.

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

标签:python-behave,lettuce,python
来源: https://codeday.me/bug/20191119/2033215.html