Skip to content

Defining Requirements: Functions & Assertions 📝✅

In Python, you write functions. A good function has a clear Input and a clear Output. In Agile, a Requirement (called a User Story) is just a function definition for a feature.

The Function Definition (User Story) 📖

We use a simple sentence template to define what we want.

The Template

As a [User Role], I want [Action] so that [Value].

This looks a lot like a function docstring!

python
def feature(user_role):
    """
    As a <user_role>, 
    I want to <action>,
    So that <value>.
    """
    pass

Example: The "Weather App"

TypeRequirementCritique
Bad"Make a button that shows weather."Too vague! Which weather? Current? Forecast?
Good"As a Traveler, I want to see the rain forecast so that I know if I need an umbrella."Clear User, Action, and Value.

The 3 C's of User Stories

A User Story isn't just a sentence. It has three parts:

  1. Card: The written text (e.g., on a sticky note or Jira ticket).
  2. Conversation: The discussion between you (the coder) and the client to clarify details.
  3. Confirmation: The test cases (Acceptance Criteria) to prove it works.

Example: The "Weather App"

INVEST in Good Stories

A good User Story follows the INVEST checklist:

  • I - Independent: Can you code this without waiting for other features?
  • N - Negotiable: Can we change the implementation details?
  • V - Valuable: Does it help the user?
  • E - Estimable: Can we guess how hard it is?
  • S - Small: Can we finish it in a few days?
  • T - Testable: Can we write a pytest case for it?

The Assertions (Acceptance Criteria) ✅

How do you know if your code works? You run tests. Acceptance Criteria are just the test cases for your story. They tell you when you are "Done".

The "Given-When-Then" Formula 🥒

This is exactly like writing a test function:

  • Given (Setup): The starting state.
  • When (Action): What the user does.
  • Then (Assert): What should happen.

Scenario: Login Feature

User Story: As a User, I want to log in.

Acceptance Criteria 1: Success

  • Given: I am on the login page.
  • When: I enter correct username and password.
  • Then: I am redirected to the Home page.

Acceptance Criteria 2: Failure

  • Given: I am on the login page.
  • When: I enter the WRONG password.
  • Then: I see an error message "Invalid Password".

Task: Write Your Own ✍️

Task

Imagine you are building a To-Do List app in Python. Write a User Story and Acceptance Criteria for "Marking a task as complete".

Possible Answer

Story:

As a Student, I want to mark a task as complete so that I can track my progress.

Criteria:

  • Given: I have a task "Study Python" in my list.
  • When: I type complete "Study Python" in the app.
  • Then: The task should have a "[x]" symbol next to it.