0
0
PyTesttesting~10 mins

Why advanced patterns handle real-world complexity in PyTest - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test checks a function that calculates discounts based on complex rules. It verifies that the function correctly applies multiple conditions, showing how advanced patterns help manage real-world complexity.

Test Code - pytest
PyTest
import pytest

def calculate_discount(price, customer_type, is_holiday):
    if price <= 0:
        return 0
    if customer_type == 'VIP' and is_holiday:
        return price * 0.3
    elif customer_type == 'VIP':
        return price * 0.2
    elif is_holiday:
        return price * 0.1
    else:
        return 0


def test_calculate_discount():
    # VIP customer on holiday
    assert calculate_discount(100, 'VIP', True) == 30
    # VIP customer not on holiday
    assert calculate_discount(100, 'VIP', False) == 20
    # Regular customer on holiday
    assert calculate_discount(100, 'Regular', True) == 10
    # Regular customer not on holiday
    assert calculate_discount(100, 'Regular', False) == 0
    # Zero price
    assert calculate_discount(0, 'VIP', True) == 0
    # Negative price
    assert calculate_discount(-50, 'VIP', True) == 0
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test startspytest test runner initialized-PASS
2Calls calculate_discount(100, 'VIP', True)Function evaluates conditions for VIP customer on holidayReturn value equals 30PASS
3Calls calculate_discount(100, 'VIP', False)Function evaluates conditions for VIP customer not on holidayReturn value equals 20PASS
4Calls calculate_discount(100, 'Regular', True)Function evaluates conditions for regular customer on holidayReturn value equals 10PASS
5Calls calculate_discount(100, 'Regular', False)Function evaluates conditions for regular customer not on holidayReturn value equals 0PASS
6Calls calculate_discount(0, 'VIP', True)Function evaluates zero price caseReturn value equals 0PASS
7Calls calculate_discount(-50, 'VIP', True)Function evaluates negative price caseReturn value equals 0PASS
8Test endsAll assertions passed-PASS
Failure Scenario
Failing Condition: Function returns incorrect discount for VIP customer on holiday
Execution Trace Quiz - 3 Questions
Test your understanding
What discount does a VIP customer get on a holiday for a price of 100?
A10
B20
C30
D0
Key Result
Using multiple conditions and test cases helps cover real-world scenarios, ensuring the function behaves correctly under complex rules.