[Eclipse笔记]Give TestNG a try in Eclipse.


自从接触J2SE 5.0以来,似乎Cedric Beust这个名字就在脑海中挥之不去,大约是跟他在java.sun.com的一篇对J2SE 5.0新特性的介绍文章有关吧,其实这个法国人给人留下印象最深的是他基于JUnit和J2SE 5.0的注解(Annotation)的思想创作的测试工具:TestNG。

用过JUnit的朋友,尤其是经常使用的朋友大概会跟他有同感,那就是JUnit有些时候功能显得太单调,不够灵活,但是很少有人像他那样自己另起炉灶去写一个自己的测试框架。也许这就是我们跟发达国家在技术创新上的差距吧,我想这至少是意识上的差距。

国外各大网站已经前前后后做了不少介绍,国内网站则似乎还没有十分重视这个看上去不起眼的家伙。这究竟是个什么样的工具呢?有些什么特色?大致总结一下就是:

对Java注解的支持
利用XML配置测试过程
不需要实现接口或继承类(如JUnit的TestCase/TestSuite)
支持独立的方法和分组
支持并行测试
测试方法支持传入参数
任意制定的调用次数和成功率统计

现在,我们有机会在Eclipse环境尝试这个测试框架了,还要多多感谢TestNG插件的作者Alexandru Popescu牺牲掉那么多自己的时间。怎么样?跃跃欲试?该插件的update site是: http://beust.com/eclipse/,需要注意的是它目前只支持Eclipse 3.1版本,因为Java注解的原因。

为了简单做一个演示,我随便写了一些代码:

[FooCalculator.java]
[Eclipse笔记]Give TestNG a try in Eclipse. package sean.home.test;
[Eclipse笔记]Give TestNG a try in Eclipse.
[Eclipse笔记]Give TestNG a try in Eclipse.
public   class  FooCalculator  {
[Eclipse笔记]Give TestNG a try in Eclipse.    
[Eclipse笔记]Give TestNG a try in Eclipse.    
public static int increaseByOne(int origVal) {
[Eclipse笔记]Give TestNG a try in Eclipse.        
return ++origVal;
[Eclipse笔记]Give TestNG a try in Eclipse.    }

[Eclipse笔记]Give TestNG a try in Eclipse.
[Eclipse笔记]Give TestNG a try in Eclipse.}

[Eclipse笔记]Give TestNG a try in Eclipse.

[FooCalculatorTest.java]
[Eclipse笔记]Give TestNG a try in Eclipse. package sean.home.test;
[Eclipse笔记]Give TestNG a try in Eclipse.
[Eclipse笔记]Give TestNG a try in Eclipse.import com.beust.testng.annotations.
* ;
[Eclipse笔记]Give TestNG a try in Eclipse.
[Eclipse笔记]Give TestNG a try in Eclipse.
public   class  FooCalculatorTest  {
[Eclipse笔记]Give TestNG a try in Eclipse.    
[Eclipse笔记]Give TestNG a try in Eclipse.    
private int origVal;
[Eclipse笔记]Give TestNG a try in Eclipse.    
[Eclipse笔记]Give TestNG a try in Eclipse.    @Configuration (beforeTestClass 
= true)
[Eclipse笔记]Give TestNG a try in Eclipse.    
public void setUp() {
[Eclipse笔记]Give TestNG a try in Eclipse.        origVal 
= Integer.MAX_VALUE;
[Eclipse笔记]Give TestNG a try in Eclipse.    }

[Eclipse笔记]Give TestNG a try in Eclipse.    
[Eclipse笔记]Give TestNG a try in Eclipse.    @Test
[Eclipse笔记]Give TestNG a try in Eclipse.    
public void testIncreasByOne() {
[Eclipse笔记]Give TestNG a try in Eclipse.        
int result = FooCalculator.increaseByOne(origVal);
[Eclipse笔记]Give TestNG a try in Eclipse.        assert result 
> 0;
[Eclipse笔记]Give TestNG a try in Eclipse.    }

[Eclipse笔记]Give TestNG a try in Eclipse.    
[Eclipse笔记]Give TestNG a try in Eclipse.}

[Eclipse笔记]Give TestNG a try in Eclipse.

[TestFooCalculator.xml]
[Eclipse笔记]Give TestNG a try in Eclipse. <! DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd"  >
[Eclipse笔记]Give TestNG a try in Eclipse.
[Eclipse笔记]Give TestNG a try in Eclipse.
< suite  name ="Custom Suite"   >
[Eclipse笔记]Give TestNG a try in Eclipse.    
< test  name ="To Test FooCalculator"   >
[Eclipse笔记]Give TestNG a try in Eclipse.        
< classes >
[Eclipse笔记]Give TestNG a try in Eclipse.            
< class  name ="sean.home.test.FooCalculatorTest"   />
[Eclipse笔记]Give TestNG a try in Eclipse.        
</ classes >
[Eclipse笔记]Give TestNG a try in Eclipse.    
</ test >
[Eclipse笔记]Give TestNG a try in Eclipse.
</ suite >
[Eclipse笔记]Give TestNG a try in Eclipse.

完成以后,我们可以在Eclipse中右键选中我们的测试类或者配置文件,选运行TestNG,这样我们就能看到Console视图和TestNG视图中显示的结果了。

更多信息,参考: http://beust.com/testng/。希望对NUnit的爱好者、学习者们也有帮助。

你可能感兴趣的:(eclipse)