Working with Test Scripts

prevnext

Running Test Scripts


You can run test scripts either from within or outside of TestManager. Test scripts that you execute from within TestManager can run on the local host or on an agent host.

Where you run a test script depends, in part, upon your reason for running it:

In order to run a test script from TestManager that was generated by QualityArchitect, you must include in your classpath the full paths:

For other test scripts containing only Rational classes that are run from TestManager, you do not need to modify your classpath. This is true whether the test script executes on the local host or on an agent. You do not have to copy any files to the agent or modify its classpath.

For test scripts containing Rational classes that are run outside TestManager, their full pathnames must be specified in your classpath. The following table lists the relevant .jar files, their default paths, and product(s) that use them.

File Installed Location Required for
rational_ct.jar Rational Test\QualityArchitect QualityArchitect
rttseajava.jar Rational Test\tsea QualityArchitect
TestManager
rttssjava.jar Rational Test QualityArchitect
TestManager
swingall.jar Rational Test\QualityArchitect QualityArchitect

For test scripts containing private classes (classes that are unknown to TestManager or QualityArchitect), the full pathnames of these must be specified in your classpath. This is true whether the test scripts execute within or outside TestManager. In addition, for test scripts executed from TestManager that run on an agent, the .jar files must be present on the agent, and their full paths must be specified in the agent's classpath.


Specifying the JVM Location

When TestManager plays back a Java test script, it must be able to load a Java Virtual Machine (jvm.dll or jvm.so). You must have either a Sun JDK or Sun JRE installed. To play back test scripts on an agent computer, a JRE is sufficient. To play back test scripts on the local computer, a JDK is required.

On Windows systems, TestManager finds the currently installed Java Virtual Machine (JVM) by looking at the following registry key:

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
\CurrentVersion

This registry key is created by the installation programs of both the Sun JDK and the Sun JRE.

To override the most recently installed JDK/JRE, you can create an environment variable called RATL_JVM_LOCATION and assign it to the fully qualified path of the JVM library (jvm.dll for JDK 1.2 and above, or javai.dll for JDK.1.x).

On UNIX systems, the location of the JVM must be specified using the library environment variable appropriate for your system. Thus, on Solaris, you must indicate the location of the JVM using the environment variable LD_LIBRARY_PATH. For example:
/usr/jdk1.2.2/jre/lib/sparc/classic:/usr/jdk1.2.2/jre/lib/sparc/native_threads

If you fail to specify the location of the JVM, and are unable to locate it relative to the installation of the JDK, you receive the following error message at playback:

[1.1.5.172] Execution Adaptor rttseajava: couldn\qt retrieve test 
script info for SabreClient (datapath = c:\testscripts): [1.3.9.259]: 
Java() failed
Not able to find or start JVM (Java Virtual Machine), library jvm or 
javai
[1.1.4.52] RTmaster run not executed due to fatal workload description 
errors.
*** RTmaster Exiting (1 error, 0 warnings)

Running Test Scripts in a TestManager Suite

A TestManager suite is a collection of test scripts. In TestManager, you typically run tests by running a single script or a number of scripts in a suite.

You can combine scripts of different types in the same suite -- for example, you can add your Java scripts to a suite that also contains Visual Basic, GUI, and VU scripts, and even scripts of a custom test type.

For information about adding scripts to a TestManager suite, see Rational TestManager User's Guide.

A .java test script that you want to run inside a suite must extend the TestScript class and implement the testMain() method. This method is the entry point for the class. Additionally, your test script code should appear between tms.startTestServices() and tms.endTestServices() calls. (tms is a protected member variable of the TestScript class.)

The following is an example of a skeletal .java test script that includes the testMain() method and service start/end calls, shown in bold type:

import java.io.*;
import com.rational.test.tss.*;

public class Hello extends com.rational.test.tss.TestScript {

	 public void testMain(String[] args) {
	 	 try{
	 	 	 	 tms.startTestServices();

	 	 	 	 // Your test script code goes here
	 	 }
	 	 catch{

	 	 	 	 // handle exceptions
	 	 }
	 	 finally{
	 	 	 	 	 tms.endTestServices();
	 	 }
	 }
}

Adding a Source Folder for Java Scripts

For TestManager to run a Java script, you must create a Java test script source folder (if one doesn't already exist for the current folder) and place the script into it. At suite runtime, TestManager compiles the script, places the resulting .class file in the same folder, and then executes the .class file.

For example, suppose you want to manually create and code a script named Script1.java and run it from a folder named D:\TestScripts, which doesn't exist. You would do the following:

  1. Create the folder D:\TestScripts.

  2. Create the script Script1.java and save it to D:\TestScripts.

  3. Register the test script source folder D:\TestScripts with TestManager. For information, see Registering Test Script Source Folders.

  4. Add Script1.java to a TestManager suite and run the suite.

When the suite is run, TestManager compiles Script1.java, places the resulting .class file in D:\TestScripts, and executes Script1.


Adding a Script Contained in a Java Package

In Java, a package lets you assign a single name to a group of related classes.

If you want TestManager to run a Java test script that is part of a package, the source and the .class runtime must both be located in an appropriate folder below the test script source folder. The folder's path name is determined by the name of the test script source folder plus the name of the package.

For example, suppose you want to manually create and run a script named Script1, which is located in the package com.rational.test. You want to run the script from a folder named D:\TestScripts, which doesn't exist. You would do the following:

  1. Create the folder D:\TestScripts.

  2. Create the folders \com\rational\test below the test script source folder D:\TestScripts.

  3. Place the script Script1.java in D:\TestScripts\com\rational\test.

  4. Register the test script source folder D:\TestScripts with TestManager. For information, see Registering Test Script Source Folders.

  5. Add Script1.java to a TestManager suite and run the suite.

When you run the suite, TestManager compiles Script1.java, places the resulting .class file in D:\TestScripts\com\rational\test, and executes Script1 using the class name com.rational.test.Script1.


Running Test Scripts Outside TestManager

There are two motivations for running test scripts outside TestManager:


Running Test Scripts from Your IDE

A test script that you want to run from your IDE must include a main() entry point as well as a testMain() entry point.

The following example extends the previous example on page11 by including the code, shown in bold type, required for running and debugging the script in your IDE:

import java.io.*;
import com.rational.test.tss.*;

public class Hello extends com.rational.test.tss.TestScript {

	 public static void main(String[] args) {
	  	 Hello h = new Hello();
	 	 h.testMain(args);
	 }

	 public void testMain(String[] args) {
	 	 try{
	 	 	 	 tms.startTestServices();

	 	 	 	 // Your test script code goes here
	 	 }
	 	 catch{

	 	 	 	 // handle exceptions
	 	 }
	 	 finally{
	 	 	 	 	 tms.endTestServices();
	 	 }
	 }
}

The following example further extends the skeletal test script shown above. This example illustrates the inclusion of Test Script Services calls and the creation of a debug file usable from your IDE.

import java.io.*;
import com.rational.test.tss.*;

public class Hello extends com.rational.test.tss.TestScript {
	 public static void main(String[] args) {
	 	 Hello h = new Hello();
	 	 h.testMain(args);
	 }
	 public void testMain(String[] args) {
	 	 try {
	 	 	 tms.startTestServices();
	 	 	 FileOutputStream debugfile = new
	 	 	 FileOutputStream("Hello.dat",true);
	 	 	 PrintStream deb = new PrintStream(debugFile);
	 	 	 deb.println("Hello World");
	 	 	 System.out.println("Hello World");
	 	 	 System.out.println("Starting first sleep for 5 seconds");
	 	 	 TSSMeasure.commandStart("string1", "string1", 0);
	 	 	 Thread.sleep(5000);
	 	 	 com.rational.test.tss.TSSNamedValue[] a = null;
	 	 	 TSSMeasure.commandEnd((short) 0,"string1",0,0,"string2",a);
	 	 	 TSSMeasure.think();
	 	 	 System.out.println("Starting first sleep for 1 second");
	 	 	 Thread.sleep(1000);
	 	 	 System.out.println("Hello World done");
	 	 }
	 	 catch {
	 	 	 	 	 System.out.println("Exception has occurred");
	 	 }
	 	 finally {
	 	 	 	 	 tms.endTestServices();
	 	 }
	 }
}

Accessing Test Script Services from an External Application

When you run a test script from TestManager, it links with rttssjava.dll and is executed directly by the TestManager Test Script Execution Engine (TSEE). This is the usual, and simpler, way to run Java test scripts.

Optionally, you can add Test Script Services method calls to an external application that logs data to the TestManager datastore. In this case, the application links with rttssremotejava.dll and is executed by a separate process (proxy TSS server) that communicates with the TestManager TSEE. This advanced technique can be used to support threaded execution.

The following example illustrates how to link a Java program with rttssremotejava.dll, and how to connect an external program to a TestManager TSS listener port. The lines that perform these functions appear in bold.

/**
 * TSSRemoteorDirect is a Java test script that can be executed within
 * TestManager, or detached from TestManager. The script demonstrates
 * some typical Test Script Services by executing and measuring
 * two delay calls.

import java.io.*;
import com.rational.test.tss.*;

public class TSSRemoteorDirect extends 
com.rational.test.tss.TestScript {


    // BEGIN REQUIRED FOR TSS REMOTE

    public boolean tssConnect() {
        try {
            String host = TSSUtility.getEnv("RTTSS_HOST");
            Integer port = new 
Integer(TSSUtility.getEnv("RTTSS_PORT"));
            Integer vtid = new 
Integer(TSSUtility.getEnv("RTTSS_VTID"));
            int rc = TSSSession.connect(host,
                                        port.intValue(),
                                        vtid.intValue());
            if (rc == 0)
                return true;
            else return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    // END REQUIRED FOR TSS REMOTE


    public void testMain(String[] args) {
        try {
            com.rational.test.tss.TSSNamedValue[] b = null;


            // BEGIN REQUIRED FOR TSS REMOTE

            String remote = System.getProperty("rt.tssjava.remote");
            if (remote != null && remote.equals("true"))
                tssConnect();
            // END REQUIRED FOR TSS REMOTE


            TSSMeasure.commandStart("step001", "step001", MST_DELAY);
            TSSUtility.delay(2000);
            TSSMeasure.commandEnd(TSS_LOG_RESULT_PASS,
                                  "fail",
                                  0,
                                  0,
                                  "Delayed 2 seconds",
                                  b);

            TSSMeasure.commandStart("step002", "step002", MST_DELAY);
            TSSUtility.delay(3000);
            TSSMeasure.commandEnd(TSS_LOG_RESULT_PASS,
                                  "fail",
                                  0,
                                  0,
                                  "Delayed 3 seconds",
                                  b);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        TSSRemoteorDirect h = new TSSRemoteorDirect();
        h.testMain(args);
    }
}


Running an External Program that Accesses TSS

The program shown in the previous section can be executed either directly from TestManager or as an external application. To execute the program directly from TestManager:

Running an external Java program that accesses the TSS classes and methods documented in Chapter 3 requires these addional measures:

To run the sample program TSSRemoteorDirect.java using TestManager's built-in Command Line exection adapter, follow these steps:

  1. From TestManager, select File > Run Test Script > Command Line. The Specify Command Line dialog box opens. Alternatively, you can reach this dialog box by inserting a Command Line test script into a performance test suite. In this case, the test script is run whenever the suite it belongs to is run.

  2. In File to run from Command Line, enter:

    java.exe
    
  3. In the Arguments box, enter:

    -Drt.tssjava.remote=true -cp "d:\program files\rational\rational 
    test\rttssjava.jar;d:\testscripts\java" TSSRemoteorDirect
    
  4. Click OK.

prevnext


Rational Test Script Services for Java Rational Software Corporation
Copyright (c) 2003, Rational Software Corporation http://www.rational.com
support@rational.com
info@rational.com