java中test的用法详解,testng使用详解(示例代码)

本文详细介绍了TestNG测试框架的使用,包括@Test注解的各种属性,如enable、expectedExceptions、dependsOnMethods等,并通过示例代码展示了如何在Java中使用TestNG进行单元测试。此外,还讲解了@Test注解的其他功能,如数据提供、超时设置、分组测试、方法依赖和多线程测试。最后,提到了TestNG的配置文件testng.xml的使用和方法参数传递。
摘要由CSDN通过智能技术生成

一、testng 介绍

TestNG 是一个测试框架,其灵感来自 JUnit 和 NUnit,但同时引入了一些新的功能,使其功能更强大,使用更方便。

TestNG 设计涵盖所有类型的测试:单元,功能,端到端,集成等,它需要 JDK5 或更高的 JDK 版本。

详细使用说明请参考官方链接:https://testng.org/doc/index.html

在 maven 中引入依赖:

org.testng

testng

6.10

简单示例:

(1)被测试 HelloWorld 类

package study.testng;

public class HelloWorld {

public String hello(){

return "hello world !";

}

}

(2)测试类 TestHelloWorld 类

package study.testng;

import org.testng.Assert;

import org.testng.annotations.Test;

public class TestHelloWorld {

//测试返回结果不为空

@Test

public void tester1(){

HelloWorld hello = new HelloWorld();

String helloworld = hello.hello();

Assert.assertNotNull(helloworld);

}

//测试返回结果为”hello world !“字符串

@Test

public void tester2(){

HelloWorld hello = new HelloWorld();

String helloworld = hello.hello();

System.out.println(helloworld);

Assert.assertEquals(helloworld, "hello world !");

}

}

(3)测试结果

[TestNG] Running:

C:UsersAdministrator.IntelliJIdea2019.2systememp-testng-customsuite.xml

hello world !

===============================================

Default Suite

Total tests run: 2, Failures: 0, Skips: 0

===============================================

二、@Test注解及常用属性

凡是在类方法中添加了 @Test 注解的就是我们需要测试的方法

1、enable 测试方法是否执行

默认是 true , 如果设置为 false ,则在运行时不会执行这个测试方法;

示例:

package com.ggf.testng.annotation;

import org.testng.annotations.Test;

/**

* @Description: 忽略测试,可以通过@Test的注解的enable属性来配置是否执行用例方法

* enable默认值为 true,需要设置为false才会跳过这个测试用例

* @Author: ggf

* @Date: 2019/12/29

*/

public class IgnoreTest {

@Test

public void ignore1() {

System.out.println("ignore1 run...");

}

@Test(enabled = false)

public void ignore2() {

System.out.println("ignore2 run ...");

}

@Test

public void ignore3() {

System.out.println("ignore3 run ...");

}

}

运行结果:

[TestNG] Running:

C:UsersAdministrator.IntelliJIdea2019.2systememp-testng-customsuite.xml

ignore1 run...

ignore3 run ...

===============================================

Default Suite

Total tests run: 2, Failures: 0, Skips: 0

===============================================

2、预期异常expectedExeption

@Test(expectedExceptions = ClassName.class)

如果 ClassName 类抛出了异常,测算测试通过,没有异常算测试不通过;

expectedExceptions的值也可以是一个数组:

@Test(expectedExceptions = {ClassName.class, ClassName2.class,... })

示例:

package com.ggf.testng.annotation;

import org.testng.annotations.Test;

/**

* @Description: 异常测试

* @Author: ggf

* @Date: 2019/12/29

*

* 什么时候会用到异常测试??

* 在我们期望结果为某一个异常的时候

* 比如:我们传入了某些不合法的参数,程序抛出了异常

* 也就是说我的期望结果就是这个异常。

*/

public class ExpectedException {

/**

* 运行时异常,我们期望返回一个运行时异常,这条用例才是正确的。

*/

@Test(expectedExceptions = RuntimeException.class, enabled = false)

public void runTimeExceptionFailed() {

System.out.println("没有抛出异常,这条用例不通过!");

}

/**

* 结果抛出了一个运行时异常,和我们的期望一致,测试通过。

*/

@Test(expectedExceptions = RuntimeException.class)

public void runTimeExceptionSuccess() {

System.out.println("程序抛出了运行时异常,测试用例通过!");

throw new RuntimeException();

}

}

运行结果:

[TestNG] Running:

C:UsersAdministrator.IntelliJIdea2019.2systememp-testng-customsuite.xml

程序抛出了运行时异常,测试用例通过!

===============================================

Default Suite

Total tests run: 1, Failures: 0, Skips: 0

===============================================

3、依赖方法dependsOnMethods

在被依赖的方法运行完成之后运行当前方法,如果依赖方法测试不通过,那么当前方法也不会继续运行了;依赖的方法可以

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值