This lab focuses on Test-Driven Development (TDD)—writing test cases first and then implementing the required functionality. Each student will contribute one test case and submit a pull request.
The repository is organized as follows:
tdd_lab/
├── 📂 tests/ # Contains all test cases
│ ├── 📄 test_counter.py # Test cases for the counter API (each student contributes a test)
├── 📂 src/ # Source code for the counter service
│ ├── 📄 __init__.py # Flask app initialization
│ ├── 📄 counter.py # Counter API implementation
│ ├── 📄 status.py # HTTP status codes
├── 📄 requirements.txt # Dependencies for the project
├── 📄 pytest.ini # Pytest configuration
├── 📄 README.md # Project documentationTo follow this lab, you need Python version 3.8 or later. The exercises have been tested with the following versions: 3.8.1, 3.9.5, 3.9.6, 3.9.7, and 3.10.10. However, any Python version 3.8+ should work without configuration issues.
If you encounter any setup or configuration problems, please reach out to the T.A. for assistance.
Sometimes it is useful to upgrade pip before installing dependencies. If you like, run: pip install --upgrade pip and later install the dependencies using: pip install -r requirements.txt
- It is a good practice to configure python virtual environment. Use the commands below to setup python virtual environment on
Linux/MacOSorWindows OSpython3 -m venv venv source venv/bin/activate # macOS/Linux venv\Scripts\activate # Windows
pip install -r requirements.txt- macOS/Linux
export FLASK_APP=src- Windows
set FLASK_APP=srcflask run✅ Visit http://127.0.0.1:5000/counters/foo in the browser. If it returns {"error": "Counter not found"}, your API is working!
If you are having trouble merging changes to the main branch of the team's repo, you can take a look at this doc: How to Handle Merge Conflicts in the Testing Lab.
Below are common errors students may encounter and their solutions:
| Error | Cause | Solution |
|---|---|---|
ImportError: cannot import name 'app' from 'src' |
Flask app is not detected | Run export FLASK_APP=src before running flask run |
Error: No such command 'db' |
Flask-Migrate missing | Run pip install flask-migrate |
sqlalchemy.exc.OperationalError: table account has no column named balance |
Database not migrated | Run flask db upgrade |
ModuleNotFoundError: No module named 'src' |
Missing dependencies | Run pip install -r requirements.txt |
If you continue to experience issues, follow these steps:
- Check that Flask is running with
flask run. - Ensure all dependencies are installed with
pip install -r requirements.txt. - Consult your team first before reaching out for help.
- If the issue persists, open a GitHub Issue in your team repository, including:
- A clear description of the problem.
- The exact error message.
- Steps you have already tried.
🚀 Debug first, then ask for help!