Cucumber + pythonでユーザ・テストケースと駆動スクリプトをできるライブラリを探してみましたがbehaveを発見してここに整理しておきます。
設置は
pip install behave
で終わります。
使用法はcucumberに似てます。まずディレクトリーは以下のようになるべきです。
└── features
└── steps
*.featureファイルはfeaturesフォルダーに位置されて実行pythonファイルはstepsフォルダーに位置されます。
まず*.featureファイルから作成。
ブレンダーを対象で例えてみましょう1
# filename : behaveTest.feature
Feature : Testing Blender
Scenario : put apple into blender
Given I put "apples" in a blender
When I switch the blender on
Then It should transform into "apple juice"
シナリオはGiven-When-Thenの方で作成されてます。behaveはGherkin文法を従います。
テストするブレンダーモデルは以下のようです。
# filename : blender.py
class Blender:
TRANSFORMATION_MAP = {
"Red Tree Frog": "mush",
"apples": "apple juice",
"iPhone": "toxic waste",
"Galaxy Nexus": "toxic waste",
}
def __init__(self):
self.thing = None
self.result = None
def add(self, thing):
self.thing = thing
def switch_on(self):
self.result = self.TRANSFORMATION_MAP.get(self.thing, "DIRT")
TRANSFORMATION_MAPはこのブレンダーの機能を定義しています。appleを入ればapple juice、iPhoneを入ればtoxic wasteが出ると定義されています。
featureと合わせて実行されるテスト実行コードは以下のようです。
# filename : tutorial.py
from behave import given, when, then
from hamcrest import assert_that, equal_to
from blender import Blender
@given('I put "{things}" in a blender')
def step_given_things_into_blender(context, things):
context.blender = Blender()
context.blender.add(things)
@when('I switch the blender on')
def step_when_switch_blender_on(context):
context.blender.switch_on()
@then('it should be transfer into "{other_thing}"')
def sten_then_should_transfer_into(context,other_thing):
assert_that(context.blender.result, equal_to(other_thing))
ここにはPyhamcrestを用いてassert_thatを使用しました。
@given段階ではBlenderオブジェクトを読んで伝達された「things」をadd()します。
@when段階ではBlenderオブジェクトのswitch_on()を実行します。
@then段階ではBlenderオブジェクトのresult値と伝達された「other_thing」と比較してpass, failを判定します。
テストを遂行すれば1シナリオがpassedになります。
$ behave behaveTest.feature
Feature: Step Parameters (tutorial.py) # behaveTest.feature:1
Scenario: Apple # behaveTest.feature:2
Given I put "apples" in a blender # steps/tutorial.py:5 0.000s
When I switch the blender on # steps/tutorial.py:11 0.000s
Then it should be transfer into "apple juice" # steps/tutorial.py:15 0.000s
1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.001s
シナリオをもう一個を加えてみましょう。iPhoneを入れた場合です。
# filename : behaveTest.feature
Feature: Step Parameters (tutorial.py)
Scenario: Apple
Given I put "apples" in a blender
When I switch the blender on
Then it should be transfer into "apple juice"
Scenario: iPhone
Given I put "iPhone" in a blender
When I switch the blender on
Then it should be transfer into "toxic waste"
テスト遂行もう一回
$ behave behaveTest.feature
Feature: Step Parameters (tutorial.py) # behaveTest.feature:1
Scenario: Apple # behaveTest.feature:2
Given I put "apples" in a blender # steps/tutorial.py:5 0.000s
When I switch the blender on # steps/tutorial.py:11 0.000s
Then it should be transfer into "apple juice" # steps/tutorial.py:15 0.000s
Scenario: iPhone # behaveTest.feature:8
Given I put "iPhone" in a blender # steps/tutorial.py:5 0.000s
When I switch the blender on # steps/tutorial.py:11 0.000s
Then it should be transfer into "toxic waste" # steps/tutorial.py:15 0.001s
1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.002s
二つのシナリオがpassedになりました。
QA業務はだんだん自動化に移す傾向です。テスト・ケースをexcelとかテキストで作成するのは今は第三者テスターさんの仕事になっています。この投稿を見ている皆さんは第三者テスターさんじゃないのでこんなbehave見たいなツールを用いてテスト・ケースを整理し自動化まで一気にカーバーするのも良いかと思います。