Dashboard > Documentation > Documentation > Getting Started - Running Test from Ant
  Documentation Log In | Sign Up   View a printable version of the current page.  
  Getting Started - Running Test from Ant
Added by Björn Granvik, last edited by Adam Skogman on Feb 04, 2006  (view change)
Labels: 
old

This guide is scheduled for a rewrite in DDSteps 1.1.

Introduction

DDSteps can be used in many configurations, for example Ant, Maven or an IDE (e.g. Eclipse). This guide will explain how to get DDSteps installed in an Ant environment. The guide can also be used if you already have an Ant file for your project and just want to add DDSteps test cases.

Installation

This section will explain how to install DDSteps and configure Ant to run JUnit tests.

1. Download and unzip the DDSteps distribution file to a directory of your choice, e.g. C:\Java\ddsteps-1.0. We'll call this directory %DDSTEPS_HOME% from now on.

2. Enable the JUnit optional Ant task (if you haven't done so already). We need this to run the JUnit tests from our Ant build file later. The easiest is to copy the %DDSTEPS_HOME%/lib/junit-3.8.1.jar file to your %ANT_HOME%\lib directory. (In Eclipse you'll need to add the file to the Ant run classpath. Click here for a ddstepsWeb:screen-shot.)

3. Create a project directory with a src and a conf sub-directories, e.g. C:/workspaces/ddsteps/Tutorial. The result should look something like:

4. DDSteps uses Commons Logging and (by default) Log4J for logging. We need to do some basic configuration to avoid ugly output to the console. Create a file named log4j.properties in the conf directory:

log4j.properties
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [ddstepsWeb:%t] %-5p %c - %m%n

# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN

5. Create an Ant build file (build.xml) in the project base directory.

<?xml version="1.0"?>
<project name="ddsteps-tutorial" basedir="." default="test">

	<!-- Set some basic properties -->
	<property name="ddsteps.home" value="C:/Java/ddsteps-0.6" />
	<property name="src.dir" value="src" />
	<property name="lib.dir" value="lib" />
	<property name="conf.dir" value="conf" />
	<property name="build.dir" value="classes" />
	<property name="testreports.dir" value="junit-reports" />
	<property name="testhtml.dir" value="${testreports.dir}/html" />


	<!-- Path for building test cases. Include all jars from DDSteps. -->
	<path id="build-classpath">
		<fileset dir="${ddsteps.home}">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<!-- Path for running test cases. The source directory is needed by -->
	<!-- DDSteps to find the Excel file containing the test data. -->
	<path id="test-classpath">
		<fileset dir="${ddsteps.home}">
			<include name="**/*.jar" />
		</fileset>
		<path location="${build.dir}" />
		<path location="${conf.dir}" />
		<path location="${src.dir}" />
	</path>
	
	<!-- Compiles our test case to classes directory. -->
	<target name="build">
		<delete dir="${build.dir}" />
		<mkdir dir="${build.dir}" />

		<javac destdir="${build.dir}" failonerror="true">
			<src path="${src.dir}" />
			<classpath refid="build-classpath" />
		</javac>
	</target>

	<!-- Runs our test case and generates a nice HTML report. -->
	<target name="test" depends="build">
		<delete dir="${testreports.dir}" />
		<delete dir="${testhtml.dir}" />
		<mkdir dir="${testreports.dir}" />
		<mkdir dir="${testhtml.dir}" />

		<junit printsummary="true" fork="yes">
			<formatter type="xml" />
			<test name="test.TutorialTest" todir="${testreports.dir}" />
			<classpath refid="test-classpath" />
		</junit>
		
		<junitreport todir="${testhtml.dir}">
			<fileset dir="${testreports.dir}">
				<include name="TEST-*.xml" />
			</fileset>
			<report format="frames" todir="${testhtml.dir}" />
		</junitreport>
	</target>
</project>

6. Now the project directory should look something like:

Create Test Class

This section explains how to create a simple test class.

1. Create a test class TutorialTest that inherits from DdTestCase.

TutorialTest.java
package test;

import org.ddsteps.DdTestCase;
import org.ddsteps.data.DataLoader;
import org.ddsteps.data.excel.CachingExcelDataLoader;

public class TutorialTest extends DdTestCase {
	
	private String text;
	private int someNumber;
	private boolean someBoolean;

	protected DataLoader createDataLoader() {
		return CachingExcelDataLoader.getInstance();
	}

	public int getSomeNumber() {
		return someNumber;
	}

	public String getText() {
	
		return text;
	}

	public boolean isSomeBoolean() {
		return someBoolean;
	}
	
	public void setSomeNumber(int number) {
		this.someNumber = number;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void setSomeBoolean(boolean someBoolean) {
		this.someBoolean = someBoolean;
	}
	
	// Test method
	public void testFoo() throws Exception{
		assertTrue("Fail on someNumber != 11", someNumber == 11);
		assertTrue("someBoolean can't be false", someBoolean);
	}
}

The class have three JavaBean properties that will be used by DDSteps to fill each test with data (text, someNumber, someBoolean).

Create Excel document

This section will explain how to create an Excel document that will be used as data container for our tests.

1. Create Excel document with the same name as the class, TutorialTest.xls in the same directory as the Java source file.

2. Fill the document with data as described in the picture below.

Note that the header names (text, someNumber, someBoolean) corresponds to the getters/setters in our class!

Run Tests

Run Ant from the project base directory. The output should look something like:

Note that three tests were run even though we only had one test class!

Open C:/workspaces/ddsteps/Tutorial/junit-reports/html/index.html to view a nice report.

Summary

This tutorial walked through a complete "Hello World" example of how to set up DDSteps and write data driven test cases based on DDSteps. DDSteps has more features that is described in this tutorial so get started and explore the world of data driven testing! You can download the source code used in this tutorial www:here. Note that you may need to change ddsteps.home property to point to your installation of DDSteps.

Are you enjoying Confluence? Please consider purchasing it today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2 Build:#512 Apr 26, 2006) - Bug/feature request - Contact Administrators