前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TestNG Hello World

TestNG Hello World

作者头像
明明如月学长
发布2021-08-27 15:28:54
3390
发布2021-08-27 15:28:54
举报

【本系列其他教程正在陆续翻译中,点击分类: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文件中添加依赖
代码语言:javascript
复制
    4.0.0
 
    com.websystique.testng
    TestNGHelloWorldExample
    1.0.0
    jar
 
    TestNGHelloWorldExample
 
    
        
            org.testng
            testng
            6.9.4
            test

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

第2步: 创建简单Java类

下面是一个简单的java类,里面只有一个方法。此类的方法将被用来作单元测试。

代码语言:javascript
复制
package com.websystique.testng;
 
public class VatCalculator {
     
    /*
     * Returns 21% VAT on given amount
     */
    public double getVatOnAmount(double amount){
        return amount * 0.21;
    }
第3步: 创建测试类

下面的测试类 用来测试上面类中的 getVatOnAmount  方法

代码语言:javascript
复制
package com.websystique.testng;
 
import org.testng.annotations.Test;
import org.testng.Assert;
 
public class TestVatCalculator {
 
    @Test
    public void testGetVatOnAmount(){
        VatCalculator calc = new VatCalculator();
        double expected = 21;
        Assert.assertEquals(calc.getVatOnAmount(100), expected);
        Assert.assertNotEquals(calc.getVatOnAmount(120), expected);
    }

@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  来执行测试。

代码语言:javascript
复制
E:\workspace7\TestNGHelloWorldExample>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building TestNGHelloWorldExample 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ TestNGHelloWorldExample ---
[INFO] Deleting E:\workspace7\TestNGHelloWorldExample\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNGHelloWorldExample ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform d
ependent!
[INFO] skip non existing resourceDirectory E:\workspace7\TestNGHelloWorldExample\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ TestNGHelloWorldExample ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform depende
nt!
[INFO] Compiling 1 source file to E:\workspace7\TestNGHelloWorldExample\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestNGHelloWorldExample --
-
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform d
ependent!
[INFO] skip non existing resourceDirectory E:\workspace7\TestNGHelloWorldExample\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ TestNGHelloWorldExample ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform depende
nt!
[INFO] Compiling 1 source file to E:\workspace7\TestNGHelloWorldExample\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ TestNGHelloWorldExample ---
[INFO] Surefire report directory: E:\workspace7\TestNGHelloWorldExample\target\surefire-reports
 
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.websystique.testng.TestVatCalculator
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@3a4346cd
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.976 sec
 
Results :
 
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.150s
[INFO] Finished at: Fri Jul 03 20:26:37 CEST 2015
[INFO] Final Memory: 12M/226M
[INFO] ------------------------------------------------------------------------
E:\workspace7\TestNGHelloWorldExample>

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/05/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 项目目录结构:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档