Development Guide
This document provides comprehensive information for developers working on TradeTrack.
Table of Contents
- Getting Started for Developers
- Project Structure
- Code Style and Standards
- Testing
- Building and Distribution
- Contributing Guidelines
Getting Started for Developers
Prerequisites
- Python 3.9 or higher
- Git
- Virtual environment (recommended)
Development Setup
-
Clone the repository:
git clone https://github.com/randyoyarzabal/stocks.git cd stocks -
Set up development environment:
# Create virtual environment python3 -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate # Install production dependencies pip install -r requirements.txt # Install development dependencies pip install -r tests/requirements.txt -
Verify development setup:
# Run the automated test setup python tests/setup_tests.py # Run basic tests python tests/run_tests.py --syntax
๐งช Testing
Test Structure
The test suite is located in the tests/ directory and follows a practical approach for a small CLI project:
tests/
โโโ __init__.py
โโโ conftest.py
โโโ requirements.txt # Test dependencies
โโโ setup_tests.py # Automated test setup
โโโ run_tests.py # Test runner script
โโโ README.md # Test documentation
โโโ test_basic.py # Core functionality tests
โโโ test_config_loader.py # Configuration tests
โโโ test_currency_formatter.py
โโโ test_portfolio_loader.py
โโโ test_yahoo_quotes.py
โโโ test_rich_display.py
โโโ test_portfolio_library.py
Test Philosophy
This test suite follows a focused approach:
- Essential functionality only - Tests core features that users depend on
- Simple and maintainable - Easy to understand and update
- Fast execution - Quick feedback during development
- Practical coverage - Tests what matters most, not everything
Running Tests
Quick Test Commands
# Run all tests
python tests/run_tests.py
# Run specific test categories
python tests/run_tests.py --basic
python tests/run_tests.py --component
# Run tests with pattern matching
python tests/run_tests.py --pattern "test_config"
# Enable debug mode for detailed error output
python tests/run_tests.py --debug
python tests/run_tests.py --component --debug
# Run with pytest directly
pytest tests/ -v
pytest tests/test_config_loader.py -v
Test Categories
- Basic Tests: Core functionality and imports
- Component Tests: Individual module testing
Debug Mode
The test runner includes a debug mode that provides detailed error information when tests fail. This is particularly useful during development and troubleshooting.
Usage:
# Enable debug mode for all tests
python tests/run_tests.py --debug
# Enable debug mode for specific test categories
python tests/run_tests.py --basic --debug
python tests/run_tests.py --component --debug
# Enable debug mode for pattern matching
python tests/run_tests.py --pattern "test_config" --debug
Debug Output Includes:
- Return code from failed test commands
- Execution duration
- Complete STDOUT output (test results)
- Complete STDERR output (error messages)
- Clear visual separation between debug sections
When to Use Debug Mode:
- When tests are failing and you need to see detailed error information
- During development to understand test behavior
- When troubleshooting CI/CD issues
- When investigating test performance problems
Test Dependencies
Test dependencies are managed in tests/requirements.txt:
# Install test dependencies
pip install -r tests/requirements.txt
# Or use the automated setup
python tests/setup_tests.py
Writing Tests
Test Naming Convention
- Test files:
test_*.py - Test classes:
TestClassName - Test methods:
test_should_do_something_when_condition
Example Test Structure
def test_config_loader_init_default(self):
"""โ Test ConfigLoader initialization with default config path."""
# Arrange
loader = ConfigLoader()
# Act
config = loader.load_config()
# Assert
assert config is not None
assert loader.config_path == Path('conf/config.yaml')
Best Practices
- Use descriptive test names with โ checkmarks
- Follow Arrange-Act-Assert pattern
- Use fixtures for reusable test data
- Mock external dependencies
- Test both success and error cases
- Include edge cases and boundary conditions
Development Tools
Code Quality
Formatting
# Format code with Black
black libs/ tests/
# Sort imports with isort
isort libs/ tests/
Linting
# Run flake8 linting
flake8 libs/ tests/
# Run type checking with mypy
mypy libs/ --ignore-missing-imports
Security Scanning
# Run security linting
bandit -r libs/
# Check for vulnerable dependencies
safety check
Continuous Integration
The project uses GitHub Actions for automated testing:
- Multi-platform: Linux, Windows, macOS
- Multi-version: Python 3.9, 3.10, 3.11, 3.12, 3.13
- Comprehensive: All test categories
- Scheduled: Daily runs at 2 AM UTC
See .github/workflows/test.yml for CI configuration.
๐ Project Structure
tradetrack/
โโโ libs/ # Core application modules
โ โโโ config_loader.py # Configuration management
โ โโโ currency_formatter.py
โ โโโ portfolio_library.py
โ โโโ portfolio_loader.py
โ โโโ rich_display.py
โ โโโ tax_analysis.py
โ โโโ yahoo_quotes.py
โ โโโ lot_analysis.py
โโโ conf/ # Configuration files
โ โโโ config.yaml # Default configuration
โ โโโ version.py # Version information
โโโ docs/ # Documentation
โ โโโ configuration.md
โ โโโ dependencies.md
โ โโโ development.md # This file
โโโ tests/ # Test suite
โโโ templates/ # Template files
โโโ ttrack.py # Main application entry point
โโโ requirements.txt # Production dependencies
โโโ README.md # User documentation
๐ Debugging
Common Issues
- Import Errors: Ensure all dependencies are installed
- Test Failures: Check test data and mock configurations
- Performance Issues: Use profiling tools to identify bottlenecks
- Memory Leaks: Monitor memory usage in long-running tests
Debug Commands
# Run tests with debug output
pytest tests/ -v -s --tb=long
# Run specific test with debug
pytest tests/test_config_loader.py::TestConfigLoader::test_config_loader_init_default -v -s
# Profile test performance
pytest tests/ --profile
๐ Performance Testing
Memory Testing
# Run memory tests
python tests/run_tests.py --memory
# Monitor memory usage
python -m memory_profiler tests/test_runtime.py
Performance Benchmarks
# Run performance tests
python tests/run_tests.py --performance
# Benchmark specific functions
pytest tests/ -k "performance" --benchmark-only
๐ Contributing
Development Workflow
- Create feature branch:
git checkout -b feature/new-feature - Make changes and test:
# Run tests python tests/run_tests.py # Check code quality black libs/ tests/ flake8 libs/ tests/ - Commit changes:
git add . git commit -m "Add new feature with tests" - Push and create PR:
git push origin feature/new-feature
Code Review Checklist
- All tests pass
- Code follows style guidelines
- New features have tests
- Documentation is updated
- No security vulnerabilities
- Performance impact is acceptable
๐ Learning Resources
Python Testing
- pytest Documentation
- unittest.mock Documentation
- Python Testing with pytest
- Effective Python Testing
Development Tools
๐ Getting Help
Internal Resources
- Check
tests/README.mdfor detailed test documentation - Review existing tests for examples
- Check CI logs for test failures
External Resources
- Python documentation
- pytest documentation
- Rich library documentation
- Yahoo Finance API documentation
Note: This development guide is maintained alongside the codebase. Please update it when adding new features or changing the development process.