Information in this topic applies to desktop applications only. |
This topic describes how you can create and organize the unit tests of your .NET applications using TestComplete.
Preparing Application for Unit Testing
Calling Test Methods From TestComplete
Requirements
-
You must have a license for TestComplete Desktop module and the module’s plugins must be enabled in TestComplete.
-
Your .NET application must be compiled as Open.
-
You must make your test methods as object methods. This corresponds to the unit testing structure, in which one testing class serves for testing one or several objects in the tested application.
Note: | Due to their specifics, MSAA and UIA applications are not applicable to unit testing using TestComplete. |
Preparing Application for Unit Testing
To make testing methods available to TestComplete, add the TestComplete unit testing assembly to your .NET application:
-
Open your .NET project in Visual Studio.
-
Add the AutomatedQA.TestComplete.UnitTesting.dll assembly to the References list of your .NET project. By default, the assembly is in the <TestComplete>\Bin\Extensions folder. The assembly holds classes for calling unit test methods.
-
Set the Copy Local property of this assembly to True.
-
To make a tested class available to TestComplete, use the
UnitTesting.AddClass
method:C#
using TestComplete; // Add this directive to provide access to the UnitTesting object
…
// Add MyTestClass to unit testing
Type[] typearr = {typeof (MyTestClass)};
UnitTesting.AddClasses(typearr);Visual Basic .NET
Implements TestComplete ' Add this directive to provide access to the UnitTesting object
…
' Add MyTestClass to unit testing
Dim typearr As Type() = New Type() {GetType(MyTestClass)}
UnitTesting.AddClasses(typearr)The
AddClasses
method uses one parameter - an array of testing classes. In our example, the array holds the only element that refers to classMyTestClass
.
Calling Test Methods From TestComplete
After you compile your application, you can call test routines from TestComplete:
- Launch your .NET application.
-
In the application, call the
AddClasses
method to make the test classes and their methods available to TestComplete.Note: You can skip this step if you call the AddClasses
method in the constructor of the application form. -
In TestComplete, open your test project and add a TCUnitTest item to the project.
-
Open the added item in the TCUnitTest editor.
-
In the Process text box, click the ellipsis button and select the process started by your application in the subsequent dialog.
-
In the editor, specify the needed tests. To load all tests defined in the application, click Load. To add tests manually, click Add Test and enter the test routine name on the Tests lists. If needed, unite tests into groups.
-
Choose the test run mode:
- Automatically load a list of tests and run all tests - Select this option to run all tests defined in the application.
- Run selected tests only - Select this option to specify the list of test routines to be run.
- Automatically load a list of tests and run without selected tests - Select this option to run all tests defined in the application except for the tests selected on the Tests list.
-
Once you configure the test in your project, run it.
Important Notes:
-
When TestComplete runs a unit test, it first creates a new instance of the testing class that contains this routine and then calls the routine.
-
The testing class must have a constructor without parameters. Otherwise, TestComplete will not be able to create a new class instance.
-
TestComplete can call a test routine only if it does not take any parameters because there is currently no way to supply test routines with parameters.
Alternatives to TestComplete Unit Tests
As an alternative, you can create unit tests for your .NET applications by using the NUnit framework or MSTest tool and include the tests to your test project by using the NUnit or MSTest items correspondingly. See –
Samples
TestComplete includes a sample application that demonstrates unit testing .NET applications:
<TestComplete Samples>\Desktop\Unit Testing\C#
TestComplete project suite:
<TestComplete Samples>\Desktop\Unit Testing\C#\TCProject
Note: | If you do not have the sample, download the TestComplete Samples installation package from the support.smartbear.com/downloads/testcomplete/samples/ page of our website and run it. |