Skip to content

Commit 44bf2f1

Browse files
FindHaofacebook-github-bot
authored andcommitted
Refactor test suite to use unittest and remove pytest configuration (#27)
Summary: - Removed `conftest.py` file and migrated tests from pytest to unittest framework. - Updated `tests/test_tritonparse.py` to define test cases using `unittest.TestCase` for better organization. - Adjusted README documentation to reflect changes in test execution and structure. - Implemented setup and teardown methods for managing Triton hooks and CUDA device availability. This refactor enhances the maintainability and clarity of the test suite by standardizing on unittest. Pull Request resolved: #27 Test Plan: ``` % python -m unittest tests.test_tritonparse -v test_convert (tests.test_tritonparse.TestTritonparseCPU.test_convert) Test convert function with various data types ... ok test_extract_python_source_info (tests.test_tritonparse.TestTritonparseCUDA.test_extract_python_source_info) Test extract_python_source_info function ... ok test_whole_workflow (tests.test_tritonparse.TestTritonparseCUDA.test_whole_workflow) Test unified_parse functionality ... Temporary directory: /tmp/tmp85w50lrw Found 1 log files in /tmp/tmp85w50lrw/logs: ['dedicated_log_triton_trace_yhao_.ndjson'] tritonparse log file list: /tmp/tmpcudrc3a1/log_file_list.json INFO:tritonparse:Copying parsed logs from /tmp/tmpcudrc3a1 to /tmp/tmp85w50lrw/parsed_output ok ---------------------------------------------------------------------- Ran 3 tests in 3.465s ``` Reviewed By: xuzhao9 Differential Revision: D78018758 Pulled By: FindHao fbshipit-source-id: 5d7677649186899e89b4f782af468d28e1905513
1 parent 4fc3a64 commit 44bf2f1

File tree

5 files changed

+252
-285
lines changed

5 files changed

+252
-285
lines changed

pyproject.toml

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ dependencies = [
1111

1212
[project.optional-dependencies]
1313
test = [
14-
"pytest>=7.0.0",
15-
"pytest-cov>=4.0.0",
16-
"pytest-xdist>=3.0.0",
14+
"coverage>=7.0.0",
1715
]
1816

1917
[tool.setuptools.packages.find]
@@ -26,20 +24,5 @@ line-length = 88
2624
line-length = 88
2725
target-version = ["py310"]
2826

29-
[tool.pytest.ini_options]
30-
testpaths = ["tests"]
31-
python_files = ["test_*.py"]
32-
python_classes = ["Test*"]
33-
python_functions = ["test_*"]
34-
addopts = [
35-
"-v",
36-
"--tb=short",
37-
"--strict-markers",
38-
"--disable-warnings",
39-
]
40-
markers = [
41-
"cuda: mark test as requiring CUDA",
42-
]
43-
4427
[project.urls]
4528
"Homepage" = "https://github.com/pytorch-labs/tritonparse"

tests/README.md

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ This directory contains the test suite for tritonparse, including both automated
55
## Test Structure
66

77
### Automated Tests
8-
- `test_tritonparse.py`: Comprehensive automated test suite
9-
- `conftest.py`: Pytest configuration and fixtures
8+
- `test_tritonparse.py`: Comprehensive automated test suite using unittest
109
- `__init__.py`: Makes the directory a Python package
1110

1211
### Manual Test Examples
@@ -30,26 +29,27 @@ pip install -e ".[test]"
3029

3130
#### Running All Tests
3231
```bash
33-
# Using pytest directly
34-
python -m pytest tests/test_tritonparse.py -v
32+
# Using unittest directly
33+
python -m unittest tests.test_tritonparse -v
3534

36-
# With print statements visible
37-
python -m pytest tests/test_tritonparse.py -s -v
35+
# With print statements visible (unittest shows prints by default on failure)
36+
python -m unittest tests.test_tritonparse -v
3837

39-
# With coverage
40-
python -m pytest tests/test_tritonparse.py --cov=tritonparse --cov-report=html
38+
# For coverage (requires coverage package)
39+
coverage run -m unittest tests.test_tritonparse
40+
coverage html
4141
```
4242

4343
#### Running Specific Test Categories
4444
```bash
4545
# CPU-only tests
46-
python -m pytest tests/test_tritonparse.py -m "not cuda"
46+
python -m unittest tests.test_tritonparse.TestTritonparseCPU -v
4747

4848
# CUDA tests only (requires CUDA)
49-
python -m pytest tests/test_tritonparse.py -m cuda
49+
python -m unittest tests.test_tritonparse.TestTritonparseCUDA -v
5050

5151
# Specific test function
52-
python -m pytest tests/test_tritonparse.py::test_whole_workflow -s -v
52+
python -m unittest tests.test_tritonparse.TestTritonparseCUDA.test_whole_workflow -v
5353
```
5454

5555
### Manual Test Example
@@ -63,7 +63,7 @@ The `test_add.py` file serves as a manual test example that demonstrates:
6363

6464
#### Running Manual Test
6565
```bash
66-
# Direct execution (not through pytest)
66+
# Direct execution (not through unittest)
6767
TORCHINDUCTOR_FX_GRAPH_CACHE=0 TRITONPARSE_DEBUG=1 python tests/test_add.py
6868
```
6969

@@ -75,25 +75,25 @@ This will:
7575
## Test Categories
7676

7777
### CPU Tests (No CUDA Required)
78-
- `test_convert()`: Tests data conversion functionality with various data types
79-
- `test_unified_parse()`: Tests parsing functionality with mock data
78+
- `TestTritonparseCPU.test_convert()`: Tests data conversion functionality with various data types
8079

8180
### CUDA Tests (Require GPU)
82-
- `test_extract_python_source_info()`: Tests Python source code extraction during Triton compilation
83-
- `test_whole_workflow()`: Tests complete workflow from kernel execution to log parsing
81+
- `TestTritonparseCUDA.test_extract_python_source_info()`: Tests Python source code extraction during Triton compilation
82+
- `TestTritonparseCUDA.test_whole_workflow()`: Tests complete workflow from kernel execution to log parsing
8483

8584
## Test Features
8685

87-
### Fixtures
88-
- `triton_hooks_setup`: Manages Triton hooks and compilation settings
86+
### Test Setup
87+
- `TestTritonparseCUDA.setUp()`: Manages Triton hooks and compilation settings
8988
- Saves and restores all triton knobs (compilation and runtime hooks)
9089
- Ensures clean state between tests
9190
- Automatically restores settings even if tests fail
91+
- `TestTritonparseCUDA.tearDown()`: Restores original settings after each test
9292

9393
### CUDA Device Management
94-
- `cuda_device`: Provides CUDA device for tests
95-
- `cuda_available`: Checks CUDA availability
96-
- Automatic skipping of CUDA tests when CUDA is not available
94+
- Built-in CUDA availability checking in test setUp methods
95+
- Automatic skipping of CUDA tests when CUDA is not available using `@unittest.skipUnless`
96+
- Self-managed CUDA device assignment in test classes
9797

9898
### Kernel Isolation
9999
Each test function defines its own Triton kernel to avoid compilation cache interference:
@@ -110,35 +110,40 @@ The following environment variables are used during testing:
110110

111111
## Test Configuration
112112

113-
### Pytest Configuration (`conftest.py`)
114-
- Custom markers for CUDA tests
115-
- Automatic CUDA availability checking
116-
- Fixtures for device management
113+
### Unittest Configuration
114+
- CUDA availability checking built into test classes
115+
- Setup and teardown methods for proper test isolation
116+
- Class-based organization for logical test grouping
117117

118118
### Test Isolation
119-
- Each test function has its own kernel definition
120-
- Fixtures ensure clean state between tests
119+
- Each test method has its own kernel definition
120+
- setUp/tearDown methods ensure clean state between tests
121121
- Temporary directories are automatically cleaned up
122122

123123
## Adding New Tests
124124

125125
### For Automated Tests
126-
1. Add test functions to `test_tritonparse.py`
127-
2. Use `@pytest.mark.cuda` for CUDA tests
128-
3. Use `triton_hooks_setup` fixture for tests that modify Triton settings
129-
4. Define kernels inside test functions to avoid cache interference
126+
1. Add test methods to appropriate TestCase classes in `test_tritonparse.py`
127+
2. Use `@unittest.skipUnless(torch.cuda.is_available(), "CUDA not available")` for CUDA tests
128+
3. Add tests to `TestTritonparseCUDA` class for tests that modify Triton settings (automatic setUp/tearDown)
129+
4. Define kernels inside test methods to avoid cache interference
130130

131131
Example:
132132
```python
133-
@pytest.mark.cuda
134-
def test_my_function(cuda_device, triton_hooks_setup):
135-
@triton.jit
136-
def my_kernel(x_ptr, y_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
137-
# Your kernel logic here
133+
class TestTritonparseCUDA(unittest.TestCase):
134+
def setUp(self):
135+
# Automatic setup for CUDA tests
136+
pass
137+
138+
@unittest.skipUnless(torch.cuda.is_available(), "CUDA not available")
139+
def test_my_function(self):
140+
@triton.jit
141+
def my_kernel(x_ptr, y_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
142+
# Your kernel logic here
143+
pass
144+
145+
# Your test code here
138146
pass
139-
140-
# Your test code here
141-
pass
142147
```
143148

144149
### For Manual Tests
@@ -164,7 +169,7 @@ Tests are automatically run on GitHub Actions for:
164169
### Debug Mode
165170
```bash
166171
# Run with verbose output and print statements
167-
python -m pytest tests/test_tritonparse.py -s -v --tb=long
172+
python -m unittest tests.test_tritonparse -v
168173
```
169174

170175
### Manual Verification

tests/conftest.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/test_add.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import torch
1111
import triton
1212
import triton.language as tl
13+
1314
import tritonparse.structured_logging
1415
import tritonparse.utils
1516

0 commit comments

Comments
 (0)