Ant和TestNG任务示例

testng教程

在本教程中,我们将向您展示如何在Ant构建中运行TestNG测试。

1.按班级运行

build.xml
<taskdef name="testng" classname="org.testng.TestNGAntTask">
	<classpath location="lib/testng-6.8.14.jar" />
  </taskdef>
	
  <target name="testng" depends="compile">

	<!-- Assume test.path contains the project library dependencies -->
	<testng classpathref="test.path"
		outputDir="${report.dir}" 
		haltOnFailure="true">
			
		<!-- Extra project classpath, which is not included in above "test.path" -->
		<!-- Tell Ant where is the project and test classes -->
		<classpath location="${test.classes.dir}" />
		<classpath location="${src.classes.dir}" />
			
		<!-- Tell Ant what test classes need to run -->
		<classfileset dir="${test.classes.dir}" includes="**/*Test*.class" />
			
	</testng>

  </target>

2.通过XML运行

${resources.dir}/testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestAll">
 
    <test name="anyname">
	<classes>
		<class name="com.mkyong.test.TestMessage" />
	</classes>
    </test>
  
</suite>
build.xml
<taskdef name="testng" classname="org.testng.TestNGAntTask">
	<classpath location="lib/testng-6.8.14.jar" />
  </taskdef>
	
  <target name="testng" depends="compile">

	<testng classpathref="test.path"
		outputDir="${report.dir}" 
		haltOnFailure="true">
			
		<classpath location="${test.classes.dir}" />
			
		<!-- Tell Ant where is testng.xml -->
		<xmlfileset dir="${resources.dir}" includes="testng.xml"/>
			
	</testng>

  </target>

3.例子

一个Web应用程序示例,向您展示如何运行一组TestNG测试。

3.1返回信息

MessageGenerator.java
package com.mkyong.message;
 
import org.springframework.stereotype.Component;

@Component
public class MessageGenerator {

	public String getWelcomeMessage() {
		return "welcome"; 
	}
 
}

3.2两次TestNG测试。

TestMessage.java
package com.mkyong.test;

import org.testng.Assert;
import org.testng.annotations.Test;
import com.mkyong.message.MessageGenerator;

public class TestMessage {

	@Test
	public void test_welcome_message() {
		MessageGenerator obj = new MessageGenerator();
		Assert.assertEquals(obj.getWelcomeMessage(), "welcome");
	}
 
}
TestMessage2.java
package com.mkyong.test;

import org.testng.Assert;
import org.testng.annotations.Test;
import com.mkyong.message.MessageGenerator;

public class TestMessage2 {

	@Test
	public void test_welcome_message_2() {
		MessageGenerator obj = new MessageGenerator();
		Assert.assertEquals(obj.getWelcomeMessage(), "welcome");
	}
 
}

3.3使用ivy获取项目依赖项,并声明项目范围。

ivy.xml
<ivy-module version="2.0">
	<info organisation="org.apache" module="WebProject" />
	
	<configurations>
        <conf name="compile" description="Required to compile application"/>
        <conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
        <conf name="test"    description="Required for test only" extends="runtime"/>
    </configurations>
    
	<dependencies>
		<dependency org="org.testng" name="testng" rev="6.8.14" conf="test->default" />
	</dependencies>
</ivy-module>

3.4运行单元测试

build.xml
<project xmlns:ivy="antlib:org.apache.ivy.ant" 
	name="HelloProject" default="main" basedir=".">
	<description>
		Running TestNG Test 
	</description>

	<!-- Project Structure -->
	<property name="jdk.version" value="1.7" />
	<property name="projectName" value="WebProject" />
	<property name="src.dir" location="src" />
	<property name="test.dir" location="src" />
	<property name="report.dir" location="report" />
	<property name="web.dir" value="war" />
	<property name="web.classes.dir" location="${web.dir}/WEB-INF/classes" />

	<!-- ivy start -->
	<target name="resolve" description="retrieve dependencies with ivy">
		<echo message="Getting dependencies..." />
		<ivy:retrieve />

		<ivy:cachepath pathid="compile.path" conf="compile" />
		<ivy:cachepath pathid="runtime.path" conf="runtime" />
		<ivy:cachepath pathid="test.path" conf="test" />

	</target>

	<!-- Compile Java source from ${src.dir} and output it to ${web.classes.dir} -->
	<target name="compile" depends="init, resolve" description="compile source code">
		<mkdir dir="${web.classes.dir}" />
		<javac destdir="${web.classes.dir}" source="${jdk.version}" 
			target="${jdk.version}" debug="true" includeantruntime="false" classpathref="compile.path">
			<src path="${src.dir}" />
		</javac>
	</target>

	<!-- Run TestNG -->
	<target name="testng" depends="compile">

	  <testng classpathref="test.path"
		outputDir="${report.dir}" 
		haltOnFailure="true">

		<classpath location="${web.classes.dir}" />

		<xmlfileset dir="${resources.dir}" includes="testng.xml"/>

                <!--
		  <classfileset dir="${web.classes.dir}" includes="**/*Test*.class" />
		-->
	  </testng>

	</target>

	<!-- Create folders -->
	<target name="init">
		<mkdir dir="${src.dir}" />
		<mkdir dir="${web.classes.dir}" />
		<mkdir dir="${report.dir}" />
	</target>

	<!-- Delete folders -->
	<target name="clean" description="clean up">
		<delete dir="${web.classes.dir}" />
		<delete dir="${report.dir}" />
	</target>

	<target name="main" depends="testng" />

</project>
${resources.dir}/testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestAll">
 
	<test name="example1">
		<classes>
			<class name="com.mkyong.test.TestMessage" />
			<class name="com.mkyong.test.TestMessage2" />
		</classes>
	</test>
  
</suite>

运行

$ ant testng

输出量

testng:
   [testng] [TestNG] Running:
   [testng]   /Users/mkyong/Documents/workspace/AntSpringMVC/resources/testng.xml
   [testng] 
   [testng] 
   [testng] ===============================================
   [testng] TestAll
   [testng] Total tests run: 2, Failures: 0, Skips: 0
   [testng] ===============================================
   [testng] 

BUILD SUCCESSFUL
Total time: 3 seconds

做完了

下载源代码

下载它– AntSpringMVC-TestNG-示例 (90 KB)

参考文献

  1. 蚂蚁– TestNG任务
  2. Ang和jUnit任务
  3. TestNG –套件测试

翻译自: https://mkyong.com/ant/ant-and-testng-task-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值