Created
April 7, 2026 19:29
-
-
Save lejonmanen/6b75aa1d29071708eeaed11a5974f859 to your computer and use it in GitHub Desktop.
Python BDD exempel, Gherkin och behave
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Gör en Feature-fil för varje större feature i appen. | |
| # Varje feature testas med ett eller flera scenarion. | |
| # Varje scenario beskrivs med flera steg, som definieras i en separat step-fil. | |
| # | |
| # Även icke-tekniska personer ska kunna läsa och förstå vad dessa testscenarier går ut på. | |
| # | |
| Feature: Enkel kalkylator | |
| Scenario: Addera två tal | |
| Given att jag har talen 3 och 5 | |
| When jag adderar dem | |
| Then ska resultatet vara 8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Inga ändringar i vanliga kodfiler. | |
| def addera(a, b): | |
| return a + b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Här är vår egentliga testkod. | |
| # Man definierar stegen som står i feature-filer. | |
| # Skriv funktionerna så att de kan användas separat. Använd context för att "komma ihåg" värden till nästa steg. | |
| # | |
| from behave import given, when, then | |
| from calculator import addera | |
| @given("att jag har talen {a:d} och {b:d}") | |
| def step_given(context, a, b): | |
| context.a = a | |
| context.b = b | |
| @when("jag adderar dem") | |
| def step_when(context): | |
| context.actual_result = addera(context.a, context.b) | |
| @then("ska resultatet vara {expected:d}") | |
| def step_then(context, expected): | |
| assert context.actual_result == expected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment