Want to write tests that tell a clear story anyone can follow?
Why Given-When-Then pattern in PyTest? - Purpose & Use Cases
Imagine testing a calculator app by clicking buttons and writing down results on paper every time you want to check if addition works.
This manual way is slow and easy to mess up. You might forget steps or mix results, making it hard to trust your testing.
The Given-When-Then pattern helps organize tests clearly: Given some setup, When an action happens, Then expect a result. It makes tests easy to read and follow.
def test_add(): result = add(2, 3) assert result == 5
def test_add(): # Given two numbers a, b = 2, 3 # When they are added result = add(a, b) # Then the result should be 5 assert result == 5
This pattern makes tests clear and simple, so anyone can understand what is tested and why.
When testing a login feature, you can say: Given a user with valid credentials, When they enter them and press login, Then they should see the homepage.
Manual testing is slow and error-prone.
Given-When-Then organizes tests into clear steps.
It improves test readability and trust.