|
| 1 | +# Running Tests |
| 2 | + |
| 3 | +This document provides instructions on how to set up your environment for testing and how to run the various tests included in this project. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +1. **Create and Activate a Virtual Environment (Recommended):** |
| 8 | + |
| 9 | + ```bash |
| 10 | + conda env create -f environment.yaml |
| 11 | + conda activate unity_multiagent_rl |
| 12 | + ``` |
| 13 | + |
| 14 | + Or using pip: |
| 15 | + |
| 16 | + ```bash |
| 17 | + python -m venv .venv |
| 18 | + source .venv/bin/activate # On Windows use: .venv\Scripts\activate |
| 19 | + pip install -r requirements.txt |
| 20 | + ``` |
| 21 | + |
| 22 | +2. **Install Dependencies:** |
| 23 | + Ensure all project dependencies, including testing libraries, are installed. The testing libraries (`pytest`, `pytest-mock`) are included in both `requirements.txt` and `environment.yaml`. |
| 24 | + ```bash |
| 25 | + pip install -r requirements.txt |
| 26 | + ``` |
| 27 | + |
| 28 | +## Running Tests |
| 29 | + |
| 30 | +All tests are written using the `pytest` framework. |
| 31 | + |
| 32 | +### Test Categories |
| 33 | + |
| 34 | +The project includes several categories of tests: |
| 35 | + |
| 36 | +1. **Algorithm Tests** (`tests/algos/`): Test individual algorithm implementations (MAPPO, MADDPG, MASAC, MATD3) |
| 37 | +2. **Network Tests** (`tests/networks/`): Test neural network modules and components |
| 38 | +3. **Buffer Tests** (`tests/buffers/`): Test replay buffer and rollout storage functionality |
| 39 | +4. **Evaluation Tests** (`tests/eval/`): Test evaluation and competitive evaluation systems |
| 40 | +5. **Smoke Tests** (`tests/test_smoke_runs.py`): Quick end-to-end tests for all algorithms |
| 41 | + |
| 42 | +### Basic Test Commands |
| 43 | + |
| 44 | +- **Run All Tests:** |
| 45 | + To run all tests in the project, navigate to the root directory and execute: |
| 46 | + |
| 47 | + ```bash |
| 48 | + pytest |
| 49 | + ``` |
| 50 | + |
| 51 | + You can add `-v` for more verbose output. |
| 52 | + |
| 53 | +- **Run Tests in a Specific Directory:** |
| 54 | + To run all tests within a particular directory (e.g., algorithm tests): |
| 55 | + |
| 56 | + ```bash |
| 57 | + pytest tests/algos/ |
| 58 | + ``` |
| 59 | + |
| 60 | +- **Run Tests in a Specific File:** |
| 61 | + To run all tests within a particular file (e.g., MADDPG tests): |
| 62 | + |
| 63 | + ```bash |
| 64 | + pytest tests/algos/test_maddpg.py |
| 65 | + ``` |
| 66 | + |
| 67 | +- **Run a Specific Test Function:** |
| 68 | + To run a specific test function by name: |
| 69 | + |
| 70 | + ```bash |
| 71 | + pytest tests/algos/test_maddpg.py::test_maddpg_initialization |
| 72 | + ``` |
| 73 | + |
| 74 | +- **Run Smoke Tests Only:** |
| 75 | + To run quick smoke tests for all algorithms: |
| 76 | + |
| 77 | + ```bash |
| 78 | + pytest tests/test_smoke_runs.py |
| 79 | + ``` |
| 80 | + |
| 81 | +- **Run Tests with a Keyword Expression:** |
| 82 | + To run tests whose names match a keyword expression: |
| 83 | + |
| 84 | + ```bash |
| 85 | + pytest -k "maddpg and not initialization" |
| 86 | + ``` |
| 87 | + |
| 88 | +## Test Coverage (Optional) |
| 89 | + |
| 90 | +To generate a test coverage report, you can install `pytest-cov`: |
| 91 | + |
| 92 | +```bash |
| 93 | +pip install pytest-cov |
| 94 | +``` |
| 95 | + |
| 96 | +Then run pytest with the coverage flag: |
| 97 | + |
| 98 | +```bash |
| 99 | +pytest --cov=. --cov-report html |
| 100 | +``` |
| 101 | + |
| 102 | +This will generate an HTML report in the `htmlcov/` directory, which you can open in a web browser to see detailed coverage information. |
| 103 | + |
| 104 | +## Useful Test Options |
| 105 | + |
| 106 | +- **Run tests with verbose output:** |
| 107 | + |
| 108 | + ```bash |
| 109 | + pytest -v |
| 110 | + ``` |
| 111 | + |
| 112 | +- **Run tests and stop on first failure:** |
| 113 | + |
| 114 | + ```bash |
| 115 | + pytest -x |
| 116 | + ``` |
| 117 | + |
| 118 | +- **Run tests in parallel (requires pytest-xdist):** |
| 119 | + |
| 120 | + ```bash |
| 121 | + pip install pytest-xdist |
| 122 | + pytest -n auto |
| 123 | + ``` |
| 124 | + |
| 125 | +- **Run only failed tests from last run:** |
| 126 | + ```bash |
| 127 | + pytest --lf |
| 128 | + ``` |
| 129 | + |
| 130 | +## Continuous Integration (CI) Suggestion |
| 131 | + |
| 132 | +To automate the running of tests, consider setting up a Continuous Integration workflow. Below is a basic example for GitHub Actions. Create a file named `.github/workflows/ci.yml`: |
| 133 | + |
| 134 | +```yaml |
| 135 | +name: Python CI |
| 136 | +
|
| 137 | +on: |
| 138 | + push: |
| 139 | + branches: [main] |
| 140 | + pull_request: |
| 141 | + branches: [main] |
| 142 | +
|
| 143 | +jobs: |
| 144 | + build: |
| 145 | + runs-on: ubuntu-latest |
| 146 | + strategy: |
| 147 | + matrix: |
| 148 | + python-version: ["3.11"] # Project uses Python 3.11 |
| 149 | +
|
| 150 | + steps: |
| 151 | + - uses: actions/checkout@v4 |
| 152 | + - name: Set up Python ${{ matrix.python-version }} |
| 153 | + uses: actions/setup-python@v4 |
| 154 | + with: |
| 155 | + python-version: ${{ matrix.python-version }} |
| 156 | + - name: Install dependencies |
| 157 | + run: | |
| 158 | + python -m pip install --upgrade pip |
| 159 | + pip install -r requirements.txt |
| 160 | + - name: Test with pytest |
| 161 | + run: | |
| 162 | + pytest -v |
| 163 | +``` |
| 164 | + |
| 165 | +This workflow will run tests on every push to the `main` branch and on every pull request targeting `main`, using Python 3.11 as specified in the project requirements. |
0 commit comments