Testing Guide¶
To ensure the accuracy and stability of the AKQuant backtesting engine, we have established a layered testing system. This guide will help you understand how to run tests, interpret results, and add new test cases.
Testing System Overview¶
AKQuant's tests are divided into the following levels:
- Unit Tests
- Rust: Run with
cargo testto cover core algorithms (matching, fee calculation, risk control). - Python: Run with
pytestto cover Python interfaces, data processing, and configuration parsing.
- Rust: Run with
- Golden Tests / Integration Tests
- Use Synthetic Data to simulate specific market scenarios (e.g., T+1, Limit Up/Down, Option Exercise).
- Compare backtest results (equity curve, trade records, metrics) strictly against a locked Baseline.
- Ensure that any algorithmic changes do not unintentionally alter backtest results.
🚀 Quick Start¶
1. Prepare Environment¶
Ensure you have installed development dependencies:
2. Run All Tests¶
3. Run Only Golden Tests¶
🏆 Golden Tests¶
Golden Tests are the core of AKQuant's quality assurance. They are located in the tests/golden/ directory.
Directory Structure¶
tests/golden/
├── strategies/ # Strategy code for testing (e.g., stock_t1.py)
├── data/ # Auto-generated synthetic data (Parquet format)
├── baselines/ # Locked baseline results (Standard Answers)
├── runner.py # Test executor and comparison logic
├── gen_data.py # Data generation script
└── test_golden.py # Pytest entry point
Included Scenarios¶
| Test Case | Description | Verification Points |
|---|---|---|
| stock_t1 | Stock T+1 Trading | Day 1 buy -> Day 2 sell allowed; Day 3 buy -> Day 3 sell rejected. |
| futures_margin | Futures Margin | Initial capital sufficient for 1 lot; attempting to buy 2nd lot rejected due to insufficient margin. |
| option_basic | Option Basic Trading | Verify option contract buying, holding, selling (closing position), and PnL calculation. |
How to Update Baseline?¶
If you modify core algorithms (e.g., improved matching logic or fee calculation) causing backtest results to change expectedly, the Golden Tests will fail. You need to update the baseline:
- Confirm Differences: Carefully check the failure output to ensure the differences are caused by your code changes and are as expected.
- Regenerate Baseline:
- Submit Changes: Commit the updated
tests/golden/baselines/files along with your code.
➕ How to Add New Tests?¶
Add Python Unit Tests¶
Create a new test_*.py file in the tests/ directory and write tests using pytest style:
Add New Golden Test Case¶
- Generate Data: Modify
tests/golden/gen_data.pyto add new data generation logic. - Write Strategy: Create a new strategy file in
tests/golden/strategies/(e.g.,my_test.py). - Register Test: Modify the
mainfunction intests/golden/runner.pyto add a newrun_testcall. - Generate Baseline: Run
python tests/golden/runner.py --generate-baseline.
FAQ¶
Q: Why do tests pass locally but fail in CI? A: This might be due to environmental differences (e.g., Pandas version, OS floating-point precision). Golden tests have a certain Tolerance, but large discrepancies will still cause errors. Check the CI logs for details.
Q: How to debug Rust parts?
A: Use cargo test to run native Rust tests. If you need to debug Python calls into Rust, refer to the mixed debugging guide in the developer documentation.