簡體   English   中英

將Behave或Lettuce與Python unittest集成

[英]Integrating Behave or Lettuce with Python unittest

我正在用Python看BDD。 驗證結果是一種拖累,因為驗證的結果不會在失敗時打印。

比較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

到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

Behave教程

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提供的所有基於類的斷言並將它們轉換為普通函數,該模塊的文檔說明

nose.tools模塊提供了unittest.TestCase所有相同的assertX方法(僅以PEP 8#函數名稱方式拼寫,因此assert_equal而不是assertEqual )。

例如:

from nose.tools import assert_equal

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

您可以像使用unittest.assertX方法一樣將自定義消息廣告到斷言。

這就是我用於運行Behave的測試套件所使用的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM