README.md 2.62 KB
Newer Older
1
2
# Run Unit Tests

3
SGLang uses the built-in library [unittest](https://docs.python.org/3/library/unittest.html) as the testing framework.
Lianmin Zheng's avatar
Lianmin Zheng committed
4
5
6
7

## Test Backend Runtime
```bash
cd sglang/test/srt
8
9

# Run a single file
Lianmin Zheng's avatar
Lianmin Zheng committed
10
python3 test_srt_endpoint.py
11

Lianmin Zheng's avatar
Lianmin Zheng committed
12
# Run a single test
Lianmin Zheng's avatar
Lianmin Zheng committed
13
python3 test_srt_endpoint.py TestSRTEndpoint.test_simple_decode
Lianmin Zheng's avatar
Lianmin Zheng committed
14
15

# Run a suite with multiple files
16
python3 run_suite.py --suite per-commit
17
18
```

Lianmin Zheng's avatar
Lianmin Zheng committed
19
20
21
## Test Frontend Language
```bash
cd sglang/test/lang
22
23

# Run a single file
Lianmin Zheng's avatar
Lianmin Zheng committed
24
python3 test_choices.py
25
```
26
27
28
29

## Adding or Updating Tests in CI

- Create new test files under `test/srt` or `test/lang` depending on the type of test.
Lianmin Zheng's avatar
Lianmin Zheng committed
30
31
32
- Ensure they are referenced in the respective `run_suite.py` (e.g., `test/srt/run_suite.py`) so they are picked up in CI. For most small test cases, they can be added to the `per-commit-1-gpu` suite. Sort the test cases alphabetically by name.
- Ensure you added `unittest.main()` for unittest and `pytest.main([__file__])` for pytest in the scripts. The CI run them via `python3 test_file.py`.
- The CI will run some suites such as `per-commit-1-gpu`, `per-commit-2-gpu`, and `nightly-1-gpu` automatically. If you need special setup or custom test groups, you may modify the workflows in [`.github/workflows/`](https://github.com/sgl-project/sglang/tree/main/.github/workflows).
33
34
35

## Writing Elegant Test Cases

Lianmin Zheng's avatar
Lianmin Zheng committed
36
37
38
39
- Learn from existing examples in [sglang/test/srt](https://github.com/sgl-project/sglang/tree/main/test/srt).
- Reduce the test time by using smaller models and reusing the server for multiple test cases. Launching a server takes a lot of time.
- Use as few GPUs as possible. Do not run long tests with 8-gpu runners.
- If the test cases take too long, considering adding them to nightly tests instead of per-commit tests.
40
41
42
43
- Keep each test function focused on a single scenario or piece of functionality.
- Give tests descriptive names reflecting their purpose.
- Use robust assertions (e.g., assert, unittest methods) to validate outcomes.
- Clean up resources to avoid side effects and preserve test independence.
44
45
46
47
48
49
- Reduce the test time by using smaller models and reusing the server for multiple test cases.


## Adding New Models to Nightly CI
- **For text models**: extend [global model lists variables](https://github.com/sgl-project/sglang/blob/85c1f7937781199203b38bb46325a2840f353a04/python/sglang/test/test_utils.py#L104) in `test_utils.py`, or add more model lists
- **For vlms**: extend global variable of lauch setttings list containing `ModelLaunchSettings` in `test_nightly_vlms_.*.py`, see [here](https://github.com/sgl-project/sglang/blob/85c1f7937781199203b38bb46325a2840f353a04/test/srt/test_nightly_vlms_mmmu_eval.py#L18)