|
| 1 | +# UCM Pytest Testing Framework |
| 2 | + |
| 3 | +A pytest-based testing framework that supports multi-level testing, platform tagging, performance data collection, and Allure report generation. |
| 4 | +[Chinese Documentation](README_zh.md) |
| 5 | +## Framework Features |
| 6 | + |
| 7 | +- [x] 🏗️ **Multi-level Testing**: Unit(0) → Smoke(1) → Regression(2) → Release(3) |
| 8 | +- [x] 🏷️ **Flexible Tagging**: Supports feature tags and platform tags |
| 9 | +- [ ] 📊 **Data Collection**: Integrates InfluxDB performance data push |
| 10 | +- [ ] 📋 **Beautiful Reports**: Allure test report integration |
| 11 | +- [ ] 🔧 **Convenient Tools**: Rich testing tools and common methods |
| 12 | + |
| 13 | +## Test Level Definitions |
| 14 | + |
| 15 | +| Level | Name | Description | Execution Timing | |
| 16 | +|-------|------|-------------|------------------| |
| 17 | +| 0 | Unit | Unit tests | Each code commit | |
| 18 | +| 1 | Smoke | Smoke tests | Build verification | |
| 19 | +| 2 | Regression | Regression tests | Before version release | |
| 20 | +| 3 | Release | Release tests | Production environment verification | |
| 21 | + |
| 22 | +## Directory Structure |
| 23 | + |
| 24 | +``` |
| 25 | +project/ |
| 26 | +├── tests/ |
| 27 | +│ ├── config.yaml # Testing framework configuration file |
| 28 | +│ ├── conftest.py # pytest configuration and fixtures, main entry point |
| 29 | +│ ├── common/ # Common utility library |
| 30 | +│ │ ├── __init__.py |
| 31 | +│ │ ├── config_utils.py # Configuration file reading utility |
| 32 | +│ │ ├── influxdb_utils.py # InfluxDB writing utility |
| 33 | +│ │ ├── allure_utils.py # Allure report utility |
| 34 | +│ ├── suites/ # Test case directory |
| 35 | +│ │ ├── unit/ # Unit tests (stage 0) |
| 36 | +│ │ ├── smoke/ # Smoke tests (stage 1) |
| 37 | +│ │ ├── regression/ # Regression tests (stage 2) |
| 38 | +│ │ └── release/ # Release tests (stage 3) |
| 39 | +├── requirements.txt # Dependencies |
| 40 | +├── pytest.ini # pytest configuration |
| 41 | +└── README.md # User guide |
| 42 | +``` |
| 43 | + |
| 44 | +## Test Case Standards |
| 45 | + |
| 46 | +### Basic Structure |
| 47 | + |
| 48 | +```python |
| 49 | +import pytest |
| 50 | +from tests.common.influxdb_utils import push_to_influx |
| 51 | +from tests.common.test_utils import setup_test_env |
| 52 | + |
| 53 | +class TestExample: |
| 54 | + """Test example class""" |
| 55 | + |
| 56 | + @pytest.mark.stage(2) |
| 57 | + @pytest.mark.feature("accuracy") |
| 58 | + def test_calculation_accuracy(self): |
| 59 | + """Test calculation accuracy""" |
| 60 | + # Arrange |
| 61 | + input_data = 1 + 1 |
| 62 | + |
| 63 | + # Act |
| 64 | + result = calculate(input_data) |
| 65 | + |
| 66 | + # Assert |
| 67 | + assert result == 2 |
| 68 | + |
| 69 | + # Collect performance data (to be implemented) |
| 70 | + push_to_influx("calculation_time", 0.001, { |
| 71 | + "test_name": "test_calculation_accuracy", |
| 72 | + "feature": "accuracy" |
| 73 | + }) |
| 74 | +``` |
| 75 | + |
| 76 | +### Tag Usage Specifications |
| 77 | + |
| 78 | +#### 1. Level Tags (Required) |
| 79 | +```python |
| 80 | +@pytest.mark.stage(0) # Unit test |
| 81 | +@pytest.mark.stage(1) # Smoke test |
| 82 | +@pytest.mark.stage(2) # Regression test |
| 83 | +@pytest.mark.stage(3) # Release test |
| 84 | +``` |
| 85 | + |
| 86 | +#### 2. Feature Tags (Recommended) |
| 87 | +```python |
| 88 | +@pytest.mark.feature("performance") # Performance test |
| 89 | +@pytest.mark.feature("accuracy") # Accuracy test |
| 90 | +``` |
| 91 | + |
| 92 | +#### 3. Platform Tags (Optional) |
| 93 | +```python |
| 94 | +@pytest.mark.platform("gpu") # Only execute on GPU platform |
| 95 | +@pytest.mark.platform("npu") # Only execute on NPU platform |
| 96 | +# Default no tag, execute on all platforms |
| 97 | +``` |
| 98 | + |
| 99 | +#### 4. Combined Usage |
| 100 | +```python |
| 101 | +@pytest.mark.stage(2) |
| 102 | +@pytest.mark.feature("performance") |
| 103 | +@pytest.mark.gpu |
| 104 | +def test_gpu_performance(self): |
| 105 | + """GPU Performance Test""" |
| 106 | + pass |
| 107 | +``` |
| 108 | + |
| 109 | +### Test Method Naming Convention |
| 110 | + |
| 111 | +```python |
| 112 | +def test_[scenario]_[expected_result]: |
| 113 | + """Test scenario description""" |
| 114 | + pass |
| 115 | + |
| 116 | +# Example: |
| 117 | +def test_calculation_returns_correct_result(): |
| 118 | + """Test calculation returns correct result""" |
| 119 | + pass |
| 120 | + |
| 121 | +``` |
| 122 | + |
| 123 | +### Assertion Specification |
| 124 | + |
| 125 | +```python |
| 126 | +# Recommended: Clear assertion messages |
| 127 | +assert result == expected, f"Expected {expected}, actual {result}" |
| 128 | + |
| 129 | +# Recommended: Use pytest built-in assertions |
| 130 | +assert isinstance(result, dict) |
| 131 | +assert len(items) > 0, "Result list should not be empty" |
| 132 | + |
| 133 | +# Data validation |
| 134 | +assert 0 <= accuracy <= 1.0, "Accuracy should be in the range of 0-1" |
| 135 | +assert response.status_code == 200 |
| 136 | +``` |
| 137 | + |
| 138 | +## Usage |
| 139 | + |
| 140 | +### Install Dependencies |
| 141 | +```bash |
| 142 | +pip install -r requirements.txt |
| 143 | +``` |
| 144 | + |
| 145 | +### Run Tests |
| 146 | + |
| 147 | +#### 1. Run by Levels |
| 148 | +```bash |
| 149 | +# Run all tests |
| 150 | +pytest |
| 151 | + |
| 152 | +# Run specific levels |
| 153 | + pytest --stage=1 # Only run smoke tests |
| 154 | + pytest --stage=2,3 # Run regression and release tests |
| 155 | + pytest --stage=1+ # Run smoke and all higher level tests |
| 156 | +``` |
| 157 | + |
| 158 | +#### 2. Run by Features |
| 159 | +```bash |
| 160 | +# Run specific feature tests |
| 161 | +pytest --feature=accuracy |
| 162 | +pytest --feature=performance,accuracy |
| 163 | +``` |
| 164 | + |
| 165 | +#### 3. Run by Platforms |
| 166 | +```bash |
| 167 | +# Run all tests (default) |
| 168 | +pytest |
| 169 | + |
| 170 | +# Only run GPU-related tests |
| 171 | + pytest --platform=gpu |
| 172 | + |
| 173 | + # Exclude NPU tests |
| 174 | + pytest --platform=!npu |
| 175 | +``` |
| 176 | + |
| 177 | +#### 4. Combined Filtering |
| 178 | +```bash |
| 179 | +# Run performance regression tests on GPU platform |
| 180 | +pytest --stage=2 --platform=gpu --feature=performance |
| 181 | +``` |
| 182 | + |
| 183 | + |
0 commit comments