现在的构建工具有ant和maven,其实maven都自带了ant类似的功能,如果项目不是maven来管理,一般的项目需要自动化测试一般会结合jenkins和ant配置,jenkins安装网上还是蛮多的,我就不一一叙述啦。ant工具最核心的就是build.xml,基本上都是围绕这个文件来配置的。
1、ant+junit自动化测试配置
ant的build.xml配置如下
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--变量设置 -->
<project basedir="." default="build" name="demo">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.8"/>
<property name="source" value="1.8"/>
<!-- 生成报告junit4.xml路径 -->
<property name="report.path" value="report"/>
<property name="buildTest.path" value="target/test-classes"/>
<property name="maven.path" value="自己的maven本地库地址"/>
<!-- 由于测试的例子使用eclipse maven生成 的build.xml文件,所以会有maven的影子 -->
<path id="Maven Dependencies.libraryclasspath">
<pathelement location="${maven.path}/.m2/repository/org/glassfish/jersey/containers/jersey-container-servlet-core/2.22.2/jersey-container-servlet-core-2.22.2.jar"/>
<pathelement location="${maven.path}/.m2/repository/org/glassfish/hk2/external/javax.inject/2.4.0-b34/javax.inject-2.4.0-b34.jar"/>
<pathelement location="${maven.path}/.m2/repository/org/glassfish/jersey/core/jersey-common/2.22.2/jersey-common-2.22.2.jar"/>
......
</path>
<path id="demo.classpath">
<pathelement location="target/classes"/>
<pathelement location="target/test-classes"/>
<path refid="Maven Dependencies.libraryclasspath"/>
</path>
<path id="run.SimpleTest (1).classpath">
<path refid="demo.classpath"/>
<path refid="Maven Dependencies.libraryclasspath"/>
</path>
<target name="init">
<mkdir dir="target/classes"/>
<mkdir dir="target/test-classes"/>
<mkdir dir="${report.path}"/>
<copy includeemptydirs="false" todir="target/classes">
<fileset dir="src/main/java">
<include name="**/*.java"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
<copy includeemptydirs="false" todir="target/classes">
<fileset dir="src/main/resources">
<exclude name="**/*.java"/>
<exclude name="**"/>
</fileset>
</copy>
<copy includeemptydirs="false" todir="target/test-classes">
<fileset dir="src/test/java">
<exclude name="**/*.java"/>
</fileset>
</copy>
<copy includeemptydirs="false" todir="target/test-classes">
<fileset dir="src/test/resources">
<exclude name="**/*.java"/>
<exclude name="**"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="target/classes"/>
<delete dir="target/test-classes"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="target/classes" includeantruntime="false" source="${source}" target="${target}" encoding="UTF-8">
<compilerarg line="-encoding UTF-8"/>
<src path="src/main/java"/>
<src path="src/main/resources"/>
<include name="**/*.java"/>
<classpath refid="trc-couponToB.classpath"/>
</javac>
<javac debug="true" debuglevel="${debuglevel}" destdir="target/test-classes" includeantruntime="false" source="${source}" target="${target}" encoding="UTF-8">
<compilerarg line="-encoding UTF-8"/>
<src path="src/test/java"/>
<src path="src/test/resources"/>
<classpath refid="trc-couponToB.classpath"/>
</javac>
</target>
<target name="SimpleTest">
<java classname="com.xxx.xxx.test.SimpleTest" failonerror="true" fork="yes">
<classpath refid="run.SimpleTest (1).classpath"/>
</java>
</target>
<!-- =================================================================== -->
<!-- 执行测试案例 -->
<!-- =================================================================== -->
<target name="junit" depends="clean,init,build">
<junit printsummary="true" fork="true">
<formatter type="xml" usefile="true"/>
<classpath refid="demo.classpath"/>
<batchtest fork="on" todir="${report.path}" haltonfailure="no">
<fileset dir="${buildTest.path}">
<include name="**/*Test.class"/>
</fileset>
</batchtest>
</junit>
<!-- 产生单元测试报表文档 -->
<junitreport todir="${report.path}">
<fileset dir="${report.path}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${report.path}" />
</junitreport>
</target>
<!-- 清除Junit生成的报表文档 -->
<target name="delete">
<delete dir="${report.path}"/>
</target>
</project>
junit测试例子
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class SimpleTest {
private int x = 1;
private int y = 1;
@Test
public void testAddition() {
int z = x + y;
assertEquals(2, z);
}
// public static void main(String[] argc){
// System.err.println("我只是试一下而已");
// }
}
ant在jenkins中的配置
测试结果
生成了junitreport html形式的结果,可以很清晰的再次查看。
2、ant+testng自动化配置
build.xml配置testng
<?xml version="1.0" encoding="UTF-8"?>
<project name="Demo" default="run" basedir=".">
<echo message="import libs" />
<path id="run.classpath">
<fileset dir="${basedir}">
<include name="lib/testng.jar" />
<include name="lib/sikuli-script.jar" />
</fileset>
<fileset dir="${basedir}/lib/selenium">
<include name="selenium-java-2.46.0.jar" />
<include name="libs/*.jar" />
</fileset>
</path>
<taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="run.classpath" />
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile" depends="clean">
<echo message="mkdir"/>
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes" debug="on" encoding="UTF-8" includeAntRuntime="false">
<classpath refid="run.classpath"/>
</javac>
</target>
<path id="runpath">
<path refid="run.classpath"/>
<pathelement location="build/classes"/>
</path>
<target name="run" depends="compile">
<testng classpathref="runpath" outputDir="test-output">
<xmlfileset dir="${basedir}" includes="testng.xml"/>
<jvmarg value="-ea" />
</testng>
</target>
</project>
testng例子
import org.testng.annotations.Test;
public class NewTest {
@Test
public void f() {
System.err.println("我只是试一下而已");
}}
其中对应的testng.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<classes>
<!-- 下面这个name是你自己包名字然后.你的类名 -->
<class name="com.xxx.NewTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
3、ant+restAssured自动化测试配置
和ant+junit很类似,就不在陈述。因为restAssured其实在很大程度上还是junit的。大家不妨试试