Writing Your First Testcase

General

This page describes how you write a simple testcase and register it in a TestCase4j Server installation.

Preparations

  1. Install TestCase4j Server by following the tutorial: Installing TestCase4j Server

Your first test case

  1. Create a new Java class for your test case, make sure it implements org.testcase4j.core.TestCase, e.g. com.mydomain.MyFirstTestCase.java
  2. Implement at least one testcase step in your class
    • It must start with prefix execute
    • It must take one parameter of type org.testcase4j.core.TestCaseStepConfiguration
    • It must return a instance of com.testcase4j.core.Result
  3. Package your compiled test case .class file in a JAR file, e.g. MyTestCases-0.1.jar

Example

Below is an example implementation of a stateless test case with three steps. Each step only prints information about its name.

package com.mydomain;
 
import org.apache.log4j.Logger;
import org.testcase4j.core.Result;
import org.testcase4j.core.TestCase;
import org.testcase4j.core.configuration.TestCaseConfiguration;
import org.testcase4j.core.configuration.TestCaseStepConfiguration;
 
/**
 * 
 */
public class MyFirstTestCase implements TestCase
{
 
    /** */
    private static Logger logger = Logger.getLogger(MyFirstTestCase.class);
 
    /** */
    private TestCaseConfiguration config;
 
    public void init(TestCaseConfiguration config)
    {
        this.config = config;
        logger.info("Running init for test case: " + config.getName());
    }
 
    public void onBefore()
    {
        logger.info("Running onBefore for test case: " + config.getName());
    }
 
    public void onAfter()
    {
        logger.info("Running onAfter for test case: " + config.getName());
    }
 
    public Result executeStepA(TestCaseStepConfiguration config)
    {
        logger.info("Executing step A");
 
        return Result.createOK();
    }
 
    public Result executeStepB(TestCaseStepConfiguration config)
    {
        logger.info("Executing Step B");
 
        return Result.createOK();
    }
 
    public Result executeStepC(TestCaseStepConfiguration config)
    {
        logger.info("Executing step C");
 
        return Result.createOK();
    }
}

Register your test case in TestCase4j Server

  1. Configure your test case in TestCase4j Server by defining it in the test case configuration
    • Assign a name to your test case
    • Make sure that the class-loader-URL tag contains a entry for your test case JAR
    • Make sure the class tag match the fully-qualified name of your test case class, e.g. com.mydomain.MyFirstTestCase
  2. Start the server
    • By default, the testcase configuration file used by TestCase4j Server is located in the /conf directory of where TestCase4j Server was installed
<!DOCTYPE testcase4j-testcases PUBLIC
       "-//testcase4j//DTD testcase4j test cases configuration 1.0//EN"
       "/org/testcase4j/core/resources/testcase4j-testcases-config_1_0.dtd">
 
<testcase4j-testcases> 
  <class-loader-urls>
    <url>file:/home/MyFirstTestCase.jar</url>
  </class-loader-urls>
  <testcase-logger-config appenderDatePattern="'.'yyyy-MM-dd'.log'" 
                                   baseDirectory="/tmp/testcase4j/logs"
                                   layoutPattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %x - %m %n"
                                   logLevel="INFO"/>
  <testcases>
      <testcase name="MyFirstStatelessTestCase"
                    class-name="com.mydomain.MyFirstTestCase">
          <steps>
              <step name="StepA" timeout="30000"/>
              <step name="StepB" timeout="30000"/>
              <step name="StepC" timeout="30000"/>
          </steps>
      </testcase>
  </testcases>
<testcase4j-testcases>
  • Once you save the configuration, TestCase4j Sever will pick it up (happens by default every 20 seconds) and register it in the server. You may then invoke it
    • For easy testing you may use a Telnet client to execute it. Establish a telnet connection to localhost on port 2000 and issue the command EXECUTE(<your test case name>)[enter]
Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.