在Maven里使用TestNG的方法的单元测试实例

要在Maven里面使用TestNG很简单。去TestNG的网站上可以找到非常详细的一段代码,将下列代码加入<dependencies></dependencies>标签之间:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
  当然,这是对应的TestNG 6.1.1版本。其它版本的TestNG,应当只需要改动一下版本号即可。
  在src/main/java文件夹中书写Main.java文件如下:
package org.silentmusicbox.justanothermavenproject;
public class Main {
public String sayHello() {
return "Hallo Welt!";
}
public static void main(String [] args) {
Main objOfMain = new Main();
System.out.println(objOfMain.sayHello());
}
}
  在src/test/java文件夹中书写TestMain.java文件如下:
packageorg.silentmusicbox.justanothermavenproject;
importorg.testng.Assert;
importorg.testng.annotations.BeforeMethod;
importorg.testng.annotations.Test;
publicclassTestMain{
privateMainm;
@BeforeMethod
publicvoidinit(){
m=newMain();
}
@Test
publicvoidtestSayHello(){
Assert.assertEquals(m.sayHello(),"HalloWelt!");
}
}
  运行mvn test,一切正常。如果将sayHello()方法中的返回值改为"Hello World!"则报错。说明TestNG已经正常运行了。
最新内容请见作者的GitHub页:http://qaseven.github.io/

上一篇:如何修复Ubuntu/Mint中无法添加PPA源的错误


下一篇:《WCF的绑定模型》博文系列汇总[共6篇]