在Junit 5中的参数性测试中如何通过预期和实际值

发布于 2025-01-31 22:23:35 字数 1744 浏览 6 评论 0原文

我正在尝试实现参数化测试,其中我有一组输入和预期值,我想使用assertequals junit的方法进行测试。 我正在使用junit版本5.x 我将输入值传递给其他软件包中定义的自定义方法(我正在测试),并且我正在用<<<代码> assertequals 方法。

import static org.junit.jupiter.api.Assertions.assertEquals;

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

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.runners.Parameterized.Parameters;

class StringHelperTest {
    private StringHelper helper = new StringHelper();
    private String input;
    private String expectedOutput;

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }

    public String getExpectedOutput() {
        return expectedOutput;
    }

    public void setExpectedOutput(String expectedOutput) {
        this.expectedOutput = expectedOutput;
    }

    @Parameters
    public static Collection<String[]> testConditions() {
        String[][] expectedOutputs = { { "AACD", "CD" }, { "ACD", "CD" }, { "CDEF", "CDEF" }, { "CDAA", "CDAA" } };
        return Arrays.asList(expectedOutputs);
    }

    @ParameterizedTest
    @Test
    public void truncateAInFirst2Positions_A_atStart() {
        assertEquals(expectedOutput, helper.truncateAInFirst2Positions(input));

    }
}

在方法testConditions()中,实际和期望值作为2 dimensinalal字符串阵列enduretoutputs {{&lt; ltt; acture_value&gt; ,,&lt; 。}}。

我如何将endureTupts数组传递到truncateainfirst2positions_a_atstart()方法来测试greenge> enduce> enducent> enducent> enducent> enducent> enducent> enduceouttups array中提到的所有条件

I am trying to implement Parameterized test in which I have a set of input and expected values which I want to test using assertEquals method of JUnit. I'm using JUnit version 5.x for this I am passing the input value to my custom method defined in other package (that I'm testing) and I am checking it with an expected value with assertEquals method.

import static org.junit.jupiter.api.Assertions.assertEquals;

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

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.runners.Parameterized.Parameters;

class StringHelperTest {
    private StringHelper helper = new StringHelper();
    private String input;
    private String expectedOutput;

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }

    public String getExpectedOutput() {
        return expectedOutput;
    }

    public void setExpectedOutput(String expectedOutput) {
        this.expectedOutput = expectedOutput;
    }

    @Parameters
    public static Collection<String[]> testConditions() {
        String[][] expectedOutputs = { { "AACD", "CD" }, { "ACD", "CD" }, { "CDEF", "CDEF" }, { "CDAA", "CDAA" } };
        return Arrays.asList(expectedOutputs);
    }

    @ParameterizedTest
    @Test
    public void truncateAInFirst2Positions_A_atStart() {
        assertEquals(expectedOutput, helper.truncateAInFirst2Positions(input));

    }
}

In the method testConditions() the actual and expected values are given as a 2 dimensinonal String array expectedOutputs {{<actual_value>,<expected_value>},{...}}.

How do I pass expectedOutputs array to the truncateAInFirst2Positions_A_atStart() method to test all conditions mentioned in expectedOutputs array

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情域 2025-02-07 22:23:35

只是为了参考,因为已经回答了这个问题,但是如果您需要的其他内容(例如您自己的对象),则可以使用 org org.junit.jupiter。 params.provider.arguments.arguments ,例如:

@ParameterizedTest
@MethodSource("provideMyObject")
void myTestMethod(ActualObject actual, ExpectedObject expected) {
   // ...
}

static Stream<Arguments> provideMyObject() {
    return Stream.of(
            org.junit.jupiter.params.provider.Arguments.arguments(new ActualObject("foo"), new ExpectedObject("foo")),
            org.junit.jupiter.params.provider.Arguments.arguments(new ActualObject("bar"), new ExpectedObject("bar"))
    );
}

另请参阅: https://junit.org/junit5/junit5/docs/current/current/current/user-guide/#writing-tests-paramets-parameterized-tests-parameterized-tests-tests-sources-sources-sources-sources-sources-sources-methodsource

Just for reference as the question is already answered, but if you need something else than strings (e.g. your own objects) you can use org.junit.jupiter.params.provider.Arguments.arguments, e.g.:

@ParameterizedTest
@MethodSource("provideMyObject")
void myTestMethod(ActualObject actual, ExpectedObject expected) {
   // ...
}

static Stream<Arguments> provideMyObject() {
    return Stream.of(
            org.junit.jupiter.params.provider.Arguments.arguments(new ActualObject("foo"), new ExpectedObject("foo")),
            org.junit.jupiter.params.provider.Arguments.arguments(new ActualObject("bar"), new ExpectedObject("bar"))
    );
}

see also: https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests-sources-MethodSource

幸福还没到 2025-02-07 22:23:35

使用JUNIT 5,您具有参数化测试和输入参数的来源,这是该方法的简单参数。

因此,您需要使用此签名的方法:

@ParameterizedTest
//Source Annotation
void truncateAInFirst2Positions_A_atStart(String actual, String expected) {

现在您查找了一个用例匹配源。当您想使用方法proivde测试数据时,您可以使用@methodsource(“ testconditions”)。注释中的字符串指向提供您的测试数据的静态方法。该测试将执行X次,其中X是您集合中的对计数。字符串数组中的元素将用作方法参数。

对于诸如字符串之类的简单类型,CSV源可以更简单,更易于阅读:

@CsvSource({
"AACD, CD",
"ACD, CD"
})

您可以在外部文档中检查所有可能的来源和可能的源:
httpps://junit.org/junit.org/junit.org/junit5/docs/当前/用户指定/#写作测验 - 参数测试

With JUnit 5 you have ParameterizedTest and a Source for the input parameters, which are simple the parameters of the method.

So you want a Method with this signature:

@ParameterizedTest
//Source Annotation
void truncateAInFirst2Positions_A_atStart(String actual, String expected) {

And now you look up a for your use case matching source. When you want to proivde your test data with your method you could use @MethodSource("testConditions"). The String in the annotation points to the static method providing your test data. The test will be executed x times where x is the count of pairs in your collection. The elements in your String Array will be used as method arguments.

For simple types like Strings a CSV source can be simpler and easier to read:

@CsvSource({
"AACD, CD",
"ACD, CD"
})

You can check all possible sources and possiblites in the offcial documentation:
https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文