This document describes the comprehensive test suite for the Python shell implementation.
The test suite is organized into several files:
tests/test_shell.py- Unit tests for individual shell componentstests/test_integration.py- Integration tests for complete shell functionalityrun_tests.py- Simple test runner script
-
Command Parsing Tests - Test command line parsing logic
- Simple commands (no arguments)
- Commands with arguments
- Commands with quoted arguments
- Commands with redirection
- Edge cases and error conditions
-
Built-in Command Tests - Test each built-in command individually
echo- Text outputpwd- Current directory displaytype- Command type identificationcd- Directory navigationexit- Shell termination
-
External Command Tests - Test external program execution
- Successful command execution
- Failed command execution
- Non-existent commands
-
PATH Management Tests - Test PATH environment handling
- Building executable command dictionary
- Handling empty PATH
- Handling non-existent PATH directories
-
Edge Case Tests - Test unusual inputs and error conditions
- Empty commands
- Whitespace-only commands
- Complex quoting
- Multiple redirect symbols
-
Redirection Tests - Test output redirection functionality
- Basic redirection with
> - Redirection with
1> - Commands without redirection
- Basic redirection with
-
Full Integration Tests - Test complete shell workflows
- End-to-end command processing
- Environment setup and teardown
- Real directory navigation
- External command execution
-
Command Chaining Tests - Test multiple command sequences
- Multiple echo commands
- Directory navigation sequences
- Mixed command types
# Run all tests
pipenv run python -m pytest
# Run with verbose output
pipenv run python -m pytest -v
# Run specific test file
pipenv run python -m pytest tests/test_shell.py
# Run specific test class
pipenv run python -m pytest tests/test_shell.py::TestEchoCommand
# Run specific test
pipenv run python -m pytest tests/test_shell.py::TestEchoCommand::test_echo_single_word# Simple test execution
python run_tests.py
# Help information
python run_tests.py --helpThe test suite covers:
- ✅ All built-in commands (
echo,pwd,type,cd,exit) - ✅ External command execution
- ✅ Command parsing with various arguments and quoting
- ✅ Output redirection parsing (preparation for implementation)
- ✅ PATH environment variable handling
- ✅ Error handling and edge cases
- ✅ Directory navigation and environment setup
- ✅ Integration scenarios with multiple commands
Tests use isolated environments:
- Temporary directories for file system operations
- Mocked PATH variables to avoid system interference
- Mocked HOME directories for consistent testing
- Subprocess mocking for external command testing
To add new tests:
- For unit tests: Add to
tests/test_shell.pyin the appropriate test class - For integration tests: Add to
tests/test_integration.py - Follow naming conventions: Test methods should start with
test_ - Use descriptive names: Test names should clearly describe what they test
- Include docstrings: Explain what each test validates
- Clean up: Use setup/teardown methods to maintain test isolation
- Comprehensive: Cover all functionality and edge cases
- Isolated: Each test should be independent
- Fast: Tests should run quickly for rapid feedback
- Readable: Test code should be clear and well-documented
- Maintainable: Tests should be easy to update as code evolves