简体   繁体   English

将Behave或Lettuce与Python unittest集成

[英]Integrating Behave or Lettuce with Python unittest

I'm looking at BDD with Python. 我正在用Python看BDD。 Verification of results is a drag, because the results being verified are not printed on failure. 验证结果是一种拖累,因为验证的结果不会在失败时打印。

Compare Behave output: 比较Behave输出:

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

to SpecFlow+NUnit output: 到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"
  --------------------------^

Finding failure causes is way faster with the SpecFlow output. 使用SpecFlow输出查找失败原因的速度更快。 To get the variable contents on error, they have to be put into a string manually. 要在出错时获取变量内容,必须手动将它们放入字符串中。

From the Lettuce tutorial : 来自生菜教程

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

From the Behave tutorial : Behave教程

if text not in context.response:
    fail('%r not in %r' % (text, context.response))

Compare this to Python unittest : 将此与Python unittest进行比较:

self.assertEqual('foo2'.upper(), 'FOO')

resulting in: 导致:

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'

However, the methods from Python unittest cannot be used outside a TestCase instance. 但是,Python unittest中的方法不能在TestCase实例之外使用。

Is there a good way of getting all the niceness of Python unittest integrated into Behave or Lettuce? 是否有一种很好的方法可以将Python单元测试的所有优点集成到Behave或Lettuce中?

nose includes a package that takes all the class-based asserts that unittest provides and turns them into plain functions, the module's documentation states : nose包含一个包,它接受unittest提供的所有基于类的断言并将它们转换为普通函数,该模块的文档说明

The nose.tools module provides [...] all of the same assertX methods found in unittest.TestCase (only spelled in PEP 8#function-names fashion, so assert_equal rather than assertEqual ). nose.tools模块提供了unittest.TestCase所有相同的assertX方法(仅以PEP 8#函数名称方式拼写,因此assert_equal而不是assertEqual )。

For instance: 例如:

from nose.tools import assert_equal

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

You can ad custom messages to assertions just like you would with the .assertX methods of unittest . 您可以像使用unittest.assertX方法一样将自定义消息广告到断言。

That's what I use for the test suites that I run with Behave. 这就是我用于运行Behave的测试套件所使用的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM