是否有一个等同于NUnit的testcase属性的JUnit?

时间:2010-12-29 12:35:59

标签: unit-testing junit nunit testcase testcaseattribute

我已经搜索了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中有没有办法做到这一点?

6 个答案:

答案 0 :(得分:11)

2017更新:JUnit 5将通过junit-jupiter-params扩展程序包含参数化测试。 documentation

中的一些示例

基本类型的单个参数(@ValueSource):

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

逗号分隔值(@CsvSource)允许指定类似于以下JUnitParams的多个参数:

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

其他来源注释包括@EnumSource@MethodSource@ArgumentsSource@CsvFileSource,有关详细信息,请参阅documentation


原始回答:

JUnitParams(https://github.com/Pragmatists/JUnitParams)似乎是一个不错的选择。它允许您将测试参数指定为字符串,如下所示:

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

您还可以通过单独的方法,类或文件指定参数,有关详细信息,请参阅JUnitParamsRunner api docs

答案 1 :(得分:10)

显然,正确答案是“不,没有等价物。”那太可悲了。

JUnit参数化测试和理论(如此处和JUnit - How to test a method with different values?中提到的)都可以完成工作,但几乎没有干净。写作很难写,难以阅读。

我希望有一天JUnit可以添加更简单的NUnit语法。似乎不应该那么困难;虽然也许需要lambdas?

答案 2 :(得分:6)

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

答案 3 :(得分:5)

您可以使用zohhak

获取带参数的junit

用法示例:

@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);
    }
}

答案 4 :(得分:2)

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

@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);
}

答案 5 :(得分: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 ...
}
相关问题