Pramati Technologies

Working With Testing Tools

Overview

Testing tools are used in IDEs to create and run tests. The different types of testing frameworks are:

Unit Testing

Developers write unit tests to check a program for expected behavior. However, unit testing is not weaved into the development process. This prevents developers from measuring the progress of development. If a program feature lacks an automated unit test, it can be assumed that it does not work. The tests should demonstrate that the program works and:

Functionality Testing

Functionality or black box testing is generally performed within other test scripts to ensure that a product is performing within acceptable parameters. The major components of the product being tested are subjected to a work load and it is verified that any output that results from these tests is in agreement with the expected output if the product were working properly. A `pass' for each aspect of the series of tests indicates that the device functions as expected under heavy utilization. A `fail' indicates that the device could not serve a particular request.

Testing Tools in Studio

The testing tools provided along with Studio enables you to measure your progress, spot unintended side effects, and focus your development efforts. Studio features:

Studio simplifies the usage of these tools by eliminating the need to compile or run the test at the command prompt. This chapter describes how to use these tools.

What is JSP Test Tool

Studio features the JSP Test Tool for performing functionality tests. This tool tests web components such as HTMLs and JSPs. It can not be used to test Swing components. This is a capture replay tool, designed to exercise every operation that can be performed on that server, record the testcases, and display the results.

Recording in JSP Test Tool

The JSP Test Tool keeps a record of the series of actions performed as a part of a test case and compares the results when the test case is replayed at a later date. For example, the tool receives Req1 and sends it to the server. The response from the server is recorded and sent to the client.

If the same test case is run later, the tool checks the size of Res2 against the record of Res1. If the sizes differ, the result is marked as a failure.

JSP Test GLobal Settings

The JSP Test Global Settings panel enables the user to configure Server, Filter, and Error settings. The proxy server settings applied prior to the test, appear in the respective fields. To view the panel:

Server Settings

Table 1: Fields available in the Global Settings panel
Attribute
Description
Sever IP
Specifies the IP address of the server that hosts the test case
Server Port
Specifies the port at which the server receives the request
Capture Port
Specifies the port at which the JSP Test Tool runs

Filter Settings

If the user does not want the tool to record and compare graphical elements for a testcase, the file extensions such as *.gif, *.jpj, and *.bmp can be provided in the `Hide File with the following extensions'. To skip recording test conditions for certain elements, the extensions can be entered in the check errors for files with the following extensions text box can be used. These settings are optional.

Error Settings

While running a test, the tool verifies if the response received is the expected response. To display errors in the test case interface, the user can select from the following:

Using JSP Test Tool

To understand recording in JSP Test Tool, consider the following example:

    1. Open the sample BankDesk. Start the Server if it is not running already.
  1. Right click on the desk and select Deploy.
  2. Right click on BankDesk in the explore panel and select New Folder > JSP Test Module.

  3. Type name of the test module as Bank_test in the panel. The location of the test module is the desk, by default. Use the browse button to target another folder.
  4. Right click on the test module and select New Test. Type a Test Name as test1. Test Module is selected as Bank_test by default. This field is non-editable. Description is optional. Click OK.

  5. Click Settings in the Record panel.

    Attribute
    Description
    Start
    Starts the process of recording requests
    Stop
    Stops the process of sending requests. Can be used when a wrong operation is performed
    Pause
    Pauses in the middle of recording
    Close
    Closes the panel
    Settings
    Opens the Settings panel
    Tests
    Provides information on the test name, total number of requests sent, number of requests served successfully and number of requests not found
    Requests
    Provides the following informations:
    i) Get/Post: Type of request sent to the server.
    ii) Request: Lists the exact HTTP request made to the server.
    iii) Passed: Shows if the request has passed or failed during a replay.
  6. Use the Global Settings option which is selected by default. Select Customize Settings to apply different settings.
  7. Click Start button. Enter the initial URL as http://localhost:8181/bankWeb/bank.jsp.
  8. Perform some action on the JSP such as, creating a new account or transfer of money. The Record panel records details.
  9. Right click on the test in the explore panel and select replay. All the actions are performed again. The new HTTP requests are compared to the corresponding request recorded.
Note: If the requests match the record, a tick appears in the Record panel. If the string specified in `Page Error Info' panel is found, the test fails and a cross appears. Click on cross or tick to view details.
  1. Click any HTTP request listed in the record panel to view JSP details.

    Table 2:
    Attribute
    Description
    Base
    Displays the HTML page code recorded earlier
    Current
    Displays the HTML code for the current page
    HTML
    Displays the HTML page
  2. Double click on request to view the Request header panel.

    Table 3:
    Attribute
    Description
    Request Parameters
    Lists the information to be posted through that JSP and the related values. Enables to change the request parameters.
    Request Headers
    Provides information on type of image accepted, Referer, Accept-language, Proxy-Connection, User Agent, Host, Cookie
    Page Error Info
    Enables user to search for string literals in the HTML response.
    Specify the string to be searched for in the `Page Error Info' panel. For example, when it is specified that the response is an error if the specified string is found, the JSP Test Tool searches for the string in each JSP during a replay.
What is JUnit

JUnit is an open source Java testing framework used to write and run repeatable tests. It has the following features:

Read the section `Writing and running a JUnit test' that gives an example to explain how to use JUnit in Studio. To write and test a code, you should be familiar with the concept of a testcase, fixture, suite and test runner. These are briefly explained here.

For more information on JUnit, visit http://www.junit.org/news/article/index.htm.

Key terms

Simple Test Case

A test case is an object to test the basic concept in the code. It can be one of the following:

Test Fixture

A test fixture is a set of common initializer of objects based on which two or more tests operate. To write a test fixture:

Test Case

A test case extends the class Assert and implements the test.

In JUnit, to write a test against a fixture:

  1. Write the test case method in the fixture class. Make it public.
  2. Create an instance of the test case class and pass the name of the testcase() method to the constructor.

When the test is run, the name of the test is used to look up the method to run. When there are several tests, they can be organized into a Suite.

Suite

A TestSuite is an object that runs any number of test cases simultaneously.

Test Runner

Test Runner tool provided with JUnit defines the suite to be run and displays its results. A static method suite that returns a test suite, makes the suite accessible to the Test Runner.

Writing and running a test using JUnit

Before running a test using JUnit, download JUnit JAR, Junit3.8.1.zip from

http://www.junit.org/index.htm. To configure JUnit in Studio:

    1. Select File > IDE Options > Testing > JUnit. Specify the JUnit JAR location in the field.

  1. Select a test runner to view the output. The Text Based test runner opens a new tab in the output panel and displays the result in a text format. The SwingUI Runner opens a new panel and displays results in the dialog.

Creating and running a test

    1. Create a new blank desk, TestDesk. Create a Java module, Test and a Java file, com.Test1.
  1. Add one method to com.Test1. For example,
public int addNos(int a, int b) 
{ 
return (a+b); 
} 
  1. In the explore panel, right click on the Java file and select Generate JUnit Test Case.
  2. Retain the default class name, com.TestTest1 that appears in the JUnit Test Case panel. Select the methods setUp() and addNos and click OK.

  3. Give an appropriate argument in the test case. For example,
public void testAddNos()  
{ 
		myTest1.addNos( 1, 2); 
} 
  1. In the explore panel, right click on the test case and select Run JUnit Test. The results are displayed in the swingUI test runner, JUnit panel.

Viewing a test failure

Consider the following example of test failure:

Use an assertion in the test case as,

assertTrue ("Sum should not be negative",(myTestaddNos(1, -2)>0)); 

Run the Test. The result will appear as a failure. Details are also provided

What is Jakarta Cactus

Cactus is a test framework for unit testing server-side Java code for servlets, EJBs, Tag Libs, filters and JSPs. It extends JUnit and provides several redirectors that create proxy instances of the test on the server-side.

Studio simplifies working with Cactus by eliminating the following manual steps:

For more information on Jakarta Cactus, read the Jakarta Cactus documentation.

Writing and running a test Using Cactus

Before writing a test, download the Cactus JARs. For more information, visit

http://jakarta.apache.org/cactus/getting_started.html. Configure Cactus in Studio as follows:

    1. Select File > IDE Options > Testing > Cactus from the main menu.
  1. In the dialog, provide the location the Cactus JAR files.
  2. Select the Browser Based test runner and save settings.
Note: The swingUI and text based test runners will be made available at a later date.

For this example, consider the desk, TestDesk created for JUnit test.

Using Cactus to run a EJB test case

    1. Create a new EJB module, EJBMod, a web module, WebMod, and a Java module JavaMod.
  1. Create stateless session bean, TestBean.
  2. Write a business method addNos and compile the file. Check in the remote interface, the business method should show there. If not, add the method manually.
public void int addNos(int a,int b) { 
        return a+b; 
    } 
  1. In the explore panel, right click on TestBean and select Generate Cactus Test Cases.
  2. Name the test case as com.pramati.TestCase2. Select the source module as Java module. Select setup() and lookupcode() of framework methods, and business method addNos. Select all begin() an end() methods and click OK. When the test is run, the sequence of execution is, begin(), addNos method, and end ().

Note: If for a test case, some external parameters are required, the parameters can be initialized in begin (). For more information on begin and end methods, visit http://jakarta.apache.org/cactus/getting_started.html.
  1. Give appropriate argument and compile the test case. For example,
public void Test addNos() 
{ 
myTestLocal.addNos(1,2); 
} 
  1. In the explore panel, right click on the test case and select Run Cactus Test. If Server is not already started, it starts on selecting Run Cactus Test and deploys the EJB module and web module.

The result is displayed in a browser in XML format.

To obtain the result in HTML format, copy the Cactus XLS file in the web module and run the test again. For more information on this, read instructions in http://jakarta.apache.org/cactus/integration/integration_browser.html.


Pramati Technologies  © Copyright   TOCPREVNEXTINDEX