Learn about BDD (Behavior Driven Development) with AI

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.
πŸ₯’ Gherkin

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.
πŸ“ AI prompt
Absolutely, here's a BDD scenario for making a sandwich without using "I":

Boundary Value Analysis (BVA) is a testing technique used to identify errors at the boundaries of input domains rather than within the ranges. The idea is to focus on the edges of the input ranges where errors are most likely to occur. This is particularly useful in situations where values are input as a range or limit.

To demonstrate Boundary Value Analysis using Behavior-Driven Development (BDD), let's take an example of a banking system that has the following constraints for account balance

Scenario: Making a Sandwich
  Given a user has slices of bread available
    And there is a selection of fillings
  When the user selects two slices of bread
    And chooses a filling from the selection
    And places the filling between the slices of bread
  Then the user should have a sandwich
    And the sandwich should contain the chosen filling
    And the sandwich should be ready to eat

In this scenario:
- Given: Describes the initial conditions or preconditions required for the scenario to start. In this case, the availability of bread slices and a selection of fillings.
- When: Describes the actions or events that occur. Here, it's a user selecting two slices of bread, choosing a filling, and assembling the sandwich.
- Then: Describes the expected outcomes or verifications that should be true after the actions have been performed. In this case, verifying that a sandwich exists, it contains the chosen filling, and it's ready to eat.
πŸ€– AI response

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.
πŸ“ AI prompt
Certainly, here's a BDD scenario for changing a bike wheel:

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


In this scenario:
- Given: Describes the initial conditions or preconditions required for the scenario to start. In this case, the cyclist having a spare tire and necessary tools, and the bike having a flat tire.
- When: Describes the actions or events that occur. Here, it's the cyclist removing the flat tire, replacing it with a spare tire, and properly securing the new wheel.
- Then: Describes the expected outcomes or verifications that should be true after the actions have been performed. In this case, verifying that the bike has a functional wheel, is ready to ride, and that the flat tire is appropriately handled.
πŸ€– AI response

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
--
πŸ“ AI prompt
Your scenario is well-structured and covers the main steps involved in changing a bike wheel. However, here are some suggestions for improvement:

1. Clarify Preconditions: While you mention that the cyclist has a spare tire and necessary tools, it might be helpful to specify what tools are needed. For example, a wrench or tire lever. Additionally, you could mention the condition of the spare tire - is it in good condition and properly inflated?

2. Add Clarity to Actions: The steps under the "When" section are clear, but you could add more detail to make them more explicit. For example, specify how the cyclist removes the wheel (e.g., using a wrench), and how they secure the new wheel in place (e.g., tightening the bolts to the recommended torque).

3. Check for Completion: The "Then" section mentions that the cyclist should have a bike with a functional wheel and that the bike should be ready to ride. Consider adding verification steps to ensure these conditions are met. For example, checking that the tire pressure is correct and that the wheel spins freely without rubbing against the frame or brakes.

4. Address Safety Concerns: While you mention that the flat tire should be safely stored for repair or disposal, you could provide more guidance on how to do this. For example, emphasizing the importance of properly disposing of old tires to minimize environmental impact.

5. Consider Error Handling: It might be beneficial to include steps for handling potential errors or complications that could arise during the wheel-changing process. For example, what should the cyclist do if they encounter difficulties removing the wheel or if the spare tire is also damaged?

By addressing these points, you can provide a more comprehensive and detailed scenario for changing a bike wheel, ensuring that users have clear guidance on each step of the process and any associated requirements or considerations.
πŸ€– AI response

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?
Certainly! Here's the improved version of the scenario:

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
πŸ€– AI response

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
--
πŸ“ AI prompt
Sure, we can break down the scenario into more specific BDD scenarios. Here are some suggestions:

1. Preparing for Wheel Change:

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 gathers all necessary tools and materials
Then the cyclist is ready to change the bike wheel


2. Removing the Flat Tire:

Given the cyclist has gathered all necessary tools and materials
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
Then the flat tire is detached from the bike


3. Preparing the Spare Tire:

Given the cyclist has gathered all necessary tools and materials
And the flat tire has been removed
When the cyclist prepares the spare tire for installation
Then the spare tire is ready to be mounted onto the wheel rim
Mounting the Spare Tire:

Given the cyclist has prepared the spare tire for installation
And the flat tire has been removed
When the cyclist positions the spare tire onto the wheel rim
And inflates the spare tire to the recommended pressure
Then the spare tire is properly mounted and inflated
Securing the New Wheel:

Given the spare tire has been properly mounted and inflated
When the cyclist tightens the bolts to secure the new wheel in place
Then the new wheel is securely attached to the bike frame
Final Checks and Cleanup:

Given the new wheel is securely attached to the bike frame
When the cyclist checks the tire pressure of the new wheel
And adjusts the tire pressure if necessary
And ensures the bike is ready to ride
Then the bike is ready for use with the functional wheel
And the flat tire is safely stored for repair or disposal
Breaking down the scenario into smaller, more focused steps like these can help clarify the behavior expected at each stage of the process.
πŸ€– AI response

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
--
πŸ“ AI prompt
Certainly! Here's how you can add an "Examples" section to make the scenario data-driven using the given variables:

Scenario: Changing a Bike Wheel
  Given a cyclist has the 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 <disposal_or_repair>
    
  Examples
    tools	         | flat_tire | disposal_or_repair
    wrench, levers | punctured | repair
    pump, levers   | worn      | disposal
    wrench, pump   | deflated  | repair
πŸ€– AI response

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!

Scenario: Changing a Bike Wheel
  Given a cyclist has a flat tire and <tools>
    And the bike has a puncture
  When the cyclist removes the wheel with the flat tire
    And <action_with_spare_tire> the punctured tire
  Then the bike should be <ready_state> to ride
  
  Examples
    tools	         | action_with_spare_tire                    | ready_state
    wrench, levers | uses the spare tire to replace            | ready
    wrench         | missing the levers, and can't replace     | not ready
    levers         | missing the wrench, and can't replace     | not ready
    wrench, levers | missing the spare tire, and can't replace | not ready
πŸ§‘πŸ€– Human + AI

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.