r/learnpython • u/Lokajko-Dev-343 • Sep 12 '26
What tests are actually worth writing for a backtesting engine?
I’ve recently been adding tests to a small Python backtesting engine I’m building, and it made me realize how easy it is to test the obvious things while missing the important ones.
So far I’m testing things like:
- opening and closing LONG/SHORT positions
- transaction costs
- portfolio valuation
- forced position closing at the end of the dataset
- PnL and win rate
- drawdowns
- VaR/CVaR
- full end-to-end backtest scenarios
The most useful tests so far have been small deterministic examples where I can calculate the expected result manually.
For example:
- start with $10,000
- BUY at 100
- HOLD
- SELL at 120
Then the engine should end with exactly $12,000 before fees.
For people with more testing experience: what kinds of edge cases would you consider essential here?
I’m especially interested in cases that pass normal unit tests but still produce subtly incorrect financial results.