编写TestNG测试基本上包括以下步骤:

  • 测试和编写业务逻辑,在代码中插入TestNG的注解..

  • 添加一个testng.xml文件或build.xml中在测试信息(例如类名,您想要运行的组,等..)

  • 运行 TestNG.

在这里,我们将看到一个完整的例子了TestNG测试使用POJO类,业务逻辑类,将通过TestNG的运行测试XML。

创建 EmployeeDetails.java 在 C:\ > TestNG_WORKSPACE 是一个 POJO 类.

public class EmployeeDetails {

   private String name;
private double monthlySalary;
private int age; /**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the monthlySalary
*/
public double getMonthlySalary() {
return monthlySalary;
}
/**
* @param monthlySalary the monthlySalary to set
*/
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}

EmployeeDetails 类是用来

  • get/set 员工的名字的值

  • get/set 员工月薪的值

  • get/set员工年龄的值

创建一个 EmpBusinessLogic.java 在 C:\ > TestNG_WORKSPACE 其中包含业务逻辑

public class EmpBusinessLogic {
// Calculate the yearly salary of employee
public double calculateYearlySalary(EmployeeDetails employeeDetails){
double yearlySalary=0;
yearlySalary = employeeDetails.getMonthlySalary() * 12;
return yearlySalary;
} // Calculate the appraisal amount of employee
public double calculateAppraisal(EmployeeDetails employeeDetails){
double appraisal=0;
if(employeeDetails.getMonthlySalary() < 10000){
appraisal = 500;
}else{
appraisal = 1000;
}
return appraisal;
}
}

EmpBusinessLogic 类用于计算

  • 员工的年薪

  • 考核支付予雇员

现在,让我们创建一个TestNG 类名称为 TestEmployeeDetails.java 在 C:\ > TestNG_WORKSPACE. TestNG类是一个Java类,它包含至少一个TestNG的注解。 这个类包含测试用例进行测试。可以配置,@BeforeXXX和@AfterXXX注解了TestNG测试 (在本章,我们会看到这样TestNG - Execution Procedure) 它允许执行一些Java逻辑的目标点之前和之后。

import org.testng.Assert;
import org.testng.annotations.Test; public class TestEmployeeDetails {
EmpBusinessLogic empBusinessLogic = new EmpBusinessLogic();
EmployeeDetails employee = new EmployeeDetails(); @Test
public void testCalculateAppriasal() {
employee.setName("Rajeev");
employee.setAge(25);
employee.setMonthlySalary(8000);
double appraisal = empBusinessLogic
.calculateAppraisal(employee);
Assert.assertEquals(500, appraisal, 0.0, "500");
} // test to check yearly salary
@Test
public void testCalculateYearlySalary() {
employee.setName("Rajeev");
employee.setAge(25);
employee.setMonthlySalary(8000);
double salary = empBusinessLogic
.calculateYearlySalary(employee);
Assert.assertEquals(96000, salary, 0.0, "8000");
}
}

TestEmployeeDetails 测试方法,用于类EmpBusinessLogic,

  • 测试雇员的年薪

  • 测试评估员工的金额

你可以运行测试,但是必须使用特殊的XML文件,通常命名为testng.xml配置TestNG。此文件的语法很简单,其内容如下。创建这个文件C:\ > TestNG_WORKSPACE:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<classes>
<class name="TestEmployeeDetails"/>
</classes>
</test>
</suite>

以上文件的详情如下:

  • suite代表一个XML文件。它可以包含一个或多个测试,并被定义由<suite>标记

  • 标签<test>代表一个测试,并可以包含一个或多个TestNG的类

  • <class>的标签代表一个TestNG的类是一个Java类,它包含至少一个TestNG的注解。它可以包含一个或多个测试方法。

编译使用javac测试用例类。

C:\TestNG_WORKSPACE>javac EmployeeDetails.java EmpBusinessLogic.java TestEmployeeDetails.java

现在TestNG用下面的命令:

C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

如果所有配置正确的话,你应该看到测试结果,在控制台中。此外,TestNG创建了一个非常漂亮的HTML报告,会自动在当前目录下创建一个文件夹名为test-output 。如果打开​​并加载的index.html,会看到类似下面的图片中的一个页面:

文章转载自:易百教程 [http://www.yiibai.com]
本文标题:TestNG编写测试
转载请保留原文链接:http://www.yiibai.com/html/testng/2013/0914294.html

testng入门教程2用TestNG编写测试及执行测试的更多相关文章

  1. testng入门教程3用TestNG执行case的顺序

    本教程介绍了TestNG中执行程序的方法,这意味着该方法被称为第一和一个接着.下面是执行程序的TestNG测试API的方法的例子. 创建一个Java类文件名TestngAnnotation.java在 ...

  2. testng入门教程1在testng运行一个简单的testcase

    在eclips运行java,创建一个Java类文件名TestNGSimpleTest  C:\ > TestNG_WORKSPACE import org.testng.annotations. ...

  3. testng入门教程4用TestNG执行case

    使用TestNG类执行测试用例.这个类的主入口点在TestNG的框架运行测试.用户可以创建自己的TestNG的对象,并调用它以许多不同的方式: 在现有的testng.xml 合成testng.xml, ...

  4. testng入门教程12 TestNG执行多线程测试

    testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...

  5. TestNG 入门教程【转】

    TestNG 入门教程[转] 国庆7天假期,大部分朋友都出去旅游了,微信圈里全是晒旅游的照片, 东南亚游,欧洲游呀,真是羡慕呀. 悲惨的我只去了上海野生动物园, 在家休息,利用这段假期,把之前学过的东 ...

  6. testng入门教程16数据驱动(把数据写在xml)

    testng入门教程16数据驱动(把数据写在xml) testng入门教程16数据驱动(把数据写在xml)把数据写在xml文件里面,在xml文件右键选择runas---testng执行 下面是case ...

  7. testng入门教程11 TestNG运行JUnit测试

    现在,您已经了解了TestNG和它的各种测试,如果现在担心如何重构现有的JUnit代码,那就没有必要,使用TestNG提供了一种方法,从JUnit和TestNG按照自己的节奏.也可以使用TestNG执 ...

  8. testng入门教程10 TestNG参数化测试

    在TestNG的另一个有趣的功能是参数测试.在大多数情况下,你会遇到这样一个场景,业务逻辑需要一个巨大的不同数量的测试.参数测试,允许开发人员运行同样的测试,一遍又一遍使用不同的值. TestNG让你 ...

  9. testng入门教程9 TestNG依赖测试

    有时候,你可能需要在一个特定的顺序调用方法在测试案例,或你想分享一些数据和方法之间的状态.TestNG支持这种依赖测试方法之间的显式依赖它支持声明. TestNG允许指定依赖,无论与否: 使用属性de ...

随机推荐

  1. jQuery队列(一)

    jQuery的队列依赖缓存机制事件,它同时是animate的基础. 它不像事件机制.缓存机制.回调机制一样有自己的命名空间,由于比较简单,所以直接挂在到$和jQuery对象上. 它提供的基础方法有: ...

  2. linux各种版本下载地址

    本文转载地址:http://52199999.blog.51cto.com/740826/290179 觉得好大家给顶顶,先谢谢了!呵呵 首先提供两个镜像站:http://mirrors.sohu.c ...

  3. 应急响应--记录一次漏洞紧急处理中意外发现的挖矿木马(Shiro反序列化漏洞和ddg挖矿木马)

    背景 某公司线上服务器意外发现一个Apache Shiro 反序列化漏洞,可以直接GetShell.出于做安全的谨慎,马上出现场应急,确认漏洞.该漏洞存在在cookie字段中的rememberMe字段 ...

  4. CentOS 安装Sqlite3

    wget http://www.sqlite.org/sqlite-autoconf-3070500.tar.gz tar xvzf sqlite-autoconf-3070500.tar.gz cd ...

  5. JavaAgent入门

    JavaAgent 是JDK 1.5 以后引入的,也可以叫做Java代理. JavaAgent 是运行在 main方法之前的拦截器,它内定的方法名叫 premain ,也就是说先执行 premain ...

  6. s3接口认证说明

    S3 Authorization太绕,太头痛,下面解释说明: XS3 REST API基于HMAC(哈希消息身份验证码)密钥使用自定义HTTP方案进行身份验证.要对请求进行身份验证,您首先需要合并请求 ...

  7. STS没有找到Dynamic Web Project

    解决:安装JavaEE插件 help-> install new software-> 选择sts对应的eclipse版本站点,如eclipse版本4.09选择2018-09.4.10选择 ...

  8. Thymeleaf 入门

    基本项目结构: Thymeleaf配置: spring.thymeleaf.mode=LEGACYHTML5 spring.thymeleaf.cache=false spring.thymeleaf ...

  9. zookeeper 安装的三种模式

    Zookeeper安装 zookeeper的安装分为三种模式:单机模式.集群模式和伪集群模式. 单机模式 首先,从Apache官网下载一个Zookeeper稳定版本,本次教程采用的是zookeeper ...

  10. 2-sat入门(tarjan)hdu(3062)

    hdu3062 Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...