Junit - 参数化测试(Parameterized Test)

简介: Junit - 参数化测试(Parameterized Test)

Junit 4 参数化测试 允许通过变化范围的参数值来测试方法。参数擦测试可以通过以下简单的步骤实现:

  1. 对测试类添加注解 @RunWith(Parameterized.class)。
  2. 将需要使用变化范围参数值测试的参数定义为私有变量。
  3. 使用上一步骤声明的私有变量作为入参,创建构造函数。
  4. 创建一个使用@Parameters注解的公共静态方法,它将需要测试的各种变量值通过集合的形式返回。
  5. 使用定义的私有变量定义测试方法。


Junit 4 参数化测试样例

EvenNumberChecker.java 校验输入的数字是否为偶数:

packagein.co.javatutorials;
/*** @author javatutorials.co.in*/publicclassEvenNumberChecker {
/*** Is input number even.** @param i input number* @return true if input is even number; otherwise return false*/publicbooleanisEven(inti) {
if (i%2==0) {
returntrue;
        } else {
returnfalse;
        }
    }
}

EvenNumberCheckerTest.java 对 EvenNumberChecker.java 进行参数化测试:

packagein.co.javatutorials;
importstaticorg.junit.Assert.*;
importjava.util.Arrays;
importjava.util.Collection;
importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.junit.runners.Parameterized;
importorg.junit.runners.Parameterized.Parameters;
/*** @author javatutorials.co.in*/// Step 1@RunWith(Parameterized.class)
publicclassEvenNumberCheckerTest {
// Step 2: variables to be used in test method of Step 5privateintinputNumber;
privatebooleanisEven;
// Step 3: parameterized constructorpublicEvenNumberCheckerTest(intinputNumber, booleanisEven) {
super();
this.inputNumber=inputNumber;
this.isEven=isEven;
    }
// Step 4: data set of variable values@ParameterspublicstaticCollection<Object[]>data() {
Object[][] data=newObject[][] {
                { 2, true },
                { 5, false },
                { 10, false }
        };
returnArrays.asList(data);
    }
@Testpublicvoidtest() {
System.out.println("inputNumber: "+inputNumber+"; isEven: "+isEven);
EvenNumberCheckerevenNumberChecker=newEvenNumberChecker();
// Step 5: use variables in test codebooleanactualResult=evenNumberChecker.isEven(inputNumber);
assertEquals(isEven, actualResult);
    }
}

样例输出

 在Eclipse junit 窗口的输出为:

image.png

样例日志输出

inputNumber: 2; isEven: trueinputNumber: 5; isEven: falseinputNumber: 10; isEven: false

  1. Junit - 测试框架介绍
  2. Junit - Eclipse 教程
  3. Junit - 基础注解(@BeforeClass、@Before、@Test、@After、@AfterClass)
  4. Junit - 断言方法(Assert Methods)
  5. Junit - 参数化测试(Parameterized Test)
  6. Junit - 套件测试(Suite Test)
  7. Junit - 忽略测试(Ignore Test)
  8. Junit - 超时测试(Timeout Test)
  9. Junit - 期望异常测试(Expected Test)
  10. Junit - 优先级测试(FixMethodOrder Test)
目录
相关文章
|
1月前
|
IDE Java 测试技术
Junit 单元测试
JUnit是Java常用的单元测试框架,简化了测试用例的编写和执行。其特点包括简单注解、自动化测试、可扩展性、灵活性及与IDE的集成。使用方法涉及创建测试类、利用注解如@Test、@BeforeEach等管理测试生命周期,以及使用各种断言方法验证结果。此外,JUnit支持参数化测试以覆盖更多输入组合,并能与Maven、Gradle等构建工具集成,提升测试效率和项目管理。
38 1
|
16天前
|
Java 测试技术 程序员
junit单元测试
junit单元测试
|
1月前
|
XML Java 测试技术
TestNG 与 JUnit 测试框架:哪个更好?
【2月更文挑战第16天】
46 1
TestNG 与 JUnit 测试框架:哪个更好?
|
2月前
|
测试技术 数据库
参数化单元测试
参数化单元测试
17 0
|
2月前
|
运维 Java 测试技术
spring 单元测试 Junit
spring 单元测试 Junit
43 2
|
2月前
|
存储 测试技术 数据库
数据驱动测试中的参数化
数据驱动测试中的参数化
|
3月前
|
测试技术 Shell Android开发
随机测试 Monkey Test
随机测试 Monkey Test
|
3月前
|
Java 测试技术 Spring
spring之单元测试:JUnit
【1月更文挑战第16天】 一、整合JUnit5 1、搭建子模块 2、引入依赖 3、添加配置文件 4、添加java类 5、测试 二、整合JUnit4 1、添加依赖 2、测试
75 4
|
3月前
|
缓存
pytest 运行测试函数报错的解决办法 TypeError: calling <function xxx> returned None, not a test
pytest 运行测试函数报错的解决办法 TypeError: calling <function xxx> returned None, not a test
97 0
|
3月前
|
Java 测试技术 C++
【JavaSE】Junit测试
【JavaSE】Junit测试
30 0

热门文章

最新文章