6.1 Unit testing

The testing procedure described here assumes you are using the NUnit test tool. You can download the NUnit user interface and libraries fromhttp://www.NUnit.org. This web page also gives more information about NUnit. Basically, you create a test class for each of the wrapper classes; in the test classes you implement a test method for each public method in the class.

This chapter focuses on OpenMI-specific testing. The NUnit home page gives detailed information about Unit testing.

Section 4.2 describes how to create test assemblies. The test classes used for the Simple River example are shown in Figure 18.


Fig. 18 Wrapper and test classes for the Simple river model

There is a one-to-one relation between the wrapper classes and the test classes, with two exceptions. There is no test class for the SimpleRiverEngineDLLAccess class and there is one additional class called UseCaseTests. The test class for the SimpleRiverEngineDLLAccess class was left out because every method in the SimpleRiverEngineDotNetAccess class will invoke the corresponding method in the SimpleRiverEngineDLLAccess class; therefore testing all methods in the SimpleRiverEngineDotNetAccess class will be sufficient.

The main idea of unit testing is to create very simple code that will test each method in the classes. However, it can also be useful to make some more advanced tests that are actually running full simulations. This is done in the UseCaseTests class. This class ensures that the use cases described in section 2.1 run without errors.

As described earlier, the model is migrated by implementing the IEngine interface. For every IEngine method in the MyEngineWrapper class you decide which method needs to be implemented in the remaining wrapper classes. You will implement the methods or functions that are needed in the engine core and then implement the corresponding methods in the MyEngineDLLAccess class and MyEngineDotNetAccess class. Each time you have completed the implementation of a method you can create a test method in the MyEngineDotNetAccessTest class and run the unit test. You can then move on to implement the method in MyEngineAccess and the associated test methods.

Below is the sample test code for the GetModelID method implementation in the Simple River model. Figure 19 shows the NUnit interface.

using System;
using System.Collections;
using System.IO;
using Oatc.OpenMI.Examples.ModelComponents.SimpleRiver.Wrapper;
using NUnit.Framework;

namespace Oatc.OpenMI.Examples.ModelComponents.SimpleRiver.Wrapper.UnitTest
{
	[TestFixture]
	public class SimpleRiverEngineDotNetAccessTest
	{
		SimpleRiverEngineDotNetAccess _simpleRiverEngineDotNetAccess;
		String _filePath;

		[SetUp]
		public void Init()
		{
			_simpleRiverEngineDotNetAccess = new SimpleRiverEngineDotNetAccess();
            _filePath = @"..\..\";
        	_simpleRiverEngineDotNetAccess.Initialize(_filePath);
		}

		[TearDown]
		public void ClearUp()
		{
			
		}

		[Test]
		public void GetModelID()
		{
			try
			{
				_simpleRiverEngineDotNetAccess.Initialize(_filePath);
				Assert.AreEqual("The river Rhine",_simpleRiverEngineDotNetAccess.GetModelID());
				_simpleRiverEngineDotNetAccess.Finish();
			}
			catch(System.Exception e)
			{
				this.WriteException(e.Message);
				throw(e);
			}
		}
	}
}


Fig. 19. NUnit Userinterface with Simple River wrapper classes loaded

  • No labels