Testng-plug-with-ant
提供:Dev Guides
TestNG-ANT付きプラグ
この章では、ANTを使用してTestNGを実行する方法を示します。 以下に示す手順に従ってみましょう-
ステップ1:Apache Antをダウンロードする
[Ant]
OS | Archive Name |
---|---|
Windows | apache-ant-1.8.4-bin.zip |
Linux | apache-ant-1.8.4-bin.tar.gz |
Mac | apache-ant-1.8.4-bin.tar.gz |
ステップ2:Ant環境を設定する
*ANT_HOME* 環境変数を設定して、マシン上のANTライブラリが格納されているベースディレクトリの場所を指定します。 Antライブラリをapache-ant-1.8.4フォルダーに保存したと仮定します。
OS | Output |
---|---|
Windows | Set the environment variable ANT_HOME to C:\Program Files\Apache Software Foundation\apache-ant-1.8.4 |
Linux | Export ANT_HOME=/usr/local/apache-ant-1.8.4 |
Mac | Export ANT_HOME=/Library/apache-ant-1.8.4 |
Antコンパイラの場所を次のようにシステムパスに追加します-
OS | Description |
---|---|
Windows | Append the string %ANT_HOME\bin at the end of the system variable, Path. |
Linux | Export PATH=$PATH:$ANT_HOME/bin/ |
Mac | Not required. |
ステップ3:TestNGアーカイブをダウンロードする
必要なjarファイルをダウンロードしますhttp://www.testng.org [[[1]]]
OS | Archive name |
---|---|
Windows | testng-6.8.jar |
Linux | testng-6.8.jar |
Mac | testng-6.8.jar |
ステップ4:プロジェクト構造を作成する
- C:\> TestNG_WORKSPACE に TestNGWithAnt フォルダーを作成します。
- C:\> TestNG_WORKSPACE> TestNGWithAnt に src フォルダーを作成します。
- C \> TestNG_WORKSPACE> TestNGWithAnt に *test フォルダーを作成します。
- C:\> TestNG_WORKSPACE> TestNGWithAnt に lib フォルダーを作成します。
- C:\> TestNG_WORKSPACE> TestNGWithAnt> src フォルダーに MessageUtil クラスを作成します。
/*
* This class prints the given message on console.
*/
public class MessageUtil {
private String message;
//Constructor
//@param message to be printed
public MessageUtil(String message) {
this.message = message;
}
//prints the message
public void printMessage() {
System.out.println(message);
return message;
}
//add "Hi!" to the message
public String salutationMessage() {
message = "Hi!" + message;
System.out.println(message);
return message;
}
}
- C:\> TestNG_WORKSPACE> TestNGWithAnt> src フォルダーにTestMessageUtilクラスを作成します。
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestMessageUtil {
String message = "Manisha";
MessageUtil messageUtil = new MessageUtil(message);
@Test
public void testPrintMessage() {
System.out.println("Inside testPrintMessage()");
Assert.assertEquals(message,messageUtil.printMessage());
}
@Test
public void testSalutationMessage() {
System.out.println("Inside testSalutationMessage()");
message = "Hi!" + "Manisha";
Assert.assertEquals(message,messageUtil.salutationMessage());
}
}
- testng-6.8.jarを C:\> TestNG_WORKSPACE> TestNGWithAnt> lib フォルダーにコピーします。
ANT build.xmlを作成します
まず、次のようにTestNG Antタスクを定義する必要があります-
<taskdef name = "testng" classname = "org.testng.TestNGAntTask">
<classpath>
<pathelement location = "lib/testng-6.8.jar"/>
</classpath>
</taskdef>
次に、Antで <testng> タスクを使用してTestNGテストケースを実行します。
*build.xml* ファイルは次のとおりです-
<project name = "TestNGTest" default = "test" basedir = ".">
<!-- Define <testng> task -->
<taskdef name = "testng" classname = "org.testng.TestNGAntTask">
<classpath>
<pathelement location = "lib/testng-6.8.jar"/>
</classpath>
</taskdef>
<property name = "testdir" location = "test"/>
<property name = "srcdir" location = "src"/>
<property name = "libdir" location = "lib"/>
<property name = "full-compile" value="true"/>
<path id = "classpath.base"/>
<path id = "classpath.test">
<fileset dir = "${libdir}">
<include name = "**/*.jar"/>
</fileset>
<pathelement location = "${testdir}"/>
<pathelement location = "${srcdir}"/>
<path refid = "classpath.base"/>
</path>
<target name = "clean" >
<delete verbose="${full-compile}">
<fileset dir = "${testdir}" includes="**/*.class"/>
</delete>
</target>
<target name = "compile" depends="clean">
<javac srcdir = "${srcdir}" destdir = "${testdir}" verbose="${full-compile}">
<classpath refid = "classpath.test"/>
</javac>
</target>
<target name = "test" depends="compile">
<testng outputdir = "${testdir}" classpathref="classpath.test">
<xmlfileset dir = "${srcdir}" includes="testng.xml"/>
</testng>
</target>
</project>
次のAntコマンドを実行します。
C:\TestNG_WORKSPACE\TestNGWithAnt>ant
出力を確認します。
test:
[testng] [TestNG] Running:
[testng] C:\TestNG_WORKSPACE\TestNGWithAnt\src\testng.xml
[testng]
[testng] Inside testPrintMessage()
[testng] Manisha
[testng] Inside testSalutationMessage()
[testng] Hi!Manisha
[testng]
[testng] ===============================================
[testng] Plug ANT test Suite
[testng] Total tests run: 2, Failures: 0, Skips: 0
[testng] ===============================================
[testng]
BUILD SUCCESSFUL
Total time: 1 second