26

我已经用谷歌搜索了 JUnit 测试用例,它提出了一些看起来实现起来要复杂得多的东西——你必须创建一个扩展测试用例的新类,然后调用它:

public class MathTest extends TestCase {
    protected double fValue1;
    protected double fValue2;

    protected void setUp() {
       fValue1= 2.0;
       fValue2= 3.0;
    }
 }

public void testAdd() {
   double result= fValue1 + fValue2;
   assertTrue(result == 5.0);
}

但我想要的是非常简单的东西,比如 NUnit 测试用例

[TestCase(1,2)]
[TestCase(3,4)]
public void testAdd(int fValue1, int fValue2)
{
    double result= fValue1 + fValue2;
    assertIsTrue(result == 5.0);
}

在 JUnit 中有没有办法做到这一点?

4

6 回答 6

13

2017 update: JUnit 5 will include parameterized tests through the junit-jupiter-params extension. Some examples from the documentation:

Single parameter of primitive types (@ValueSource):

@ParameterizedTest
@ValueSource(strings = { "Hello", "World" })
void testWithStringParameter(String argument) {
    assertNotNull(argument);
}

Comma-separated values (@CsvSource) allows specifying multiple parameters similar to JUnitParams below:

@ParameterizedTest
@CsvSource({ "foo, 1", "bar, 2", "'baz, qux', 3" })
void testWithCsvSource(String first, int second) {
    assertNotNull(first);
    assertNotEquals(0, second);
}

Other source annotations include @EnumSource, @MethodSource, @ArgumentsSource and @CsvFileSource, see the documentation for details.


Original answer:

JUnitParams (https://github.com/Pragmatists/JUnitParams) seems like a decent alternative. It allows you to specify test parameters as strings, like this:

@RunWith(JUnitParamsRunner.class)
public class MyTestSuite {
    @Test
    @Parameters({"1,2", "3,4"})
    public testAdd(int fValue1, int fValue2) {
       ...
    }
}

You can also specify parameters through separate methods, classes or files, consult the JUnitParamsRunner api docs for details.

于 2013-07-10T18:24:08.167 回答
11

显然正确的答案是“不,没有等价物”。这很可悲。

JUnit 参数化测试和理论(如这里和JUnit - How to test a method with different values?中提到的)两者都可以完成工作,但几乎没有那么干净。可悲的是,它们写起来很复杂,而且很难阅读。

我希望有一天 JUnit 可以添加一种更简单、类似 NUnit 的语法。看起来应该没那么难;虽然也许需要 lambdas?

于 2013-04-08T19:42:08.873 回答
6

查看JUnit Theories 和 Datapoints也可能是值得的。它们允许您对测试进行参数化,但在您的输入上运行全对类型组合。

于 2012-09-09T07:03:01.767 回答
5

You can have junit with parameters using zohhak

Usage example:

@RunWith(ZohhakRunner.class)
public class HelloWorldTest {

    @TestWith({
        "2, 1,   3",
        "3, 5,   8"
    })
    public void should_add_numbers(int addend1, int addend2, int result) {

        assertThat(addend1 + addend2).isEqualTo(result);
    }
}
于 2014-08-02T20:55:56.087 回答
2

这很愚蠢,但这是我最终得到的解决方法。使用 4 行而不是 1 行。

@Test
public void testAdd1() {
    testAdd(1,2);
}
@Test
public void testAdd2() {
    testAdd(3,4);
}
private void testAdd(int fValue1, int fValue2)
{
    double result= fValue1 + fValue2;
    assertIsTrue(result == 5.0);
}
于 2012-06-03T21:43:30.447 回答
0

我使用了一个持有类来保存我的测试用例,如下所示:

class FlexiTest {
String var1;
String var2;
double var3;
String var4;
MyObject var5;
double expected;

public FlexiTest(String var1, String var2, double var3, String var4, MyObject var5, double expected) {
    super();
    this.var1;
    this.var2;
    this.var3;
    this.var4;
    this.var5;
    this.expected = expected;
}

然后stream像这样设置我的测试类对象:

static Stream<FlexiTest> provider(){

    FlexiTest ft1 = new FlexiTest("1", "2", 3, "4", MyObject.A, 1.1);
    FlexiTest ft2 = new FlexiTest("10", "20", 30, "40", MyObject.B, 11);
    FlexiTest ft3 = new FlexiTest("100", "200", 300, "400", MyObject.C, 110);
    
    return Stream.of(ft1, ft2, ft3);
}

然后用@ParameterizedTest 和@MethodSource 用对象流方法名称注释Test 方法。还有空和空检查:

@ParameterizedTest
@MethodSource("provider")   
@NullSource
@EmptySource
public void ClientTest(FlexiTest ft)
{
... my test code ...
}
于 2021-02-19T14:29:34.360 回答