MSDN Magazine Launch Issue - February 15, 2008 - (Page 28) Before diving in, let me briefly describe the module under test so you’ll understand the simple custom test automation, and so you can create the module if you want to. (If you are familiar with creating COM objects using ATL, you may want to skip ahead to the next section.) My module is a Win32® C++ method named TriMax. Using Visual Studio, I created a new Project using a C++ ATL template and named it MyCOMLib. I accepted the ATL wizard’s default settings, which instruct Visual Studio to create a DLL. In the project’s Solution Explorer, I right-clicked on the project name and selected Add | Class from the context menu. Next, from the ATL category, I selected the ATL Simple Object Template. Then, in the ATL Simple Object Template wizard, I typed “MyMethods” into the C++ Short Name field, which automatically populated the remaining seven name fields (.h file as MyMethods.h, Class as CMyMethods, .cpp file as MyMethods.cpp, Coclass as MyMethods, Type as MyMethodsClass, Interface as IMyMethods, and ProgID as MyCOMLib.MyMethods). After finishing the wizard, I switched to the Class View winFigure 3 Simple Test Automation Script // testSimple.js WScript.Echo(“\nBegin test run\n”); WScript.Echo(“Testing TriMax() in MyCOMLib.dll\n”); var o = new ActiveXObject(“MyCOMLib.MyMethods”); var cases = new Array( “001:3*7*5:7”, “002:0*0*0:1”, “003:2*4*6:6” ); var allPassed = true; for (var i = 0; i y && x > z) *pRes = x; else if (y > z) *pRes = y; else *pRes = z; return S_OK; } I pressed Ctrl+Shift+B to perform a local build of my COM object. And, finally, I used Windows Explorer to verify that the resulting MyCOMLib.dll file was created successfully in the \MyCOMLib\ Debug subdirectory. As you can see, my TriMax method is unrealistically simple; I just want something to perform test automation against. Let me emphasize that even though the examples that follow use JavaScript test automation code to test a Win32 COM object, the principles of using Visual Studio Team System to manage custom test automation apply to any type of custom test automation (including C#, Visual Basic® .NET, Windows PowerShell®, and the like) targeting any type of system under test (including .NET managed libraries, Windows® form-based applications, ASP.NET Web applications, ASP.NET Web services, and so on). Wrapping a Simple Test Automation Script } WScript.Echo(“==============”); WScript.Echo(“Case ID = “ + id); WScript.Echo(“Inputs = “ + arg1 + “ “ + arg2 + “ “ + arg3); var actual = o.TriMax(arg1,arg2,arg3); WScript.Echo(“Expected = “ + expected); WScript.Echo(“Actual = “ + actual); if (actual == expected) { WScript.Echo(“Pass”); } else { WScript.Echo(“**FAIL**”); allPassed = false; } Now let’s look at the simple test automation script in Figure 3. Later I’ll show you how to integrate the script into VSTE for Testers and describe the advantages of doing so. My script begins by displaying a couple of messages using the Echo method of the Windows Script Host. Next I instantiate the COM object that contains my method under test: var o = new ActiveXObject(“MyCOMLib.MyMethods”); Because I am testing a classic COM object, I can reference the object using the object’s ProgID. Additionally, because I compiled the COM object on my test host machine, the object information was stored into my system registry. Note that if I had copied the MyCOMLib.dll file to my test host machine from a different machine, I would have needed to register the DLL by calling regsvr32.exe using the Run method of the WScript.Shell object. My example script stores test its case data internally: var cases = new Array( “001:3*7*5:7”, “002:0*0*0:1”, “003:2*4*6:6” ); var allPassed = true; WScript.Echo(“==============”); if (allPassed == true) { WScript.Echo(“\nAll test cases passed”); WScript.Quit(0); } else { WScript.Echo(“\nOne or more cases failed”); WScript.Quit(1); } As a general rule, it is better to store test case data externally to the test automation code. External storage allows test cases to be shared and modified more easily than internally stored data. However, internal data is simpler and sometimes a good design choice. 28 msdnmagazine Test Run http://ASP.NET http://ASP.NET
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.