|
| 1 | +# Pytest Demo Project |
| 2 | +A comprehensive testing framework based on Pytest, featuring configuration management, database integration, performance testing, and HTML report generation. |
| 3 | + |
| 4 | +## 📋 Project Features |
| 5 | + |
| 6 | +- **Modern Testing Framework**: Complete testing solution built on Pytest 7.0+ |
| 7 | +- **Configuration Management**: YAML configuration file support with thread-safe singleton pattern |
| 8 | +- **Database Integration**: Built-in MySQL support with automatic result storage |
| 9 | +- **Performance Testing**: Integrated EasyPerfBenchmark performance testing tool |
| 10 | +- **HTML Reporting**: Automatically generates timestamped HTML test reports |
| 11 | +- **Tagging System**: Supports multi-dimensional test markers (stage, feature, platform, etc.) |
| 12 | + |
| 13 | +## 🗂️ Project Structure |
| 14 | + |
| 15 | +``` |
| 16 | +pytest_demo/ |
| 17 | +├── common/ # Common modules |
| 18 | +│ ├── __init__.py |
| 19 | +│ ├── config_utils.py # Configuration utilities |
| 20 | +│ ├── db_utils.py # Database utilities |
| 21 | +│ ├── EasyPerfBenchmark/ # Performance testing module |
| 22 | +│ │ ├── __init__.py |
| 23 | +│ │ ├── EasyPerfBenchmark.py # Performance testing implementation |
| 24 | +│ └── └── requirements.txt |
| 25 | +├── results/ # Results storage directory |
| 26 | +├── suites/ # Test suites |
| 27 | +│ ├── demo/ # Example tests |
| 28 | +│ │ └── test_demo.py |
| 29 | +│ └── E2E/ # End-to-end tests |
| 30 | +│ └── test_performance.py |
| 31 | +├── config.yaml # Main configuration file |
| 32 | +├── conftest.py # Pytest configuration file |
| 33 | +├── pytest.ini # Pytest configuration |
| 34 | +├── requirements.txt # Project dependencies |
| 35 | +└── readme.md # This document |
| 36 | +``` |
| 37 | + |
| 38 | +## 🚀 Quick Start |
| 39 | + |
| 40 | +### Requirements |
| 41 | + |
| 42 | +- Python 3.8+ |
| 43 | +- MySQL 5.7+ (optional, for database functionality) |
| 44 | +- Git |
| 45 | + |
| 46 | +### Installation Steps |
| 47 | + |
| 48 | +1. **Install Dependencies** |
| 49 | + ```bash |
| 50 | + pip install -r requirements.txt |
| 51 | + ``` |
| 52 | + |
| 53 | +2. **Configure Database** (Optional) |
| 54 | + |
| 55 | + Edit the database configuration in `config.yaml`: |
| 56 | + ```yaml |
| 57 | + database: |
| 58 | + enabled: true |
| 59 | + host: "127.0.0.1" |
| 60 | + port: 3306 |
| 61 | + name: "ucm_pytest" |
| 62 | + user: "root" |
| 63 | + password: "123456" |
| 64 | + charset: "utf8mb4" |
| 65 | + ``` |
| 66 | +
|
| 67 | +3. **Run Tests** |
| 68 | + ```bash |
| 69 | + # Run all tests |
| 70 | + pytest |
| 71 | + |
| 72 | + # Run tests with specific markers |
| 73 | + pytest --stage=1 |
| 74 | + pytest --feature=performance |
| 75 | + ``` |
| 76 | + |
| 77 | +## ⚙️ Configuration Guide |
| 78 | + |
| 79 | +### config.yaml Configuration |
| 80 | + |
| 81 | +The project supports full YAML configuration management. Main configuration items include: |
| 82 | + |
| 83 | +- **reports**: Report configuration (HTML reports, timestamps, etc.) |
| 84 | +- **database**: Database connection settings |
| 85 | +- **easyPerf**: Performance testing configuration reference (API, models, experiment parameters) |
| 86 | + |
| 87 | +## 🧪 Test Examples |
| 88 | + |
| 89 | +### Basic Functional Test |
| 90 | + |
| 91 | +```python |
| 92 | +# suites/E2E/test_demo_performance.py |
| 93 | +import pytest |
| 94 | + |
| 95 | +@pytest.fixture(scope="module", name="calc") |
| 96 | +def calculator(): |
| 97 | + return Calculator() |
| 98 | + |
| 99 | +@pytest.mark.feature("mark") |
| 100 | +class TestCalculator: |
| 101 | + def test_add(self, calc): |
| 102 | + assert calc.add(1, 2) == 3 |
| 103 | + |
| 104 | + def test_divide_by_zero(self, calc): |
| 105 | + with pytest.raises(ZeroDivisionError): |
| 106 | + calc.divide(6, 0) |
| 107 | +``` |
| 108 | + |
| 109 | +### Performance Test |
| 110 | + |
| 111 | +```python |
| 112 | +# suites/E2E/test_demo_performance.py |
| 113 | +import pytest |
| 114 | +from common.EasyPerfBenchmark.EasyPerfBenchmark import EasyPerfBenchmark |
| 115 | + |
| 116 | +@pytest.mark.performance("performance1") |
| 117 | +def test_easyperf_benchmark(easyperf_config=config_instance.get_config("easyPerf")): |
| 118 | + benchmark = EasyPerfBenchmark(easyperf_config) |
| 119 | + results = benchmark.run_all() |
| 120 | + assert len(results) == len(easyperf_config["experiments"]) |
| 121 | +``` |
| 122 | + |
| 123 | +## 🏷️ Test Tagging System |
| 124 | + |
| 125 | +The project supports multi-dimensional test tagging: |
| 126 | + |
| 127 | +### Test Stage Tags |
| 128 | +- `stage(0)`: Unit tests |
| 129 | +- `stage(1)`: Smoke tests |
| 130 | +- `stage(2)`: Regression tests |
| 131 | +- `stage(3)`: Release tests |
| 132 | + |
| 133 | +### Feature Tags |
| 134 | +- `feature`: Feature module tags |
| 135 | +- `platform`: Platform tags (GPU/NPU) |
| 136 | + |
| 137 | +### Usage Examples |
| 138 | + |
| 139 | +```bash |
| 140 | +# Run smoke tests and above |
| 141 | +pytest --stage=1+ |
| 142 | + |
| 143 | +# Run tests for specific features |
| 144 | +pytest --feature=performance |
| 145 | +pytest --feature=performance,reliability |
| 146 | + |
| 147 | +# Run tests for specific platforms |
| 148 | +pytest --platform=gpu |
| 149 | +``` |
| 150 | + |
| 151 | +## 📊 Reporting System |
| 152 | + |
| 153 | +### HTML Reports |
| 154 | + |
| 155 | +The project automatically generates timestamped HTML test reports: |
| 156 | +- Location: `reports/pytest_YYYYMMDD_HHMMSS/report.html` |
| 157 | +- Contains detailed test results, error information, and execution times |
| 158 | +- Supports custom report titles and styling |
| 159 | + |
| 160 | +### Database Storage |
| 161 | + |
| 162 | +When database functionality is enabled, test results are automatically stored in MySQL: |
| 163 | +- Test case information table: `test_case_info` |
| 164 | +- Automatically adds test build ID for result tracking |
| 165 | + |
| 166 | +## 🔧 Advanced Features |
| 167 | + |
| 168 | +### Configuration Management |
| 169 | + |
| 170 | +Uses thread-safe singleton pattern for configuration management: |
| 171 | + |
| 172 | +```python |
| 173 | +from common.config_utils import config_utils |
| 174 | + |
| 175 | +# Get configuration |
| 176 | +db_config = config_utils.get_config("database") |
| 177 | +api_config = config_utils.get_nested_config("easyPerf.api") |
| 178 | +``` |
| 179 | + |
| 180 | +### Database Utilities |
| 181 | + |
| 182 | +Built-in database connection and operation tools: |
| 183 | + |
| 184 | +```python |
| 185 | +from common.db_utils import write_to_db, get_db |
| 186 | + |
| 187 | +# Write data |
| 188 | +# If table doesn't exist, it will be created using the fields from first write |
| 189 | +data = {"name": "test", "value": 123} |
| 190 | +success = write_to_db("test_table", data) |
| 191 | +``` |
| 192 | + |
| 193 | +## 🛠️ Development Guide |
| 194 | + |
| 195 | +### Adding New Tests |
| 196 | + |
| 197 | +1. Create a new test file under the `suites/` directory |
| 198 | +2. Use appropriate test markers |
| 199 | +3. Follow naming convention: `test_*.py` |
| 200 | +4. Use fixtures for test data management |
| 201 | + |
| 202 | +### Extending Configuration |
| 203 | + |
| 204 | +1. Edit `config.yaml` to add new configuration items |
| 205 | +2. Access configuration in code using `config_utils` |
| 206 | +3. Ensure configuration items have reasonable default values |
0 commit comments