Click or drag to resize

Testing an IVI-C Driver

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-C drivers using standard Visual Studio unit test projects. This topic discusses these IVI-C driver unit testing features.

Important note Important

When testing an IVI-C driver using the Visual Studio debugger, whether it be inside a unit test or in a standalone application, there is a debugger setting that should be changed from its default value. From the Tools menu, choose Options to bring up the Options Dialog.

From the tree view on the left, choose Debugging. Uncheck the box labeled Enable property evaluation and other implicit function calls.

If this option is left at its default enabled setting, then the Visual Studio debugger will sporadically execute properties on the driver and other classes as you are debugging. This can trigger unexpected and undesired instrument communication and produce confusing results.

Creating an IVI-C Driver Unit Test

All of the unit test code for an IVI-C driver 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-C Driver Wizard runs. This is controlled by the Create unit test check box on the Projects page of the New IVI-C 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 Unit Test

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

<driverName>CDriverUnitTest

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

C++
// UnitTest.cpp
TEST_METHOD(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 function. The TEST_METHOD macro 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 a test failure is reported.

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 macros -- the TEST_METHOD_INITIALIZE macro and the TEST_METHOD_CLEANUP attribute. The code below shows two typical initialize and cleanup functions.

C++
// UnitTest.cpp
TEST_METHOD_INITIALIZE(Initialize)
{
    auto status = acme4321_InitWithOptions(g_resourceDesc, VI_TRUE, VI_TRUE, "QueryInstrStatus=True, Simulate=False, DriverSetup= ", &g_session);
    Assert::AreEqual(VI_SUCCESS, status);
}

TEST_METHOD_CLEANUP(Cleanup)
{
    if (g_session != VI_NULL)
    {
        auto status = acme4321_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. All unit test interaction is performed via the Test Explorer tool window.

Once the Test Explorerwindow 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.

See Also

Download a complete CHM version of this documentation here.