|
| 1 | +# Datadog Test Optimization SDK for Python |
| 2 | + |
| 3 | +This SDK provides integration with Datadog's Test Optimization features for Python applications. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Test Session Management |
| 8 | +- Test Module Management |
| 9 | +- Test Suite Management |
| 10 | +- Test Management |
| 11 | +- Early Flake Detection |
| 12 | +- Auto test retries |
| 13 | +- Test Skipping |
| 14 | +- Known Tests Tracking |
| 15 | +- Flaky Tests Management |
| 16 | +- Performance Monitoring with Spans |
| 17 | +- Debugging with Mock Tracer |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +Install the package using pip: |
| 22 | + |
| 23 | +```bash |
| 24 | +pip install test-optimization-sdk |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +### Initialization |
| 30 | + |
| 31 | +First, initialize the SDK: |
| 32 | + |
| 33 | +```python |
| 34 | +from test_optimization_sdk import TestOptimization |
| 35 | + |
| 36 | +# Basic initialization |
| 37 | +TestOptimization.init() |
| 38 | + |
| 39 | +# Or with a working directory |
| 40 | +TestOptimization.init_with_working_dir("/path/to/working/dir") |
| 41 | + |
| 42 | +# Or with mock tracer for testing |
| 43 | +TestOptimization.init_mock() |
| 44 | +``` |
| 45 | + |
| 46 | +### Test Session Management |
| 47 | + |
| 48 | +Create and manage test sessions: |
| 49 | + |
| 50 | +```python |
| 51 | +from test_optimization_sdk import TestSession |
| 52 | + |
| 53 | +# Create a new test session |
| 54 | +session = TestSession.create( |
| 55 | + framework="my_framework", |
| 56 | + framework_version="1.0.0" |
| 57 | +) |
| 58 | + |
| 59 | +# Set tags |
| 60 | +session.set_string_tag("environment", "staging") |
| 61 | +session.set_number_tag("timeout", 30.0) |
| 62 | + |
| 63 | +# Set error information if needed |
| 64 | +session.set_error_info( |
| 65 | + error_type="TestFailure", |
| 66 | + error_message="Test failed due to assertion error", |
| 67 | + error_stacktrace="stack trace here" |
| 68 | +) |
| 69 | + |
| 70 | +# Create a test module |
| 71 | +module = session.create_module( |
| 72 | + name="my_module", |
| 73 | + framework_name="my_framework", |
| 74 | + framework_version="1.0.0" |
| 75 | +) |
| 76 | + |
| 77 | +# Close the session when done |
| 78 | +session.close(0) # 0 for success, non-zero for failure |
| 79 | +``` |
| 80 | + |
| 81 | +### Test Module Management |
| 82 | + |
| 83 | +Manage test modules within a session: |
| 84 | + |
| 85 | +```python |
| 86 | +from test_optimization_sdk import TestModule |
| 87 | + |
| 88 | +# Create a test module (from a session) |
| 89 | +module = session.create_module( |
| 90 | + name="my_module", |
| 91 | + framework_name="my_framework", |
| 92 | + framework_version="1.0.0" |
| 93 | +) |
| 94 | + |
| 95 | +# Set module tags |
| 96 | +module.set_string_tag("module_type", "integration") |
| 97 | +module.set_number_tag("timeout", 60.0) |
| 98 | + |
| 99 | +# Create a test suite |
| 100 | +suite = module.create_test_suite("my_suite") |
| 101 | + |
| 102 | +# Close the module when done |
| 103 | +module.close() |
| 104 | +``` |
| 105 | + |
| 106 | +### Test Suite Management |
| 107 | + |
| 108 | +Manage test suites within a module: |
| 109 | + |
| 110 | +```python |
| 111 | +from test_optimization_sdk import TestSuite |
| 112 | + |
| 113 | +# Create a test suite (from a module) |
| 114 | +suite = module.create_test_suite("my_suite") |
| 115 | + |
| 116 | +# Set suite tags |
| 117 | +suite.set_string_tag("suite_type", "regression") |
| 118 | +suite.set_number_tag("priority", 1.0) |
| 119 | + |
| 120 | +# Set source code information |
| 121 | +suite.set_test_source("src/my_test.py", 10, 20) |
| 122 | + |
| 123 | +# Create a test |
| 124 | +test = suite.create_test("my_test") |
| 125 | + |
| 126 | +# Close the suite when done |
| 127 | +suite.close() |
| 128 | +``` |
| 129 | + |
| 130 | +### Test Management |
| 131 | + |
| 132 | +Manage individual tests within a suite: |
| 133 | + |
| 134 | +```python |
| 135 | +from test_optimization_sdk import Test, TestStatus |
| 136 | + |
| 137 | +# Create a test (from a suite) |
| 138 | +test = suite.create_test("my_test") |
| 139 | + |
| 140 | +# Set test tags |
| 141 | +test.set_string_tag("test_type", "unit") |
| 142 | +test.set_number_tag("timeout", 5.0) |
| 143 | + |
| 144 | +# Set source code information |
| 145 | +test.set_test_source("src/my_test.py", 15, 25) |
| 146 | + |
| 147 | +# Close the test with status |
| 148 | +test.close(TestStatus.PASS) |
| 149 | + |
| 150 | +# Or close with skip reason |
| 151 | +test.close_with_skip_reason("Test skipped due to missing dependencies") |
| 152 | +``` |
| 153 | + |
| 154 | +### Performance Monitoring with Spans |
| 155 | + |
| 156 | +Monitor performance using spans: |
| 157 | + |
| 158 | +```python |
| 159 | +from test_optimization_sdk import Span |
| 160 | + |
| 161 | +# Create a new span |
| 162 | +span = Span.create( |
| 163 | + parent_id=0, # 0 for root span |
| 164 | + operation_name="test_execution", |
| 165 | + service_name="my_service", |
| 166 | + resource_name="my_resource", |
| 167 | + span_type="test" |
| 168 | +) |
| 169 | + |
| 170 | +# Set span tags |
| 171 | +span.set_string_tag("environment", "staging") |
| 172 | +span.set_number_tag("duration", 1.5) |
| 173 | + |
| 174 | +# Set error information if needed |
| 175 | +span.set_error_info( |
| 176 | + error_type="PerformanceIssue", |
| 177 | + error_message="Test execution exceeded timeout", |
| 178 | + error_stacktrace="stack trace here" |
| 179 | +) |
| 180 | + |
| 181 | +# Close the span when done |
| 182 | +span.close() |
| 183 | +``` |
| 184 | + |
| 185 | +### Debugging with Mock Tracer |
| 186 | + |
| 187 | +Use the mock tracer for debugging and testing: |
| 188 | + |
| 189 | +```python |
| 190 | +from test_optimization_sdk import MockTracer |
| 191 | + |
| 192 | +# Reset the mock tracer |
| 193 | +MockTracer.reset() |
| 194 | + |
| 195 | +# Get all finished spans |
| 196 | +finished_spans = MockTracer.get_finished_spans() |
| 197 | + |
| 198 | +# Get all currently open spans |
| 199 | +open_spans = MockTracer.get_open_spans() |
| 200 | +``` |
| 201 | + |
| 202 | +### Settings and Configuration |
| 203 | + |
| 204 | +Access and configure various settings: |
| 205 | + |
| 206 | +```python |
| 207 | +from test_optimization_sdk import TestOptimization |
| 208 | + |
| 209 | +# Get current settings |
| 210 | +settings = TestOptimization.get_settings() |
| 211 | + |
| 212 | +# Get flaky test retry settings |
| 213 | +retry_settings = TestOptimization.get_flaky_test_retries_settings() |
| 214 | + |
| 215 | +# Get known tests |
| 216 | +known_tests = TestOptimization.get_known_tests() |
| 217 | + |
| 218 | +# Get skippable tests |
| 219 | +skippable_tests = TestOptimization.get_skippable_tests() |
| 220 | + |
| 221 | +# Get test management tests |
| 222 | +managed_tests = TestOptimization.get_test_management_tests() |
| 223 | +``` |
| 224 | + |
| 225 | +## Settings Structure |
| 226 | + |
| 227 | +The SDK provides various settings structures for configuration: |
| 228 | + |
| 229 | +### Settings |
| 230 | +- `code_coverage`: Enable/disable code coverage |
| 231 | +- `early_flake_detection`: Early flake detection settings |
| 232 | +- `flaky_test_retries_enabled`: Enable/disable flaky test retries |
| 233 | +- `itr_enabled`: Enable/disable intelligent test runner |
| 234 | +- `require_git`: Enable/disable git integration |
| 235 | +- `tests_skipping`: Enable/disable test skipping |
| 236 | +- `known_tests_enabled`: Enable/disable known tests tracking |
| 237 | +- `test_management`: Test management settings |
| 238 | + |
| 239 | +### Early Flake Detection Settings |
| 240 | +- `enabled`: Enable/disable early flake detection |
| 241 | +- `slow_test_retries`: Settings for slow test retries |
| 242 | +- `faulty_session_threshold`: Threshold for faulty session detection |
| 243 | + |
| 244 | +### Test Management Settings |
| 245 | +- `enabled`: Enable/disable test management |
| 246 | +- `attempt_to_fix_retries`: Number of retries for attempt-to-fix operations |
| 247 | + |
| 248 | +## Development |
| 249 | + |
| 250 | +To set up the development environment: |
| 251 | + |
| 252 | +```bash |
| 253 | +# Clone the repository |
| 254 | +git clone https://github.com/DataDog/test-optimization-native.git |
| 255 | +cd test-optimization-native/sdks/python/test-optimization-sdk |
| 256 | + |
| 257 | +# Create a virtual environment |
| 258 | +python -m venv venv |
| 259 | +source venv/bin/activate # On Windows: venv\Scripts\activate |
| 260 | + |
| 261 | +# Install development dependencies |
| 262 | +pip install -e ".[dev]" |
| 263 | + |
| 264 | +# Run tests |
| 265 | +pytest |
| 266 | + |
| 267 | +# Format code |
| 268 | +black . |
| 269 | +isort . |
| 270 | + |
| 271 | +# Type checking |
| 272 | +mypy . |
| 273 | +``` |
0 commit comments