NinjaTrader UnitTest is an add-on for NinjaTrader 8 that provides a unit testing framework similar to Python's unittest, but written in C# and adapted for NinjaTrader.
To install NinjaTrader.UnitTest, follow these steps:
- Download the source code from the GitHub repository.
- Build the
NinjaTrader.UnitTest.slnsolution using Visual Studio to generate theNinjaTrader.UnitTest.dllfile. - Copy the
NinjaTrader.UnitTest.dllfile to thebin/Customfolder of your NinjaTrader 8 installation. - In the NinjaScript Editor, right-click on the
Referencesfolder and selectAdd Reference.... - In the
Add Referencewindow, browse to theNinjaTrader.UnitTest.dllfile and select it.
To use the NinjaTrader UnitTest add-on in your own NinjaScript project, follow these steps:
- Add a reference to the
NinjaTrader.UnitTestnamespace. - Create test cases by subclassing the
NinjaTrader.UnitTest.TestCaseclass.
Here is an example of how to create a simple test case using the NinjaTrader UnitTest add-on:
using System;
using NinjaTrader.UnitTest;
namespace NinjaTrader.NinjaScript.AddOns
{
public class MyTestCase : TestCase
{
public Tests(string name) : base(name)
{ }
public void TestAddition()
{
int result = 2 + 2;
int expected = 4;
Assert.AreEqual(expected, result);
}
}
}This code defines a new test case called MyTestCase that contains a single test method called TestAddition. This test method uses the Assert.AreEqual method from the NinjaTrader.UnitTest.Assert class to verify that the result of adding 2 and 2 is equal to 4.
To run this test case, you can create an instance of the MyTestCase class and call its Run method, like this:
MyTestCase myTestCase = new MyTestCase();
myTestCase.Run();Alternatively, you can create a TestSuite object and add the MyTestCase object to it, like this:
namespace NinjaTrader.NinjaScript.AddOns
{
public class TestAddon : NinjaTrader.NinjaScript.AddOnBase
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "TestAddon";
Description = "An add-on that runs UnitTests on Ninjatrader";
}
else if (State == State.Configure)
{
// Subscribe to connection updates
Connection.ConnectionStatusUpdate += OnConnectionStatusUpdate;
}
else if (State == State.Terminated)
{
// Unsubscribe from connection updates
Connection.ConnectionStatusUpdate -= OnConnectionStatusUpdate;
}
}
// This method is fired on connection status update events
protected void OnConnectionStatusUpdate(object sender, ConnectionStatusEventArgs connectionStatusUpdate)
{
if (connectionStatusUpdate.Status == ConnectionStatus.Connected)
{
NinjaTrader.NinjaScript.NinjaScript.Log("Connected for orders at " + DateTime.Now, LogLevel.Information);
// Run tests
TestSuite suite = new TestSuite();
suite.Add(new MyTestCase("TestAddition"));
TestResult result = suite.Run();
result.PrintSummary();
}
else if (connectionStatusUpdate.Status == ConnectionStatus.ConnectionLost)
{
NinjaTrader.NinjaScript.NinjaScript.Log("Connection for orders lost at: " + DateTime.Now, LogLevel.Information);
}
}
}
}This will execute the TestAddition method and log the results using the NinjaScript.Log method.
We welcome contributions! If you have ideas for improving this tool, please feel free to submit a pull request or open an issue.
This project is licensed under the MIT license. See the LICENSE.txt file for more information.