BDD (Behavior Driven Development)

Musical Intro

Introduction

A way to view BDD is to see it in 3 parts:

  1. Describing systems, by describing their behavior.
  2. Describing behavior with given, when, and then steps.
  3. Automating the description with a glue framework.

This lesson will focus on the first two steps. If you want the third, then I can recommend my coding course.

In BDD we use words such as “Given”, “When”, and “Then” to describe a process/behavior.

It’s like building with Lego bricks:

Given you have these blocks (don't start without them).

When specific set of blocks are combined in a specific way.
Then a specific structure must be achieved.

When more specific blocks are added in a specific way.
Then another specific step in the structure must be achieved.

...

When the final blocks are added in a specific way.
Then the final structure must be achieved.

This approach had worked very well for Lego and Ikea to describe the processes of assembling things (an excellent manual).

BDD can also be used to describe your product and used as an manual.
This is called live-documentation, because the documentation is always up-to-date, otherwise the BDD test cases won’t pass.


Using AI

All this have been created with the cooperation with AI.

Often it can be more useful just to learn, what is needed in the moment, than to learn everything about a topic. This is where AI is perfect.

We can use the free ChatGPT 3.5 to learn about operators. You are welcome to try other and if you do, please write a comment!

Lets look at essential prompts!

Learn from examples (prompt):

I want to learn about writing a test case as a BDD scenario (given, when and then).
Can you show me an example on how to write such a scenario on "the process of making a sandwich?'

Given must be the preconditions, before it can start.
When must be the needed actions.
Then must be the "must" verifications.

Don't use "I", but a proper user.
ChatGPT 3.5 (free) πŸ€–:

Get more examples, by varying the subject:

I want to learn about writing a test case as a BDD scenario (given, when and then).
Can you show me an example on how to write such a scenario on "the process of changing a bike wheel"?

Given must be the preconditions, before it can start.
When must be the needed actions.
Then must be the "must" verifications.

Don't use "I", but a proper user.
ChatGPT 3.5 (free) πŸ€–:

Sometimes you might need to start a new session.

Sometimes the AI makes a mistake like writing Given, Then, When (wrong order) and sometimes starting a new session makes it go away.

Improving a scenario (prompt):

I have written the following scenario, can analyze it on how it can be improved?
--
Scenario: Changing a Bike Wheel
Given a cyclist has a spare tire and necessary tools
And the bike has a flat tire
When the cyclist removes the wheel with the flat tire
And replaces it with the spare tire
And properly secures the new wheel in place
Then the cyclist should have a bike with a functional wheel
And the bike should be ready to ride
And the flat tire should be safely stored for repair or disposal
--
ChatGPT 3.5 (free) πŸ€–:

Implement improvements and ask again or let the AI help you (prompt):

(not all it suggests are good improvements, so pick what you agree with)

Can you implement your improvements?
ChatGPT 3.5 (free) πŸ€–:

Split a scenario into multiple more detailed ones (prompt):

can we split the following scenario into more BDD scenarios (given when then)?
--
Scenario: Changing a Bike Wheel
Given a cyclist has a spare tire, a wrench, and tire levers
And the spare tire is in good condition and properly inflated
And the bike has a flat tire
When the cyclist uses the wrench to loosen the bolts securing the wheel with the flat tire
And uses tire levers to pry the tire off the rim
And removes the wheel from the bike
And places the flat tire in a secure location for repair or disposal
And positions the spare tire onto the wheel rim
And inflates the spare tire to the recommended pressure
And tightens the bolts to secure the new wheel in place
Then the cyclist should have a bike with a functional wheel
And the tire pressure of the new wheel should be checked and adjusted if necessary
And the bike should be ready to ride
And the flat tire should be safely stored for repair or disposal
--
ChatGPT 3.5 (free) πŸ€–:

Adding a examples, to make it data driven (prompt):

I have written the following scenario, can you add "Examples" , to make it data driven?
Please use these example variables in the steps.
--
Scenario: Changing a Bike Wheel
Given a cyclist has a spare tire and necessary tools
And the bike has a flat tire
When the cyclist removes the wheel with the flat tire
And replaces it with the spare tire
And properly secures the new wheel in place
Then the cyclist should have a bike with a functional wheel
And the bike should be ready to ride
And the flat tire should be safely stored for repair or disposal
--
ChatGPT 3.5 (free) πŸ€–:
tools flat_tire disposal_or_repair
wrench, leverspuncturedrepair
pump, leversworndisposal
wrench, pumpdeflatedrepair

The AI is not yet good enough to make advanced scenarios (prompt):

When humans learn about scenarios (faster with AI)
And improve their written scenarios on the internet
Then AI will learn from them
and get better at teaching us humans!

I have written the following scenario, can you add "Examples" , to make it data driven?
In one example it can be fixed, while in the other the spare tire is missing and third the tools are missing.
--
Scenario: Changing a Bike Wheel
Given a cyclist has a spare tire and necessary tools
And the bike has a flat tire
When the cyclist removes the wheel with the flat tire
And replaces it with the spare tire
And properly secures the new wheel in place
Then the cyclist should have a bike with a functional wheel
And the bike should be ready to ride
And the flat tire should be safely stored for repair or disposal
--
A human tester πŸ§‘:
tools action_with_spare_tireready_state
wrench, leversuses the spare tire to replaceready
wrenchmissing the levers, and can’t replacenot ready
leversmissing the wrench, and can’t replacenot ready
wrench, leversmissing the spare tire, and can’t replacenot ready

Conclusion

From this lesson, we’ve learned several key aspects of using the Behavior-Driven Development (BDD) methodology effectively through structured lessons and examples:

  1. What is BDD: A way to document a process/behavior of a system with “Given,” “When,” and “Then”, and it has worked well for Lego and Ikea. This is called live-documentation, because it is always up-to-date, as long as the BDD test cases pass.
  2. Learn from examples: We explored how BDD scenarios are structured into “Given,” “When,” and “Then” to articulate test cases clearly. This format helps define the prerequisites, actions, and expected outcomes clearly.
  3. Get more examples, by varying the subject: We saw how applying BDD to different contexts, such as making a sandwich or changing a bike wheel, helps in understanding the versatile application of BDD in various real-world scenarios.
  4. Sometimes you might need to start a new session: This point underlines the iterative nature of developing BDD scenarios, where revising and refining through multiple iterations can lead to clearer and more effective test cases.
  5. Implement improvements and ask again or let the AI help you: We discussed refining BDD scenarios by incorporating feedback to enhance clarity and detail, demonstrating the iterative improvement and the role of tools like AI in assisting these refinements.
  6. Split a scenario into multiple more detailed ones: Breaking down complex scenarios into simpler, focused parts was shown as an effective strategy to manage detailed testing processes, making each step more manageable and comprehensible.
  7. Adding examples, to make it data driven: We learned how to make BDD scenarios data-driven by adding “Examples” tables, which specify multiple sets of conditions and outcomes, enabling comprehensive testing across various scenarios.
  8. The AI is not yet good enough to make advanced scenarios: This reflects on the current limitations of AI in generating complex BDD scenarios and emphasizes the need for human oversight in creating sophisticated test cases and interpreting nuanced scenarios.

Together, these lessons underscore the importance of a systematic approach in BDD to ensure thorough understanding and effective testing of user behaviors and system functionalities.

Limitless learning with a 24/7 tutor!!!

Congratulations – Lesson complete!

Want more?

Check out the comments on Linked.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit exceeded. Please complete the captcha once again.