TestNG Hello World

【本系列其他教程正在陆续翻译中,点击分类: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文件中添加依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.websystique.testng</groupId>
    <artifactId>TestNGHelloWorldExample</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
 
    <name>TestNGHelloWorldExample</name>
 
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

注意:因为我们仅仅使用testNG来测试,我们将元素的scope设置为test。
第2步: 创建简单Java类
下面是一个简单的java类,里面只有一个方法。此类的方法将被用来作单元测试。
package com.websystique.testng;
 
public class VatCalculator {
     
    /*
     * Returns 21% VAT on given amount
     */
    public double getVatOnAmount(double amount){
        return amount * 0.21;
    }
 

第3步: 创建测试类

下面的测试类 用来测试上面类中的 getVatOnAmount  方法
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  来执行测试。

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注解和你怎样设置。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值