Hi QC Python users,
How do you unit test your algorithms?
Say you have a custom indicator or some entry or exit logic you would like to unit test, how do you do that?
All code that is importing something from the `QuantConnect` package cannot be executed outside of a Lean.
Are there any example to do this? Is there a way to run Python unit tests within a Lean environment?
AK M
+1, I'd like to know as well.
Fred Painchaud
Hi,
It depends on if you are talking about unit testing in the cloud or locally and what kind of features in the algo you want to unit test.
So, in the cloud, it is not easy, or maybe even impossible to unit test. The level of control is not there to easily do it.
Locally, there are things that can be done, and it goes to the kind of features you want to unit test.
For instance, if you want to unit test your OnData() method, well, good luck 😊. You would rather be more effective doing debugging with a debugger, logging, use assertions, typing with mypy, etc.
So this is true for all methods that are heavily linked to LEAN, more or less, that are called by LEAN which feeds inputs to your algo…
But for everything else that is there to support your algo, data structures, logic, etc, that can be unit tested, locally, just like any other unit testing. You can use unittest for instance or any other Python unit testing framework.
Fred
Philipp Hofmann
Thank you Fred - do you know how I can leverage the QuantConnect Browser Coding view for this? I see that i can right-click on a file and select 'run tests', but if I do, I get 'no tests found in selected file or folder', despite there being test code. I tried with unittest and pytest syntax, no luck.Â
Raffaele
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!