【问题标题】:Generate Data-Driven Unit-Tests in Java在 Java 中生成数据驱动的单元测试
【发布时间】:2016-11-26 22:44:17
【问题描述】:

假设我有一个junit方法

public class BusinessClassTest {

    private BusinessClass fixture;

    @Test
    public void test1() {
       //the following two paths reside in the man/test/resources folder
       String inputPath = "/fixtures/1/input";
       String expectedPath = "/fixtures/1/expected";
       validatedFixture(inputPath, expectedPath);
    }

    private void valiateFixture(String inputPath, String expectedPath) {
        //inputData = load the input data
        //feed it to a fixture
        //actual = fixture.process(inputData)
        //expectedData = loadExpectedData
        //validate(expectedData, actualData);
     }
}

现在假设我在灯具下有 20 个文件夹。如何遍历文件夹并为每个文件夹生成类似于

的方法
    @Test
    public void test{test_number}() {
       //the following two paths reside in the man/test/resources folder
       String inputPath = "/fixtures/{test_number}/input";
       String expectedPath = "/fixtures/{test_number}/expected";
       validatedFixture(inputPath, expectedPath);
    }

我想把这个类构建成 maven 的一部分。


更新

我正在使用velocity来生成类,但是不确定如何从maven进行代码生成...

【问题讨论】:

    标签: java maven code-generation


    【解决方案1】:

    使用 Matt 为文件系统遍历列出的内容,您可以使用参数化测试来执行您的无数验证。

    这种方法将根据 @Parameters(name) 注释的参数为您提供命名良好的测试。

    import java.io.File;
    import java.util.ArrayList;
    import java.util.Collection;
    
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    
    @RunWith (Parameterized.class)
    public class BusinessClassTest{
        @Parameters(name="{0}:{1}")
        public static Collection<Object[]> getTestPaths() {
            Collection<Object[]> allTests = new ArrayList<>();
    
             File file = new File("/fixtures/");
              String[] dirs = file.list((current, name) -> 
                                new File(current, name).isDirectory());
    
              for (String testNumber : dirs) {
                String inputPath = "/fixtures/" + dirs + "/input";
                String expectedPath = "/fixtures/" + dirs + "/expected";
                allTests.add(asConstructorArguments(inputPath, expectedPath));
              }
    
            return allTests;
        }
    
        private static Object[] asConstructorArguments(String inputPath, String expectedPath) {
            return new Object[]{inputPath, expectedPath};
        }
    
        private final String inputData;
        private final String expectedData;
        private final Fixture fakedFixture;
    
    
        public BusinessClassTest(String input, final String expected) {
            this.inputData = input;
            this.expectedData = expected;
            fakedFixture = new Fixture() {          
                @Override
                public String process(String path) {
                    return expected;
                }
            };
        }
    
        @Test
        public void validateFixture() {
            //feed it to a fixture
            String actualData = fakedFixture.process(inputData);
            Assert.assertEquals(expectedData, actualData);
        }
    
        //Interface to emulate your API
        public interface Fixture {
            String process(String path);
        }
    }
    

    【讨论】:

      【解决方案2】:

      您真的需要动态构建测试类吗?另一种方法是遍历fixtures 基本路径的所有子目录,然后直接为每个路径调用validatedFixture。在下面的 Java8 中查找示例:

      @Test
      public void bigTest {
        File file = new File("/fixtures/");
        String[] dirs = file.list((current, name) -> 
                          new File(current, name).isDirectory());
      
        for (String testNumber : dirs) {
          String inputPath = "/fixtures/" + dirs + "/input";
          String expectedPath = "/fixtures/" + dirs + "/expected";
          // omit the test method and call validation directly
          validatedFixture(inputPath, expectedPath);
        }
      }
      

      【讨论】:

      • 感谢您抽出宝贵时间来执行此操作...我知道这种方法。我需要实际的方法,以便我可以注释它们......
      猜你喜欢
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 2013-06-11
      • 2023-01-21
      • 1970-01-01
      相关资源
      最近更新 更多