|
| 1 | +--- |
| 2 | +name: unit-tests |
| 3 | +description: Run Django unit tests in the edx-enterprise Docker container. Use when the user wants to run tests, check if tests pass, or verify test coverage. |
| 4 | +argument-hint: "<TEST_FILES>" |
| 5 | +allowed-tools: Bash(docker *), Bash(make *), Bash(colima *) |
| 6 | +--- |
| 7 | + |
| 8 | +## Arguments |
| 9 | + |
| 10 | +`<TEST_FILES>` (optional): One or more folders, test file paths, or pytest node IDs to run. Coverage is only enabled when a single folder representing a code domain is provided or no arguments are provided. |
| 11 | + |
| 12 | +Examples: |
| 13 | +- `/unit-tests` — run all tests with coverage |
| 14 | +- `/unit-tests tests/test_enterprise/test_tasks.py` — run one test file without coverage |
| 15 | +- `/unit-tests tests/test_enterprise/test_tasks.py tests/test_consent/test_helpers.py` — run multiple test files without coverage |
| 16 | +- `/unit-tests tests/test_enterprise/test_tasks.py::TestSomeClass::test_method` — run a single test by node ID without coverage |
| 17 | + |
| 18 | +## Routing rules (evaluate in order) |
| 19 | + |
| 20 | +- **No arguments** → Step 2b (whole-project tests + whole-project coverage) |
| 21 | +- **Everything else** → Step 2a (targeted tests, NO coverage) |
| 22 | + |
| 23 | +## Steps |
| 24 | + |
| 25 | +### 1. Make sure the test-shell container is running |
| 26 | + |
| 27 | +Determine if the test-shell container is running: |
| 28 | + |
| 29 | +```bash |
| 30 | +docker compose ps |
| 31 | +``` |
| 32 | + |
| 33 | +Start the test-shell container if not running: |
| 34 | + |
| 35 | +```bash |
| 36 | +make dev.up |
| 37 | +``` |
| 38 | + |
| 39 | +### 2a. Run specific unit test files or functions |
| 40 | + |
| 41 | +If `<TEST_FILES>` is provided, run only those tests using `pytest.local.ini` to disable coverage and warnings: |
| 42 | + |
| 43 | +```bash |
| 44 | +docker compose exec test-shell bash -c "pytest -c pytest.local.ini <TEST_FILES>" |
| 45 | +``` |
| 46 | + |
| 47 | +Never enable coverage reports (by adding `--cov`) when only testing specific files, since the results will be misleading. |
| 48 | + |
| 49 | +### 2b. Run whole-project unit tests and generate coverage |
| 50 | + |
| 51 | +If no arguments are given, assume the user wants to run the full test suite for the entire project: |
| 52 | + |
| 53 | +```bash |
| 54 | +docker compose exec test-shell make test |
| 55 | +``` |
| 56 | + |
| 57 | +Whole-project coverage will be reported in the console output. Specific line numbers with missing coverage will be reported. |
| 58 | + |
| 59 | +## Troubleshooting |
| 60 | + |
| 61 | +### ModuleNotFoundError |
| 62 | + |
| 63 | +If tests fail due to missing imports, first try to install requirements: |
| 64 | + |
| 65 | +```bash |
| 66 | +docker compose exec test-shell make requirements |
| 67 | +``` |
| 68 | + |
| 69 | +This is necessary at least when adding new requirements which have not yet been built into the image. |
| 70 | + |
| 71 | +### Failed to connect to the docker API on MacOS |
| 72 | + |
| 73 | +This likely just means colima needs to be started: |
| 74 | + |
| 75 | +```bash |
| 76 | +colima start |
| 77 | +``` |
0 commit comments