MQLUnit - Micro unit testing framework for complex Expert Advisors - MetaTrader 5 script


As a Java developer for 20 years, I'm used to writing more complex programs using test-driven development techniques. When I started writing more complex Expert Advisors, I lacked a reliable framework that supported my unit testing in MQL5. So I developed my own simple and straightforward solution and MQLUnit was born. It simplifies test writing so that it can be integrated into everyone's development process. I recommend you to use it immediately after developing more complex MQL5 programs. What exactly does "more complex" mean? Whenever your code exceeds a certain number of lines of code, you typically create components to handle the complexity. When you have multiple components, it's time to seriously consider a good testing strategy to ensure the quality of your code.
Download the zip file and export it to your MQL5/Include directory or directly to your EA project. MQLUnit consists of classes that you can use when writing unit tests. Users of MQLUnit create an instance of the CUnit test suite to write and run their unit tests. The following code snippet shows a simple test suite that performs setup functions and unit tests.
//+------------------------------------------------------------------+ //|MQL unit| //| Copyright 2021, Niklas Schlim | //| https://github.com/nschlimm/MQL5Unit | //+------------------------------------------------------------------+ #Property Copyright "Copyright 2021, Niklas Schlim" # Property version "1.00" #include "MQLUnitTestLibrary.mqh" Integer m_movingAverageHandle; blank initialization () { m_testSuite = ComposeTestsuite(); }; //+------------------------------------------------------------------+ //|Simple test example of moving average indicator //+------------------------------------------------------------------+ CUnitTestSuite* ComposeTestsuite() { CUnitTestSuite* testSuite = new CUnitTestSuite(); testSuite.AddSetupFunction( 1 , Test_Indicators_copyBuffer_setup); testSuite.AddUnitTestFunction( 2 ,Test_Indicators_copyBuffer); testSuite.AddTearDownFunction( 3 , Test_Indicators_copyBuffer_tearDown); return test suite; } //+------------------------------------------------------------------+ //|Initialization indicator setting method //+------------------------------------------------------------------+ Blank Test_Indicators_copyBuffer_setup() { // Initialization indicator m_movingAverageHandle= ima ( _symbol , PERIOD_CURRENT , 10 , 0 , mode_SMA , PRICE_CLOSE ); } //+------------------------------------------------------------------+ //|Detach and remove the indicator //+------------------------------------------------------------------+ Blank Test_Indicators_copyBuffer_tearDown() { // Remove indicator m_movingAverageHandle= Invalid ; } //+------------------------------------------------------------------+ //|Test indicators //+------------------------------------------------------------------+ CUnitTestAsserts* Test_Indicators_copyBuffer() { CUnitTestAsserts* ut = new CUnitTestAsserts( "Test Indicators_Copy Buffer" ); Double moving average data[]; Copy buffer (m_movingAverageHandle, 0 , 1 , 10 , moving average data); // Check whether the data is copied to the local array ut.IsTrue( __file__ , __line__ , moving average data[ 0 ] > 0 ); return Ut; } //+------------------------------------------------------------------+
Here's what you need to do:
If everything is fine, you can see the results of the test case in the strategy tester log, as shown in the image below.
that's all. You have defined your test cases. You can use a simple framework to first write unit tests that define the interfaces of your program components. The result is a cleaner design and better testability. And it will definitely improve the quality of the show. Another advantage is that when you make changes to your program, you can rerun all your tests as regression tests.
If you have questions or suggestions, please contact us. I'm happy to answer all questions and make improvements to the framework!
Attachment download
📎 mqlunittestasserts.mqh (13.42 KB)
📎 mqlunittestlibrary.mqh (2.15 KB)
📎 mqlunittestsuite.mqh (16.3 KB)
📎 simpletestsuite.mq5 (2.28 KB)
📎 suitetemplate.mq5 (1.24 KB)
Source: MQL5 #33089
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •