Skip to content

Commit 6d8c2d2

Browse files
authored
Merge pull request #1 from legalaspro/feat/comprehensive-testing-suite
Feat/comprehensive testing suite
2 parents ae5230d + 0980fbb commit 6d8c2d2

22 files changed

+5037
-9
lines changed

TESTING.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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.

algos/random.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,29 @@ def __init__(self, action_spaces):
1111

1212
@torch.no_grad()
1313
def act(self, _obs, deterministic=True):
14-
return [torch.as_tensor([sp.sample()]) for sp in self.action_spaces]
14+
actions = []
15+
for sp in self.action_spaces:
16+
sample = sp.sample()
17+
# Convert to tensor directly without wrapping in list to avoid warning
18+
action_tensor = torch.as_tensor(sample, dtype=torch.float32)
19+
# Ensure it's at least 1D for consistency
20+
if action_tensor.ndim == 0:
21+
action_tensor = action_tensor.unsqueeze(0)
22+
actions.append(action_tensor)
23+
return actions
1524

1625
# stubs to keep snapshot() calls happy
17-
def policy_snapshot(self, *_):
26+
def policy_snapshot(self, *_):
1827
return self
19-
def snapshot(self, *_):
28+
def snapshot(self, *_):
2029
return self
21-
def to(self, *_):
30+
def to(self, *_):
2231
return self
23-
def cpu(self):
32+
def cpu(self):
2433
return self
25-
def cuda(self):
34+
def cuda(self):
2635
return self
27-
def set_eval_mode(self):
36+
def set_eval_mode(self):
2837
return self
29-
def set_train_mode(self):
38+
def set_train_mode(self):
3039
return self

pytest.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pytest]
2+
filterwarnings =
3+
ignore:Call to deprecated create function.*:DeprecationWarning:python.communicator_objects.*
4+
ignore::DeprecationWarning:python.communicator_objects.*
5+
ignore::DeprecationWarning

requirements.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ tensorboard>=2.12.0
1313

1414
# Configuration
1515
pyyaml>=6.0
16-
python-dotenv>=1.0.0
16+
python-dotenv>=1.0.0
17+
18+
# Testing
19+
pytest>=7.0.0
20+
pytest-mock

0 commit comments

Comments
 (0)