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.
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
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
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/ -vValidate that tests work correctly against known solutions:
# Run the solution validation script
python test_solutions.py- 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
- 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
- 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
Tests primarily check the output (stdout) of student programs to ensure they produce the expected results.
Some tests also examine the source code to verify:
- Presence of comments
- Use of specific programming constructs (variables, operations)
- Proper coding patterns
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
When tests fail, they provide clear feedback showing:
- What was expected
- What was actually received
- Specific requirements that weren't met
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=shortWhen adding new exercises, follow this pattern:
- Create test class:
TestExercise{chapter}_{exercise_number} - Test method naming:
test_{exercise_name}_output - 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
The test_solutions.py script validates that all tests work correctly by:
- Temporarily copying solution files to main.py
- Running the corresponding tests
- Verifying all tests pass with correct solutions
- Restoring original main.py files
This ensures the testing system is reliable before students use it.
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
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