简体   繁体   English

在JUnit测试用例中使用不同的数据子集测试不同的方法

[英]Test different methods with different subset of data in JUnit test case

Say I have a JUnit test case as: 假设我有一个JUnit测试用例:

@RunWith(Parameterized.class)
public class TestMyClass {

    @Parameter
    private int expected;
    @Parameter
    private int actual;  

    @Parameters
    public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] {     
             { 0,1 }, { 1,2 }, { 2,3 }, { 3,4 }, { 4,5 }, { 5,6 },{ 6,7 }  
       });
    }

    @Test
    public void test1 { //test }

    @Test
    public void test2 { //test }

}

I want to run test1 only with {0,1}, {1,2} and {2,3} and run test2 only with {3,4}, {4,5} {5,6} 我想仅使用{0,1},{1,2}和{2,3}运行test1并仅使用{3,4},{4,5} {5,6}运行test2

How can I achieve this? 我怎样才能做到这一点?

Edit: Parameters are read at Runtime from a file. 编辑:从运行时从文件读取参数。

Seems that there is no way you can use different sets of parameters for different tests in once class using JUnit standart '@Parameters' stuff. 似乎没有办法可以使用JUnit标准'@Parameters'的东西在一次类中为不同的测试使用不同的参数集。 You can try out junit-dataprovider . 你可以试试junit-dataprovider It is similar to TestNG data providers. 它类似于TestNG数据提供者。 For example: 例如:

@RunWith(DataProviderRunner.class)
public class TestMyClass {

    @DataProvider
    public static Object[][] firstDataset() {
    return new Object[][] {     
             { 3,4 }, { 4,5 }, { 5,6 },{ 6,7 }  
       };
    }


    @DataProvider
    public static Object[][] secondDataset() {
    return new Object[][] {     
             { 3,4 }, { 4,5 }, { 5,6 },{ 6,7 }  
       };
    }

    @Test
    @UseDataProvider("firstDataset")
    public void test1(int a, int b) { //test }

    @Test
    @UseDataProvider("secondDataset")
    public void test2(int a, int b) { //test }

}

Or you can create 2 classes for each test. 或者,您可以为每个测试创建2个类。

But I think using junit-dataprovider is more convenient. 但我认为使用junit-dataprovider更方便。

there is plenty of junit libraries that allows you do that. 有很多junit库可以让你这样做。 if you know your parameters up-front (looks like your case), parametrized testing with zohhak may be the simplest: 如果您事先知道您的参数(看起来像您的情况), 使用zohhak进行参数化测试可能是最简单的:

@RunWith(ZohhakRunner.class)
public class TestMyClass {      

    @TestWith({
        "1, 6".
        "2, 8",
        "3, 4"
    })
    public void test1(int actual, int expected) { //test }

    @TestWith({
        "2, 2".
        "0, -8",
        "7, 1"
    })
    public void test2(int actual, int expected) { //test }

}

if you need to build parameters in run-time (generating, reading from file etc.) then you can check things like junit-dataprovider or junit-params 如果你需要在运行时构建参数(生成,从文件读取等),那么你可以检查junit-dataproviderjunit-params之类的东西

You can use JUnit's Enclosed runner if you don't want to use another library: 如果您不想使用其他库,可以使用JUnit的Enclosed runner:

@RunWith(Enclosed.class)
public class ATest {

  @RunWith(Parameterized.class)
  public static class TestMyClass {

    @Parameter
    private int expected;
    @Parameter
    private int actual;  

    @Parameters
    public static Collection<Object[]> data() {
      return Arrays.asList(new Object[][] {     
         { 0,1 }, { 1,2 }, { 2,3 }  
      });
    }

    @Test
    public void test1 {
      //test
    }
  }

  @RunWith(Parameterized.class)
  public static class TestMyClass {
    @Parameter
    private int expected;
    @Parameter
    private int actual;  

    @Parameters
    public static Collection<Object[]> data() {
      return Arrays.asList(new Object[][] {     
         { 3,4 }, { 4,5 }, { 5,6 },{ 6,7 }  
      });
    }

    @Test
    public void test2 {
      //test
    }
  }
}

By the way: you don't have to wrap the parametes with a List with JUnit 4.12. 顺便说一句:您不必使用带有JUnit 4.12的List来包装参数。

@Parameters
public static Object[][] data() {
  return new Object[][] {     
     { 0,1 }, { 1,2 }, { 2,3 }  
  };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM