JUnit Launcher (junit.jar)

The JUnit launcher allows interfacing with JUnit v3 or v4 tests.
It has been tested with JUnit v4.7.


Configuration

The junit.xml file is just a template and must NOT be edited. It's used by the system to build dynamically the form that the user will be able to fill in from the GUI when creating a custom execution configuration.


Parameter Description
General
Test root path This must indicate where are located all the JUnit tests.
This is a root path. Each test in XStudio has a canonical path that will be appended to this path.
This path MUST not include an ending slash.

Default value is: C:/build/classes
Additional classpath This must indicate potential additional jar classpath necessary to run the tests. This can contain several path separated by ‘;’ each pointing to either a .jar file or a directory containing .class files.

Default value is: <empty> but could be similar to C:/tests/librairies/lib1.jar;C:/tests/librairies/lib2.jar;C:/tests/classes
JUnit
JUnit version This must indicate the version of JUnit. Indeed, JUnit v3.x and JUnit v4.x are using different mechanism to run the tests.

Default value is: JUnit4
Java install path This must indicate the path to the java install.

Default value is: C:/Program Files/Java/jdk1.6.0_06
JUnit jar path This must indicate the path to the JUnit library.

Default value is: C:/Program Files/junit4.7/junit-4.7.jar


These values can be changed while creating the campaign session from XStudio.


Process

The tests are executed by the launcher using this syntax:


For JUnitv3:
"<javaInstallPath>/bin/java.exe" –classpath "<junitJarPath>;<additionalClassPath>;<testRootPath>"
junit.textui.TestRunner <testPath>.<testName>


For JUnitv4:
"<javaInstallPath>/bin/java.exe" –classpath "<junitJarPath>;<additionalClassPath>;<testRootPath>"
org.junit.runner.JUnitCore <testPath>.<testName>


And this is executed from the working directory <testRootPath>


Attachments

The useful files generated during test execution are uploaded to XStudio so that you can open/read them afterwards:
  • console capture (stored by the launcher in <testRootPath>/<testPath>/junit_traces.txt)


Results

The test will be marked as passed or failed depending on the log file generated by JUnit. The text file is parsed by the launcher. The log and the execution trace of the command are also attached to the testcase execution in XStudio.


Bonus: if your JUnit test is generating outputs on the console, you can prefix some of these outputs with either [Success], [Failure] or [Log]. These special lines will be interpreted by the launcher as "steps" in the test and these messages will be reported as such in XStudio.



Tutorial: Creating and executing JUnit tests

In this tutorial, we will learn to run some JUnit test scripts.


Prerequisites

Install JUnit 4.7 in the folder C:\\Program files\\JUnit4.7



Create a dedicated category for JUnit tests and create a test

  • create a category JUnit associated to the launcher junit.jar
  • under this category, create (somewhere in the tree) a test with name MoneyTest and with a canonical path set to org.junit.samples.money.



Creating a test campaign

  • create a campaign including only the test MoneyTest
  • create a campaign session specifying in the configuration:
    • Test root path: C:/Program Files/junit4.7 (in this case, the test root path must be set this way because the test MoneyTest is part of JUnit and physically stored at the location: C:/Program files/Junit4.7/org/junit/samples/money/Moneytest).
    • Additional classpath: <empty>
    • JUnit version: JUnit4
    • Java install path: C:/Program Files/Java/jdk1.6.0_06
    • JUnit jar path: C:/Program Files/junit4.7/junit-4.7.jar



Run a campaign session

Run the campaign session



Permissions

WARNING: if you're running your tests on Windows, it may be required to run the tests as administrator.
Having an account with Administrators permissions may even not be enough in some cases (especially if you're using Windows 10) and you may need to disable completely the UAC (User Access Control) on your computer.

To do so:
  • Press the Windows + R key combination
  • Type in regedit
  • Go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System
  • In the right-side pane, look for EnableLUA and set the value 0
  • Close the registry editor
  • Restart your computer



Debug

If your tests are not executed correctly or are reporting only failures, this is very likely because your configuration is incorrect or because you used a wrong naming convention for your tests and test cases.


The best way to quickly find out what's wrong is to look at the traces generated by XStudio (or XAgent).
The traces always include the detailed description of what the launcher performs (command line execution, script execution, API calling etc.) to run a test case. So, if you experiment some problems, the first thing to do is to activate the traces and look at what's happening when you run your tests.


Then, try to execute manually in a cmd box the exact same commands.
This will normally fail the same way.
At this point, you needs to figure out what has to be changed in these commands in order to have them run properly.

When you have something working, compare these commands to what's described in the Process chapter above. This will tell you exactly what you need to change.


Most of the time, this is related to:
  • some incorrect values in some parameters of your configuration,
  • the name of your tests,
  • the name of your test cases,
  • the canonical path of your tests