Feature: Fibonacci Number Calculation
Scenario: Calculate Fibonacci Number
Given I have a number n
When I call the Fibonacci function with n
Then the function returns the correct Fibonacci number
tests/features/fibonacci_number_calculation.feature
# -*- coding: utf-8 -*-
Feature: Fibonacci Number Calculation
@behave.given('I have a number n')
def step_1(context):
context.n = int(context.text)
@behave.when('I call the Fibonacci function with n')
def step_2(context):
context.result = fibonacci(context.n)
@behave.then('the function returns the correct Fibonacci number')
def step_3(context):
assert fibonacci(context.n) == context.result
tests/features/fibonacci_number_calculation.py
import unittest
from behave import given, when, then
@given('I have a number n')
def step1(context):
context.n = int(context.text)
@when('I call the Fibonacci function with n')
def step2(context):
context.result = fibonacci(context.n)
@then('the function returns the correct Fibonacci number')
def step3(context):
assert fibonacci(context.n) == context.result
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
tests/fibonacci_number_calculation_test.py
import unittest
from behave import given, when, then
class TestFibonacciNumberCalculation(unittest.TestCase):
def setUp(self):
self.use_behave_standard_environment()
@given('I have a number n')
def step1(context):
context.n = int(context.text)
@when('I call the Fibonacci function with n')
def step2(context):
context.result = fibonacci(context.n)
@then('the function returns the correct Fibonacci number')
def step3(context):
assert fibonacci(context.n) == context.result
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
if __name__ == '__main__':
unittest.main()
script/run_tests.sh
#!/bin/bash
python tests/fibonacci_number_calculation_test.py
This code generates a feature file with a single scenario, a test code file, and a test script. The feature file describes the behavior we want the Fibonacci function to have, the test code file contains the Gherkin steps that will be converted to test cases, and the test script runs the Behave test cases.