Click or drag to resize

Testing an IVI-C Wrapper

One of the most important aspects of IVI driver development is authoring and maintaining a suite of driver tests. Nimbus provides integrated unit testing support for IVI-COM drivers and IVI-C wrappers. This topic discusses the IVI-C wrapper unit testing features while the topic Testing an IVI-COM Driver explains how to perform similar tasks with the underlying IVI-COM driver.

Creating an IVI-C Wrapper Unit Test

All of the unit test code for an IVI-C wrapper is housed in a single Visual C++ project created by Nimbus. Typically, this project is created upfront with the other driver projects after the New IVI-COM Driver Wizard runs. This is controlled by the Create IVI-C unit test check box on the Projects page of the New IVI-COM Driver Wizard.

If the IVI-C unit test project is not created by the wizard, then it can always be generated at any stage of driver development by right-clicking on the solution node in Solution Explorer and choosing Add New Project....

Structure of an IVI-C Wrapper Unit Test

The IVI-C wrapper unit tests are contained within the IVI-C unit test project, named as follows:

<driverName>CWrapperUnitTest

This Visual C++ project contains a single source file -- UnitTest.cpp. This source file contains one test function for each IVI-C wrapper function and attribute.

C++
// UnitTest.cpp
[TestMethod]
void ConfigureEdgeTriggerSource()
{
    ViStatus status = VI_SUCCESS;
    ViConstString Source = "External";
    ViReal64 Level = 0.1;
    ViInt32 Slope = ACME4321_VAL_NEGATIVE;
    status = acme4321_ConfigureEdgeTriggerSource(g_session, Source, Level, Slope);
    Assert::AreEqual(VI_SUCCESS, status);
}

The above code shows a test function for the ConfigureEdgeTriggerSource IVI-C wrapper function. The [TestMethod] attribute marks the function as being a unit test for the Visual Studio unit test execution engine.

The Assert::AreEqual method is used to check that the status returned from the ConfigureEdgeTriggerSource driver function is equal to VI_SUCCESS. If the comparison fails, then an exception is thrown and a test failure is reported in the Test Results window.

Test Initialization and Shutdown

The IVI-C unit test code includes two special functions known as the initialize and cleanup functions. These two functions appear at the top of the UnitTest.cpp file and are declared with two special attributes -- the [TestInitialize] attribute and the [TestCleanup] attribute. The code below shows two typical initialize and cleanup functions.

C++
// UnitTest.cpp
[TestInitialize]
void Initialize()
{
    ViStatus status = acm_InitWithOptions(g_resourceDesc, VI_TRUE, VI_TRUE, "QueryInstrStatus=True, Simulate=False, DriverSetup= ", &g_session);
    Assert::AreEqual(VI_SUCCESS, status);
};

[TestCleanup]
void Cleanup()
{
    if (g_session != VI_NULL)
    {
        ViStatus status = acm_close(g_session);
        Assert::AreEqual(VI_SUCCESS, status);
        g_session = VI_NULL;
    }
};

The initialize function executes before each test method in the project, while the cleanup function executes after each test method. The primary role of the initialize function is to initialze the driver by calling the InitWithOptions function. If the init call is successful, then the driver session is stored in the g_session global variable. All of the test functions use the g_session to execute driver functions. Correspondingly, the role of the cleanup function is to close the driver session by executing the driver's close function on the global g_session.

Drivers that require special configuration or setup prior to testing methods can add custom code to the setup function. Though less common, custom code can also be added to the teardown function.

Running Unit Tests

The unit tests generated by Nimbus integrate directly with Visual Studio's native unit testing features. The starting point for executing tests is to bring up the Visual Studio Test View, which is typically docked as a tab within Solution Explorer. To bring up Test View for executing unit tests, click on the Test menu, then choose Windows and select Test View.

Once the Test View window is shown, executing tests proceeds in the exact same fashion as any other Visual Studio unit test. For details on working with the Visual Studio unit testing features, see the MSDN topic Verifying Code by Using Unit Tests.

Tip Tip

Nimbus drivers typically contain a large number of unit tests. To work with the Test View effectively, use the Group By combo box, which offers a variety of choices for organizing the display of unit tests in the Test View. For Nimbus drivers, setting the Group By combo box to Class Name is often the most effective presentation of the unit test methods.

See Also

Download a complete CHM version of this documentation here.