TestComplete Unit Tests - .NET Applications

Applies to TestComplete 15.47, last modified on January 20, 2023
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.

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:

  1. Open your .NET project in Visual Studio.

  2. 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.

    Preparing .NET Project
  3. Set the Copy Local property of this assembly to True.

  4. 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 class MyTestClass.

Calling Test Methods From TestComplete

After you compile your application, you can call test routines from TestComplete:

  1. Launch your .NET application.
  2. 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.
  3. In TestComplete, open your test project and add a TCUnitTest item to the project.

  4. Open the added item in the TCUnitTest editor.

  5. In the Process text box, click the ellipsis button and select the process started by your application in the subsequent dialog.

    Specify the tested project
  6. 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.

    TCUnit Test item settings
  7. 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.
  8. 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/testcomplete/downloads/samples page of our website and run it.

See Also

Unit Testing
Unit Testing Samples

Highlight search results