Skip to content

Latest commit

 

History

History
142 lines (104 loc) · 4.75 KB

README.md

File metadata and controls

142 lines (104 loc) · 4.75 KB

Python Kickstart Testing System

This directory contains automated tests for all exercises in the Python Kickstart course. The testing system provides instant feedback to students and ensures their solutions meet the requirements.

🎯 Purpose

The testing system implements the "Instant Feedback" principle from our development guidelines by:

  • Automatically checking student code against expected outputs
  • Providing clear error messages when solutions are incorrect
  • Validating both functionality and code structure requirements

📁 Structure

tests/
├── README.md                 # This file
├── test_01_intro.py         # Tests for Chapter 1 exercises
├── test_02_sequence.py      # Tests for Chapter 2 exercises
└── test_03_variables.py     # Tests for Chapter 3 exercises

🚀 Running Tests

For Students

Students can test their individual exercises:

# Test a specific exercise
pytest tests/test_01_intro.py::TestExercise01_01::test_hello_world_output -v

# Test all exercises in a chapter
pytest tests/test_01_intro.py -v

# Test all exercises
pytest tests/ -v

For Developers

Validate that tests work correctly against known solutions:

# Run the solution validation script
python test_solutions.py

📋 Test Coverage

Chapter 1: Introduction to Python

  • Exercise 1.1: Hello World - Checks exact output match
  • Exercise 1.2: Personal Greeting - Validates name and welcome message structure
  • Exercise 1.3: Comments and Code - Ensures output exists and comments are present

Chapter 2: Program Sequence

  • Exercise 2.1: Morning Routine - Validates sequence of steps and keywords
  • Exercise 2.2: Recipe Steps - Checks step count and comment structure
  • Exercise 2.3: Story Sequence - Validates story length and narrative structure

Chapter 3: Variables and Data Types

  • Exercise 3.1: Personal Info - Checks variable usage and output format
  • Exercise 3.2: Rectangle Calculator - Validates calculations and mathematical operations
  • Exercise 3.3: Variable Updates - Ensures progression and update operations

🔧 Test Design Principles

1. Output Validation

Tests primarily check the output (stdout) of student programs to ensure they produce the expected results.

2. Code Structure Analysis

Some tests also examine the source code to verify:

  • Presence of comments
  • Use of specific programming constructs (variables, operations)
  • Proper coding patterns

3. Flexible Matching

Tests are designed to be flexible where appropriate:

  • Personal greeting allows any name but requires specific message structure
  • Morning routine accepts various activities but checks for reasonable keywords
  • Story sequence validates structure rather than exact content

4. Clear Error Messages

When tests fail, they provide clear feedback showing:

  • What was expected
  • What was actually received
  • Specific requirements that weren't met

🛠️ Configuration

The testing system is configured via pytest.ini in the project root:

[tool:pytest]
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
addopts = -v --tb=short

📝 Adding New Tests

When adding new exercises, follow this pattern:

  1. Create test class: TestExercise{chapter}_{exercise_number}
  2. Test method naming: test_{exercise_name}_output
  3. Standard structure:
    def test_exercise_output(self):
        exercise_path = PROJECT_ROOT / "exercises" / "chapter" / "exercise" / "main.py"
        
        result = subprocess.run([sys.executable, str(exercise_path)], 
                               capture_output=True, text=True, cwd=PROJECT_ROOT)
        
        assert result.returncode == 0, f"Program failed to run: {result.stderr}"
        # Add specific assertions for the exercise

✅ Validation

The test_solutions.py script validates that all tests work correctly by:

  1. Temporarily copying solution files to main.py
  2. Running the corresponding tests
  3. Verifying all tests pass with correct solutions
  4. Restoring original main.py files

This ensures the testing system is reliable before students use it.

🎓 Educational Value

The testing system serves multiple educational purposes:

  • Immediate feedback: Students know instantly if their solution works
  • Clear requirements: Test failures show exactly what needs to be fixed
  • Best practices: Tests encourage proper coding patterns and structure
  • Confidence building: Passing tests gives students confidence in their solutions

🔄 Continuous Integration

The testing system is designed to work with CI/CD pipelines for:

  • Automated validation of new exercises
  • Regression testing when content is updated
  • Quality assurance for the course materials