0
0
PyTesttesting~10 mins

Test packages in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test package contains two simple test files. It verifies that pytest can discover and run tests across multiple files inside a package.

Test Code - pytest
PyTest
import pytest

# File: tests/test_math.py

def test_addition():
    assert 1 + 1 == 2

def test_subtraction():
    assert 5 - 3 == 2

# File: tests/test_strings.py

def test_uppercase():
    assert 'hello'.upper() == 'HELLO'

def test_isdigit():
    assert '1234'.isdigit() == True

# To run tests, execute: pytest tests/
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test runner starts and looks for tests in the 'tests' packagePytest scans the 'tests' directory and finds test_math.py and test_strings.py-PASS
2Pytest loads test_math.py and runs test_additionRunning test_addition functionAssert 1 + 1 == 2PASS
3Pytest runs test_subtraction in test_math.pyRunning test_subtraction functionAssert 5 - 3 == 2PASS
4Pytest loads test_strings.py and runs test_uppercaseRunning test_uppercase functionAssert 'hello'.upper() == 'HELLO'PASS
5Pytest runs test_isdigit in test_strings.pyRunning test_isdigit functionAssert '1234'.isdigit() == TruePASS
6Pytest finishes running all tests and reports resultsAll 4 tests passed-PASS
Failure Scenario
Failing Condition: A test assertion fails, for example if test_addition asserts 1 + 1 == 3
Execution Trace Quiz - 3 Questions
Test your understanding
What does pytest do first when running tests in a package?
AIt compiles the test files into bytecode
BIt runs all tests without scanning
CIt scans the package directory to find test files
DIt deletes old test results
Key Result
Organizing tests into packages with multiple files helps pytest discover and run tests efficiently, keeping tests modular and easy to maintain.