# ๐Ÿงช Environment Validation Tests Comprehensive tests to ensure TinyTorch environment is correctly configured and all dependencies work. ## ๐ŸŽฏ For Students **Easy-to-use command with beautiful output:** ```bash # Quick health check (1 second) tito system health # Comprehensive validation (5 seconds) tito system check # Verbose output for debugging tito system check --verbose ``` **Perfect for**: - โœ… Verifying your environment after setup - โœ… Checking everything works before starting a module - โœ… Debugging when something isn't working - โœ… Sharing with TAs when you need help **See**: [HOW_TO_USE.md](HOW_TO_USE.md) for complete student guide with examples. --- ## ๐Ÿ”ฌ For Developers ### Run All Validation Tests ```bash # Via TITO (recommended - beautiful output) tito system check # Via pytest (raw test output) pytest tests/environment/ -v ``` ### Run Specific Test Suites **Setup Validation** (comprehensive environment check): ```bash pytest tests/environment/test_setup_validation.py -v ``` **Requirements Validation** (all packages from requirements.txt): ```bash pytest tests/environment/test_all_requirements.py -v ``` ## Test Suites ### 1. Setup Validation (`test_setup_validation.py`) **Tests 50+ environment checks** organized into categories: #### Python Environment - โœ… Python version (3.8+) - โœ… Virtual environment active - โœ… pip available #### Core Dependencies - โœ… NumPy: import, arrays, matrix operations - โœ… Matplotlib: import, plotting, save figures - โœ… pytest: available for testing - โœ… PyYAML: import, YAML serialization - โœ… Rich: console rendering #### Jupyter Environment - โœ… Jupyter installed - โœ… JupyterLab available - โœ… jupyter command available - โœ… jupyter lab command works - โœ… Python3 kernel configured - โœ… Jupytext for .py โ†” .ipynb conversion #### TinyTorch Package - โœ… tinytorch package importable - โœ… tinytorch.core available - โœ… Version info defined - โœ… Tensor class (if Module 01 completed) #### Project Structure - โœ… tinytorch/ package directory - โœ… modules/ student workspace - โœ… src/ source modules - โœ… tests/ test directory - โœ… TITO CLI available #### System Resources - โœ… Adequate disk space (1GB+) - โœ… Adequate memory (checks available) - โœ… Python architecture (warns about Rosetta on M1/M2) #### Git Configuration - โœ… Git available - โœ… Git user configured - โœ… Repository initialized ### 2. Requirements Validation (`test_all_requirements.py`) **Automatically discovers and tests ALL packages** from requirements files: #### Auto-Discovery - ๐Ÿ“ Finds all requirements*.txt files in project - ๐Ÿ“‹ Parses package specifications (handles >=, ==, <, etc.) - ๐Ÿ” Converts package names to import names (PyYAML โ†’ yaml, etc.) #### Package Tests - โœ… **Installation**: Package can be imported - โœ… **Version**: Installed version matches specification - โœ… **Functionality**: Package actually works (not just installed) #### Functionality Tests Include: - **numpy**: Array creation and operations - **matplotlib**: Plot creation and saving - **pytest**: Command availability - **jupyterlab**: Command availability - **jupytext**: Notebook parsing - **PyYAML**: YAML serialization - **rich**: Console rendering - **Generic**: Import test for other packages #### Consistency Checks - โœ… No conflicting version specs across files - โœ… Requirements files are readable - โœ… Requirements files are parseable ## Example Output ### Successful Run ```bash $ pytest tests/environment/ -v tests/environment/test_setup_validation.py::TestPythonEnvironment::test_python_version PASSED โœ… Python 3.10.8 tests/environment/test_setup_validation.py::TestPythonEnvironment::test_virtual_environment_active PASSED โœ… Virtual environment active: /Users/student/TinyTorch/.venv tests/environment/test_setup_validation.py::TestCoreDependencies::test_numpy_import PASSED โœ… NumPy 1.24.3 imported tests/environment/test_setup_validation.py::TestCoreDependencies::test_numpy_operations PASSED โœ… NumPy operations work correctly ... tests/environment/test_all_requirements.py::TestRequiredPackages::test_package_installed[numpy] PASSED โœ… numpy v1.24.3 installed tests/environment/test_all_requirements.py::TestRequiredPackages::test_package_functionality[numpy] PASSED โœ… numpy: Array operations work ... ============================== 75 passed in 2.5s ============================== ๐ŸŽ‰ All validation tests passed! โœ… TinyTorch environment is correctly configured ๐Ÿ’ก Next: tito module 01 ``` ### Failed Run (with helpful errors) ```bash $ pytest tests/environment/ -v tests/environment/test_all_requirements.py::TestRequiredPackages::test_package_installed[matplotlib] FAILED โŒ matplotlib cannot be imported Import name: matplotlib Required by: requirements.txt Install: pip install matplotlib>=3.9.0 Error: No module named 'matplotlib' tests/environment/test_setup_validation.py::TestJupyterEnvironment::test_jupyter_lab_command FAILED โŒ jupyter lab command not found Fix: pip install jupyterlab ============================== 2 failed, 73 passed in 2.3s ============================== โŒ Some validation tests failed ๐Ÿ”ง Install missing packages: pip install -r requirements.txt ``` ## Integration with TITO ### `tito system health` Basic environment check (quick): ```bash tito system health # Shows: # โœ… Python 3.10.8 # โœ… Virtual environment active # โœ… NumPy v1.24.3 # โœ… Matplotlib v3.7.1 # โœ… Jupyter available ``` ### `tito system check` Comprehensive validation (runs all tests): ```bash tito system check # Runs both test suites: # 1. test_setup_validation.py (50+ checks) # 2. test_all_requirements.py (all packages) # # Takes ~5 seconds # Shows detailed results for each check ``` ### `tito system health` Quick validation (essential checks only): ```bash tito system health # Runs: # - Python environment # - Core dependencies (numpy, jupyter) # - TinyTorch package # # Takes ~1 second # Good for "is everything basically working?" ``` ## Adding New Tests ### For New Dependencies Add to `test_package_functionality()` in `test_all_requirements.py`: ```python elif package_name.lower() == 'mypackage': import mypackage # Test basic functionality result = mypackage.do_something() return result is not None, "Basic function works" ``` ### For New Environment Checks Add new test to `test_setup_validation.py`: ```python class TestMyComponent: """Test my new component.""" def test_my_check(self): """Description of what is tested.""" # Your test logic assert something_works, "Error message" print("โœ… My component works") ``` ## CI/CD Integration ### GitHub Actions ```yaml - name: Validate Environment run: | pip install -r requirements.txt pytest tests/environment/ -v ``` ### Pre-commit Hook ```bash # .git/hooks/pre-commit #!/bin/bash pytest tests/environment/test_all_requirements.py -q ``` ## Troubleshooting ### Tests fail with "No module named 'X'" ```bash # Install missing package pip install -r requirements.txt # Or specific package pip install X ``` ### Tests fail with version mismatch ```bash # Upgrade package to required version pip install --upgrade X # Or reinstall everything pip install -r requirements.txt --force-reinstall ``` ### Virtual environment not detected ```bash # Activate virtual environment source .venv/bin/activate # Mac/Linux .venv\Scripts\activate # Windows # Then run tests again pytest tests/environment/ -v ``` ### Jupyter tests fail ```bash # Reinstall Jupyter pip install --upgrade jupyter jupyterlab # Check kernel jupyter kernelspec list # Install kernel if missing python -m ipykernel install --user ``` ## Best Practices 1. **Run before starting work**: `tito system check` 2. **Run after setup**: Automatically runs at end of `tito setup` 3. **Run after package updates**: `pip install -r requirements.txt && pytest tests/environment/` 4. **Include in CI/CD**: Ensures environment consistency 5. **Add tests for new dependencies**: Keep validation comprehensive ## Performance - **Quick check** (~1s): Basic imports and versions - **Full validation** (~5s): All functionality tests - **Cached results**: Pytest caches successful imports ## What Gets Tested โœ… **60+ automated checks** across: - Python environment (3 checks) - Core dependencies (7 checks) - Jupyter environment (6 checks) - TinyTorch package (4 checks) - Project structure (7 checks) - System resources (3 checks) - Git configuration (3 checks) - All requirements.txt packages (N checks) - Package version consistency (1 check) - Requirements file validity (2 checks) **Result**: Complete confidence that environment works before students start!