This topic describes how to create Self-Testing Applications with the following compilers:
- Visual C++ 6.0
- Visual C++ 7.x (unmanaged code)
- Visual C++ 2005 (unmanaged code)
- Visual C++ 2008 (unmanaged code)
- Borland C++Builder 6.0
- CodeGear C++Builder 2009
- Embarcadero C++Builder 2010 and XE
| Currently, it is not possible to create Self-Testing Applications in managed Visual C++. |
- Preparing your C++ project
- Writing test code in your C++ Self-Testing Application
- (Optional) Writing code in your TestComplete project
Preparing your C++ project
-
Open an existing project or create a new one in your development tool (C++Builder or Visual C++).
-
If you use Visual C++, disable optimization:
-
In Visual C++ 7.x, 2005 and 2008:
-
Right-click the project in the Solution Explorer and select Properties from the context menu to display the Project Properties dialog.
-
Switch to the Configuration Properties | C/C++ | Optimization node and select Disabled (/Od) from the Optimization combo box.
-
Click OK to close the dialog.
-
Right-click the project in the Solution Explorer and select Properties from the context menu to display the Project Properties dialog.
-
In Visual C++ 6.0:
-
Select Project | Settings to display the Project Settings dialog.
-
Switch to the C/C++ tabbed page.
-
Select Optimizations from the Category dropdown list.
-
Select Disable (Debug) from the Optimizations dropdown list.
-
Click OK to close the dialog.
-
Select Project | Settings to display the Project Settings dialog.
-
In Visual C++ 7.x, 2005 and 2008:
-
C++Builder users should disable the Code Guard:
-
Select Tools | CodeGuard Configuration from the main menu to display the CodeGuard Configuration dialog.
-
Uncheck the Enable box on the Preferences tabbed page.
-
Click OK to save the changes.
-
Select Tools | CodeGuard Configuration from the main menu to display the CodeGuard Configuration dialog.
-
Include the following header file in the source file(s) where you want to insert TestComplete C++ script procedures:
For instance,Header file Scripts <TestComplete>\Connected Apps\C++\script.h for Microsoft Visual C++ and Borland C++Builder
C++#include "..\..\..\Connected Apps\C++\script.h"
-
Place this directive before the script code in each file where you will add TestComplete scripts:
This will resolve a possible conflict of names (C++using namespace TestComplete;
Sys,Log, etc.)
Writing Test Code in C++ Self-Testing Applications
-
Include the following header file in the source file(s) where you want to insert TestComplete C++ script procedures:
For instance,Header file Scripts <TestComplete>\Connected Apps\C++\script.h for Microsoft Visual C++ and Borland C++Builder
For more information on the Script.h file, its classes and functions, see Creating Connected Applications in C++.C++#include "..\..\..\Connected Apps\C++\script.h"
-
Place this directive before the script code in each file where you will add TestComplete scripts:
This will resolve a possible conflict of names (C++using namespace TestComplete;
Sys,Log, etc.)
-
Now, initialize the COM libraries by inserting a call to the
IMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTS_MTAmacro in the main thread of your application. For instance:
This macro is declared in the script.h file. It will initialize COM libraries and create global variables that provide access to TestComplete’s internal objects:C++...
using namespace TestComplete;
IMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTS_MTA
...
Log,Sys,Regions, etc.
In order to use this macro in Visual C++, you should either define the_WIN32_DCOMconstant or define the_WIN32_WINNTconstant to be greater than or equal to 0x0400.
You only have to use this macro once in the main thread of your code. If your application calls this macro twice, an error will occur.
If you use theIMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTS_MTAinitializes COM for the multi-threaded object concurrently (using theCOINIT_MULTITHREADEDvalue). If you have already initialized COM in your application for an apartment-threaded object concurrently (COINIT_APARTMENTTHREADED), you should use a similar macro -IMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTS. Otherwise, using theIMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTS_MTAmacro will cause an error. Also, if you have already initialized COM for a multi-threaded object concurrently, a call toIMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTSwill cause an error. It is recommended to use a multi-threaded model (thexxxx_MTAmacro).IMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTSmacro (notxxxx_MTA), or if you do not use any of the macros at all, you should initialize COM yourself (see step 8).
-
Create a separate thread for the procedure to run in,
unless it does not call for onscreen inputs (e.g. button clicks, menu
selections, etc.) serviced by the current thread.
If you have not yet initialized COM using theIMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTS_MTAmacro in the main thread, you should initialize it now by using one of the following ways:
-
Place a call to the
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)function at the beginning of your thread, and a call toCoUninitialize()at the end of your thread.
-
Declare a variable of the
COleInittype. The classCOleInitis declared in script.h. Its constructor calls theCoInitializeExfunction, and the destructor callsCoUninitialize. The variable will be destroyed (the destructor will be called) when the thread finishes and COM will be uninitialized when testing is over.
IMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTSorIMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTS_MTAmacros initialize both COM libraries and global variables used to access TestComplete internal objects. If you do not use these macros, you may need to update your scripts.
-
Place a call to the
-
Copy one or more test routines from TestComplete C++
scripts to your application source. Of note, normally test routines use
the p and
wvariables. When imported to a C++ application, they become variables of thevartype. TheCOleInitvariable, which is used to initialize COM, should be declared before anyvarvariables, otherwise, an error will occur. If you do not use theCOleInitvariable, you should assignNULLto allvarvariables before COM is uninitialized. For example:
C++var p, w;
...
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
...
p = NULL; // <-- Correct
CoUninitialize();
w = NULL; // <-- Incorrect!!!
// This should be done before CoUninitialize
...
-
Write the test code into your C++ source, or copy one or more test functions from TestComplete’s C++ scripts.
The script.h file only contains variables for standard TestComplete objects (Sys,Log,Regions,Filesand others). If you need to work with objects that are provided by third-party plug-ins, you should obtain this object in your code by using theGetObjectByNamefunction that is defined in the script.h file:
C++[script.h]
var GetObjectByName(const BSTR ObjName)
For more information on writing test code, see Creating Connected Applications in C++. -
In order for the test code located in a Self-Testing
Application to run successfully, TestComplete must be in the
script-running mode. To run the test code, you should either launch the
Self-Testing Application from TestComplete’s script, or call the
RunTestroutine (see Running Connected and Self-Testing Applications for more information).
If you are going to use theRunTestroutine, insert a call to it before the first statement of the test code in your Self-Testing Application. To stop the test run initiated withRunTest, use theStopTestroutine:
If you are going to launch your Self-Testing Application from TestComplete, call the test procedure from your own code. For instance, you can call the test procedure within a button’sC++RunTest("My Test", "MyProject", "C:\\Work\\MyProjectSuite.pjs");
...
// Test code
...StopTest();
OnClickevent handler.
- Compile your application.
Writing Code in TestComplete Projects
In order for Self-Testing Application to be able to use
TestComplete’s scripting objects and post messages to the test log,
TestComplete must be in the script-running mode. The testing engine
switches to this mode when you run a test from TestComplete, or when you
call the
If you do not use the
RunTest routine (see Running Connected and Self-Testing Applications).If you do not use the
RunTest routine, you should launch your Self-Testing Application from TestComplete. To do this:-
Open your project in TestComplete.
-
Add your Self-Testing Application to the project’s tested applications list. For more information on how to do this, see Defining Applications to Test.
-
Write script code that will launch your Self-Testing Application and wait while it performs the testing actions. For instance:
-
Make sure this code is called when running the current TestComplete project.
-
As you can see, the script runs until the Self-Testing
Application is closed. In order to stop the script run from the
Self-Testing Application, use the
Runner.Stopmethod. This method notifies TestComplete that the test run is over and refreshes the Test Log so that it displays the latest test results.
OnClick event handler contains the test code.
"News Powered By"
No comments:
Post a Comment