3.5. Step - 04 : Add implementation and pass¶
Let’s add steps so that addition is successful..
libmath.py
The actual implementation.
# libmath.py class libMath(object): def __init__(self): self.num1 = 0 self.num2 = 0 self.value = 0 def doStoreNum1Num2(self, num1, num2): self.num1 = num1 self.num2 = num2 def doAdd(self): self.value = self.num1 + self.num2 def getValue(self): return self.value
features/simple-addition.feature
remains as is.# features/sample-addition.feature Feature: Simple Addition Showcase simple addition for the BDD Book. Scenario: Addition of single digit numbers Given I have '1' and '3' When I add them Then The result must be '4' Scenario: Addition of double digit numbers Given I have '70' and '29' When I add them Then The result must be '99'
features/environment.py
Let’s add a hook to create a new libMath object before each scenario.
# environment.py import libmath def before_scenario(context, scenario): """ Let's start with a new clean object before every test. """ context.math = libmath.libMath()
features/steps/steps_addition.py
Calls the actual libMath for the expected behaviour
# features/steps/steps_addition.py from behave import given, when, then @given(u"I have '{num1:d}' and '{num2:d}'") def given_i_have(context, num1, num2): context.math.doStoreNum1Num2(num1, num2) @when(u"I add them") def when_i_add(context): context.math.doAdd() @then(u"The result must be '{value:d}'") def then_the_result_must_be(context, value): actual_value = context.math.getValue() assert value == actual_value, "Expected %d got %d"%(value, actual_value)
With above contents, if we run behave:
behave --no-timings --no-source
We can see that everything is successful:
Feature: Simple Addition
Showcase simple addition for the BDD Book.
Scenario: Addition of single digit numbers
Given I have '1' and '3'
When I add them
Then The result must be '4'
Scenario: Addition of double digit numbers
Given I have '70' and '29'
When I add them
Then The result must be '99'
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.001s
3.6. Changes¶
As compared to previous section Step - 03 : Run with defined steps, the changes in files is:
diff '--unified=3' --new-file --ignore-all-space --text --recursive --show-c-function --report-identical-files old/features/environment.py new/features/environment.py
--- old/features/environment.py 2022-12-11 00:00:00.000000000 +0530
+++ new/features/environment.py 2022-12-11 00:00:00.000000000 +0530
@@ -1,2 +1,7 @@
# environment.py
-# Empty for time being
+
+import libmath
+
+def before_scenario(context, scenario):
+ """ Let's start with a new clean object before every test. """
+ context.math = libmath.libMath()
Files old/features/simple-addition.feature and new/features/simple-addition.feature are identical
diff '--unified=3' --new-file --ignore-all-space --text --recursive --show-c-function --report-identical-files old/features/steps/steps_addition.py new/features/steps/steps_addition.py
--- old/features/steps/steps_addition.py 2022-12-11 00:00:00.000000000 +0530
+++ new/features/steps/steps_addition.py 2022-12-11 00:00:00.000000000 +0530
@@ -4,13 +4,15 @@ from behave import given, when, then
@given(u"I have '{num1:d}' and '{num2:d}'")
def given_i_have(context, num1, num2):
- raise NotImplementedError("STEP: Geven I Have")
+ context.math.doStoreNum1Num2(num1, num2)
+
@when(u"I add them")
def when_i_add(context):
- raise NotImplementedError(u"STEP: When I add them")
+ context.math.doAdd()
@then(u"The result must be '{value:d}'")
def then_the_result_must_be(context, value):
- raise NotImplementedError(u"STEP: Then The result must be")
+ actual_value = context.math.getValue()
+ assert value == actual_value, "Expected %d got %d"%(value, actual_value)
diff '--unified=3' --new-file --ignore-all-space --text --recursive --show-c-function --report-identical-files old/libmath.py new/libmath.py
--- old/libmath.py 2022-12-11 00:00:00.000000000 +0530
+++ new/libmath.py 2022-12-11 00:00:00.000000000 +0530
@@ -1,3 +1,17 @@
# libmath.py
-# Empty for time being
+class libMath(object):
+ def __init__(self):
+ self.num1 = 0
+ self.num2 = 0
+ self.value = 0
+
+ def doStoreNum1Num2(self, num1, num2):
+ self.num1 = num1
+ self.num2 = num2
+
+ def doAdd(self):
+ self.value = self.num1 + self.num2
+
+ def getValue(self):
+ return self.value