Running JUnit and TestNG Unit Tests and Selenium Tests Created With JUnit or TestNG

Applies to TestComplete 14.72, last modified on April 22, 2021

In TestComplete, you can run JUnit and TestNG unit tests and Selenium tests created by using JUNit and TestNG testing framework as part of your TestComplete project. In the Java Unit Test editor, you configure the tests to be run.

To learn about TestComplete support for third-party unit testing frameworks, see:

Integration With Unit Testing Frameworks

To learn about TestComplete support for Selenium WebDriver tests, see:

Integration With Selenium

Requirements

To run JUnit and TestNG tests in TestComplete:

  • Specify the full path to the Java Virtual Machine executable (java.exe) in the Unit Testing Options dialog.

  • If you want to use Maven to run your tests, the Maven modules must be installed and configured on your computer. For information on how to do this, see the Maven documentation.

  • To run Selenium tests, make sure that your computer and TestComplete project meet the requirements described in Integration With Selenium - Prerequisites.

1. Prepare Your Test Machine, TestComplete and Tests
  • Make sure that the computer where you will run tests, TestComplete and your tests are prepared for testing.

    To learn how to prepare Selenium tests for testing, see –

    Integration With Selenium - Prerequisites

  • Copy your test files to your TestComplete computer.

2. Configure Your TestComplete Project
  • Open your TestComplete test project or create a new one.

  • Add a Selenium or Unit Testing collection to your test project. See Adding and Removing Project Items and Their Child Elements.

  • Add a JUnit item or the TestNG item to the created collection.

    Added JUnit Item

    JUnit Item

    Click the image to enlarge it.

    Added TestNG Item

    TestNG item

    Click the image to enlarge it.

3. Configure the Added Item to Run Needed Tests
  • Select how you will run your tests:

    • To run your tests by using Maven, select Use Maven project.

      Enter the path to the Maven project configuration file (pom.xml) or click the ellipsis button and browse for the needed file in the subsequent dialog.

      Run JUnit Tests by Using Maven

      Run JUnit Selenium tests by using Maven

      Click the image to enlarge it.

      Run TestNG Tests by Using Maven

      Run TestNG Selenium tests by using Maven

      Click the image to enlarge it.

    • To run your tests using the classpath, select Specify the classpath directly.

      Enter not only your tests' classpaths, but also the paths to all modules and libraries your tests use:

      • For JUnit tests, the path to the junit-n.nn.jar file. For example, C:\JUnit\junit-4.12.jar.

        For TestNG tests, the path to the testng-n.n.n.jar file. For example, C:\TestNG\testng-6.8.8.jar.

      • The paths to the classes that define tests to run.

        Note: The full path to a Java class file (.class) reflects the class’s package name. For example, the com.smartbear.seltest.AppTestClass class is in the <classes>\com\smartbear\seltest folder.

        The class path is the name of the top-level folder that contains folders whose names indicate to which package they belong. In this case, the class path is <classes>.

        For example, if the files that define your test classes are in the C:\JUnitSeleniumTests\target\classes folder, specify the path to that folder.

      • For Selenium tests, the paths to the Selenium modules that your tests use. For example, C:\Selenium\selenium-java-2.43.1.jar.

      • The paths to the classes that your tests use.

      The examples below demonstrate how you can specify a classpath:

      Run JUnit Tests by Classpath

      C:\JUnit\junit-4.12.jar;C:\JUnit\hamcrest-core-1.3.jar;C:\Tests\SeleniumJUnitTests\target\classes;C:\Selenium\selenium-java-2.43.jar;
      Run JUnit Selenium Tests by classpath

      Click the image to enlarge it.

      Run TestNG Tests by Classpath

      C:\TestNG\testng-6.8.8.jar;C:\Tests\SeleniumTestNGTests\target\classes;C:\Selenium\selenium-java-2.43.jar;

      Run TestNG Selenium Tests by specifying classpath

      Click the image to enlarge it.

  • Specify the test classes you want to run:

    • To run all the test classes defined in your test, select Run all tests. Note that this mode is available only if you run your tests by using Maven.

    • To run only specific test classes, select Run the following test classes. In the test list, enter the test classes to run.

      JUnit Test Classes to Run

      Selenium test classes to run

      Click the image to enlarge it.

      TestNG Test Classes to Run

      Selenium test classes to run

      Click the image to enlarge it.

      To add a test class to the list, click Add and enter the needed test class name. For example, com.smartbear.seltest.AppTestClass or TestCaseClass1.

      Tip: If you do not know the test class names, contact the developer who created the test. As an alternative, you can explore the source files of your tests and find all the classes whose methods use the @Test annotation.

      To remove the selected test class from the list, click Remove. To temporary exclude a test class from a test run, clear the check box next to the test class name in the list.

4. Run the Test Item

Run the test item in TestComplete. For information on how to run unit test items, see Running Unit Tests.

TestComplete will command the appropriate unit testing framework to run the specified tests.

After the test run is over, view the test results.

JUnit Test Results

Selenium Test Results

Click the image to enlarge it.

TestNG Test Results

Selenium test results

Click the image to enlarge it.

Alternatives to JUnit and TestNG Tests

Another way to test units of your Java application is to create unit tests by using TestComplete and use TCUnitTest project item. See TestComplete Unit Tests - Java Applications.

See Also

Integration With Unit Testing Frameworks
Running Unit Tests
Integration With Selenium
Unit Testing Options Dialog
TestComplete Unit Tests - Java Applications

Highlight search results