Testing Python Dengan Behave
Untuk pemasangan sangat mudah, kita bisa gunakan pip saja
pip install behave
Lalu kita buat struktur folder seperti berikut, dan pastikan terdapat folder “features” dan folder “steps” di dalamnya,
├── features │ ├── first.feature │ └── steps │ └── first.py ├── __init__.py ├── simplecalc.py
Feature: Simple calculator Scenario: Run simple addition Given we have simple calculator When we add 2 and 2 Then we got 4
from behave import given, when, then from simplecalc import Simplecalc @given("we have simple calculator") def load_calc(context): context.simple = Simplecalc() @when("we add 2 and 2") def run_addition(context): context.simple.addition(2,2) @then("we got 4") def get_result(context): assert context.simple.get_result() == 4
Feature: Simple calculator # features/first.feature:1 Scenario: Run simple addition # features/first.feature:3 Given we have simple calculator # features/steps/first.py:4 0.000s When we add 2 and 2 # features/steps/first.py:8 0.000s Then we got 4 # features/steps/first.py:12 0.000s 1 feature passed, 0 failed, 0 skipped 1 scenario passed, 0 failed, 0 skipped 3 steps passed, 0 f
Scenario Outline
Feature: Simple calculator Scenario Outline: Run simple addition Given we have simple calculator with dynamic value When we add <first> and <second> Then we got <result> Examples: Run simple example |first|second|result| |10|2|12|
from behave import given, when, then from simplecalc import Simplecalc @given("we have simple calculator with dynamic value") def load_calc(context): context.simple = Simplecalc() @when("we add {first} and {second}") def run_addition(context, first, second): context.simple.addition(first,second) @then("we got {result}") def get_result(context, result): assert context.simple.get_result() == result
Feature: Simple calculator # features/second.feature:1 Scenario Outline: Run simple addition -- @1.1 Run simple example # features/second.feature:10 Given we have simple calculator with dynamic value # features/steps/second.py:4 0.000s When we add 10 and 2 # features/steps/second.py:8 0.000s Then we got 12 # features/steps/second.py:12 0.000s Traceback (most recent call last): File "/~Code/playground/trybehave/venvbheave/lib/python3.7/site-packages/behave/model.py", line 1329, in run match.run(runner.context) File "/~Code/playground/trybehave/venvbheave/lib/python3.7/site-packages/behave/matchers.py", line 98, in run self.func(context, *args, **kwargs) File "features/steps/second.py", line 14, in get_result assert context.simple.get_result() == result AssertionError
Install dulu
pip install PyHamcrest
from behave import given, when, then from simplecalc import Simplecalc from hamcrest import assert_that, equal_to @given("we have simple calculator with dynamic value") def load_calc(context): context.simple = Simplecalc() @when("we add {first} and {second}") def run_addition(context, first, second): context.simple.addition(first,second) @then("we got {result}") def get_result(context, result): assert_that(context.simple.get_result() , equal_to(result) )
Lalu jalankan perintah “behave -i second” lagi, hasilnya
Scenario Outline: Run simple addition -- @1.1 Run simple example # features/second.feature:10 Given we have simple calculator with dynamic value # features/steps/second.py:5 0.000s When we add 10 and 2 # features/steps/second.py:9 0.000s Then we got 12 # features/steps/second.py:13 0.000s Assertion Failed: Expected: '12' but: was '102'
@when("we add {first:d} and {second:d}") def run_addition(context, first, second): context.simple.addition(first,second) @then("we got {result:d}") def get_result(context, result): assert_that(context.simple.get_result() , equal_to(result) )
Jalankan lagi “behave -i second” dan hasilnya
Feature: Simple calculator # features/second.feature:1 Scenario Outline: Run simple addition -- @1.1 Run simple example # features/second.feature:10 Given we have simple calculator with dynamic value # features/steps/second.py:5 0.000s When we add 10 and 2 # features/steps/second.py:9 0.000s Then we got 12 # features/steps/second.py:13 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.000s
Kalau ingin menambahkan contoh lainnya, ubah second.features menjadi
Feature: Simple calculator Scenario Outline: Run simple addition Given we have simple calculator with dynamic value When we add <first> and <second> Then we got <result> Examples: Run simple example |first|second|result| |10|2|12| |5|2|7| |100|900|1000|
Tes lagi dan hasilnya
Feature: Simple calculator # features/second.feature:1 Scenario Outline: Run simple addition -- @1.1 Run simple example # features/second.feature:10 Given we have simple calculator with dynamic value # features/steps/second.py:5 0.000s When we add 10 and 2 # features/steps/second.py:9 0.000s Then we got 12 # features/steps/second.py:13 0.000s Scenario Outline: Run simple addition -- @1.2 Run simple example # features/second.feature:11 Given we have simple calculator with dynamic value # features/steps/second.py:5 0.000s When we add 5 and 2 # features/steps/second.py:9 0.000s Then we got 7 # features/steps/second.py:13 0.000s Scenario Outline: Run simple addition -- @1.3 Run simple example # features/second.feature:12 Given we have simple calculator with dynamic value # features/steps/second.py:5 0.000s When we add 100 and 900 # features/steps/second.py:9 0.000s Then we got 1000 # features/steps/second.py:13 0.000s 1 feature passed, 0 failed, 0 skipped 3 scenarios passed, 0 failed, 0 skipped 9 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m0.001s