Hi. In the past I was able to create unit test in Python, I found out there is this dependency quantconnect-stubs that allows you to run unit test against part of your code coupled to Quantconnect API.Unfortunetely this is not anywhere in the documentation, at least nothing that google or an llm was able to find. So I'm creating this post so at least there is any recollection in lean or quantconnect that in order to unit test QCAlgorithm you need to install quantconnect-stubs As the unit test will run outside lean, you will need to create a venv as part of your project, when running locally.
HTH
Mia Alissi
It seems you're interested in using
quantconnect-stubs
for unit testing QuantConnect algorithms in a local Python environment. Here's a detailed guide on how to set it up and run your tests effectively:Step-by-Step Setup
Create a Virtual Environment:
Activate the Virtual Environment:
Install Required Packages:
quantconnect-stubs
andpytest
(or any other testing framework you prefer):Writing Tests:
quantconnect-stubs
to simulate QuantConnect API classes and methods.pytest
:Reviewing API References
If you're looking to better understand how certain methods and classes in QuantConnect work, here's a quick overview of some relevant ones:
warm_up_indicator
: This method facilitates warming up an indicator with historical data.on_data
: The core method that gets called with new data slices, pivotal in algorithm operation.on_end_of_algorithm
: Handy for asserting final states or cleaning up resources at the algorithm's end.This setup allows you to test parts of your code that interact with the QuantConnect API, facilitating development and debugging in a local environment before deploying onto QuantConnect's platform.
Sources: Getting Started - QuantConnect.com, Getting Started - QuantConnect.com, Documentation Home - QuantConnect.com, Documentation Home - QuantConnect.com, Documentation Home - QuantConnect.com
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.
DanyG
I suppose I was wrong, this stubs are just interfaces for IDEs but cannot be used for unit test
I hope someone from Quantconnect can explain how you suppose to do unit test.
Decoupling form Lean is almost impossible, I cannot create a test double for each class, from the simple Symbol to QCAlgorithm creation of Indicators or RollingWindow
forget if you want to do a Functional unit test (Kent Beck's style, classical TDD style)
Â
Â
Â
DanyG
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!