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

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インスタンスの外では使用できません。

Behave または Lettuce に統合された Python unittest の優れた点をすべて取得する良い方法はありますか?

4

1 に答える 1

4

ノーズには、提供するすべてのクラスベースのアサートを取り、unittestそれらをプレーンな関数に変換するパッケージが含まれています。モジュールのドキュメントには次のように記載されています。

noise.tools モジュールは [...] にある同じメソッドをassertXすべて提供しますunittest.TestCase( .assert_equalassertEqual

例えば:

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 に答える