TestNG Annotations示例

来源:互联网 发布:淘宝免费申请试用技巧 编辑:程序博客网 时间:2024/06/08 06:48

【本系列其他教程正在陆续翻译中,点击分类:TestNG进行查看。

【翻译 by 明明如月 QQ 605283073】

原文地址:http://websystique.com/java/testing/testng-hello-world-example/

下一篇:TestNG Annotations示例

本文我们将学习TestNG的hello world例子。

我们将学习怎么为使用testNG设置环境,怎么写和执行单元测试和验证结果。

项目基于maven.

-------------------------------------------

使用的环境

  • TestNG 6.9.4
  • Maven 3
  • JDK 1.7
  • Eclipse JUNO Service Release 2

项目目录结构:



下面我们将详细讲述 上面结构的具体内容。

第1步: 在 pom.xml文件中添加依赖

[html] view plain copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.    
  5.     <groupId>com.websystique.testng</groupId>  
  6.     <artifactId>TestNGHelloWorldExample</artifactId>  
  7.     <version>1.0.0</version>  
  8.     <packaging>jar</packaging>  
  9.    
  10.     <name>TestNGHelloWorldExample</name>  
  11.    
  12.     <dependencies>  
  13.         <dependency>  
  14.             <groupId>org.testng</groupId>  
  15.             <artifactId>testng</artifactId>  
  16.             <version>6.9.4</version>  
  17.             <scope>test</scope>  
  18.         </dependency>  
  19.     </dependencies>  
  20. </project>  

注意:因为我们仅仅使用testNG来测试,我们将元素的scope设置为test。

第2步: 创建简单Java类

下面是一个简单的java类,里面只有一个方法。此类的方法将被用来作单元测试。
[java] view plain copy
  1. package com.websystique.testng;  
  2.    
  3. public class VatCalculator {  
  4.        
  5.     /* 
  6.      * Returns 21% VAT on given amount 
  7.      */  
  8.     public double getVatOnAmount(double amount){  
  9.         return amount * 0.21;  
  10.     }  
  11.    

第3步: 创建测试类


下面的测试类 用来测试上面类中的getVatOnAmount 方法
[java] view plain copy
  1. package com.websystique.testng;  
  2.    
  3. import org.testng.annotations.Test;  
  4. import org.testng.Assert;  
  5.    
  6. public class TestVatCalculator {  
  7.    
  8.     @Test  
  9.     public void testGetVatOnAmount(){  
  10.         VatCalculator calc = new VatCalculator();  
  11.         double expected = 21;  
  12.         Assert.assertEquals(calc.getVatOnAmount(100), expected);  
  13.         Assert.assertNotEquals(calc.getVatOnAmount(120), expected);  
  14.     }  

@Test 注解添加在方法上,使得该方法称为一个测试方法。

我们调用VatCalculator类实例的getVatOnAmount 方法,然后使用estNG Assert api 来断言

返回值。如果任何一个断言失败,整个测试将会失败。Assert API 提供了很多比较希望的结果和真是结果的方法。


关键点:

源文件创建在src/main/java文件夹 然而测试类创建在src/test/java文件夹。

测试类和原类类名应该相同只是前面(或者后面)加上了Test。


测试类的名字:如果你想通过maven来执行此测试,必须有Test作为前缀或者后缀。

测试方法的名字:这个是任意的,不是一定要加上test,但是最好和原类中方法名类似。

第4步: 执行测试

有很多种执行方式
 1 TestNG Eclipse 插件
 2 使用maven

使用 TestNG Eclipse 插件
-----------------------------
从eclipse市场里搜“testNG”进行安装。
安装成功以后,右键该测试类,run as "testNG test"
将产生如下输出:


另外一个叫“test-output”的文件夹,含有测试结果相关的文件(特别是testng-results.xml)被添加到你的项目文件夹里面。

使用maven

--------------------------

通过cmd 进入项目目录

执行 mvn clean test 命令行。

maven将通过maven-surefire-plugin  来执行测试。

[html] view plain copy
  1. E:\workspace7\TestNGHelloWorldExample>mvn clean test  
  2. [INFO] Scanning for projects...  
  3. [INFO]  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building TestNGHelloWorldExample 1.0.0  
  6. [INFO] ------------------------------------------------------------------------  
  7. [INFO]  
  8. [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ TestNGHelloWorldExample ---  
  9. [INFO] Deleting E:\workspace7\TestNGHelloWorldExample\target  
  10. [INFO]  
  11. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNGHelloWorldExample ---  
  12. [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform d  
  13. ependent!  
  14. [INFO] skip non existing resourceDirectory E:\workspace7\TestNGHelloWorldExample\src\main\resources  
  15. [INFO]  
  16. [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ TestNGHelloWorldExample ---  
  17. [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform depende  
  18. nt!  
  19. [INFO] Compiling 1 source file to E:\workspace7\TestNGHelloWorldExample\target\classes  
  20. [INFO]  
  21. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestNGHelloWorldExample --  
  22. -  
  23. [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform d  
  24. ependent!  
  25. [INFO] skip non existing resourceDirectory E:\workspace7\TestNGHelloWorldExample\src\test\resources  
  26. [INFO]  
  27. [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ TestNGHelloWorldExample ---  
  28. [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform depende  
  29. nt!  
  30. [INFO] Compiling 1 source file to E:\workspace7\TestNGHelloWorldExample\target\test-classes  
  31. [INFO]  
  32. [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ TestNGHelloWorldExample ---  
  33. [INFO] Surefire report directory: E:\workspace7\TestNGHelloWorldExample\target\surefire-reports  
  34.    
  35. -------------------------------------------------------  
  36.  T E S T S  
  37. -------------------------------------------------------  
  38. Running com.websystique.testng.TestVatCalculator  
  39. Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@3a4346cd  
  40. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.976 sec  
  41.    
  42. Results :  
  43.    
  44. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0  
  45.    
  46. [INFO] ------------------------------------------------------------------------  
  47. [INFO] BUILD SUCCESS  
  48. [INFO] ------------------------------------------------------------------------  
  49. [INFO] Total time: 5.150s  
  50. [INFO] Finished at: Fri Jul 03 20:26:37 CEST 2015  
  51. [INFO] Final Memory: 12M/226M  
  52. [INFO] ------------------------------------------------------------------------  
  53. E:\workspace7\TestNGHelloWorldExample>  

通过上面测试结果我们可以看出。总共运行了一个测试方法。没有失败,没有错误,没有忽略。


本文结束,下一篇文章将讲述怎样使用TestNG注解和你怎样设置。