【问题标题】:Junit runs all Test with the parameter marked with @ParameterJunit 使用标有@Parameter 的参数运行所有测试
【发布时间】:2015-09-23 10:41:28
【问题描述】:

在下面的代码中,我想用@Parameters标记的参数运行TestMethod1

    @RunWith(Parameterized.class)
public class Foo{

private boolean input;
private boolean expected;

public Foo(boolean input, boolean expected{
this.input=input;
this.expected=expected;
}

@Parameters
public static List<Object[]> data() {
        return Arrays.asList(new Object[][]{{false, false}, {false, false}});
    }

@Test
public void TestMethod1(){
assertEquals(expected, Baar.StaticMethod(input);
}

@Test
public void TestMethod2(){
assertEquals(expected, Baar.StaticMethod2(false);
}

问题是当我运行 junittes 时,TestMethod1 和 TestMethod2 方法都使用这些参数运行。如何告诉测试运行者只运行带有@Parameters 标记的参数的TestMethod1?

【问题讨论】:

    标签: junit junit4 parameterized-unit-test


    【解决方案1】:

    不确定纯 junit 是否允许,但有很多插件。在你的情况下(所有参数都预先知道)最简单的方法是做parametrized testing with zohhak

    @RunWith(ZohhakRunner.class)
    public class TestMyClass {      
    
        @TestWith({
            "true, false".
            "false, true"
        })
        public void test1(int actual, int expected) { //test }
    
        @TestWith({
            "false, false".
            "true, true"
        })
        public void test2(int actual, int expected) { //test }
    
        @Test
        public void test3() { //test }
    }
    

    如果您需要在运行时构建参数(生成、从文件读取等),那么您可以检查 junit-dataproviderjunit-params 等内容

    【讨论】:

      猜你喜欢
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      相关资源
      最近更新 更多