junit运行Parameterized参擞化测试

Parameterized (参数化)的测试运行器允许你使用不同的参数多次运行同一个侧试。

运行此测试的必备条件:

1.必须使用@RunWith(Parameterized.class)

2.必须声明测试用到的变量

3.提供一个@Parameterized注解的方法

例如:

public class Calculator {
    public double add(double i,double j){
        return i+j;
    }

}
package com.laoxu.gamedog.util;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.*;

/**
 * 参数化测试
 *
 * @author xusucheng
 * @create 2018-12-16
 **/
@RunWith(Parameterized.class)
public class ParameterizedTest {
    private double expected;
    private double p1;
    private double p2;

    @Parameterized.Parameters
    public static Collection<Integer[]> getTestParamters() {
        return Arrays.asList(new Integer[][]{
                {2, 1, 1}, {3, 2, 1}, {4, 3, 1}
        });
    }

    public ParameterizedTest(double expected, double p1, double p2) {
        this.expected = expected;
        this.p1 = p1;
        this.p2 = p2;
    }

    @Test
    public void testAdd() {
        Calculator calculator = new Calculator();
        System.out.println("Addition with parameters : " + p1 + " and "
                + p2);
        assertEquals(expected, calculator.add(p1, p2), 0);
    }
}

运行结果:

Addition with parameters : 1.0 and 1.0
Addition with parameters : 2.0 and 1.0
Addition with parameters : 3.0 and 1.0

 

说明运行了3次!

 

 

posted @   一锤子技术员  阅读(5)  评论(0编辑  收藏  举报  
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示