How to write dynamic Test Case

Suppose, I have a junit test class:

class MyComponentTest {

  private void test(File file) {...}

  @Test public void test1() {test("test1.txt")}
  @Test public void test2() {test("test2.txt")}
  @Test public void test3() {test("test3.txt")}
}

The test method reads the input data from the file and test the component with the input data.

What if I am changing the MyComponentTest ?

class MyComponentTest {

  private void test(File file) {...}

  @Test public void testAll() {
    for (String name: dir.list())
      test(new File(name))
  }
}

Now I have only one test ( testAll ) instead of three tests ( test1 , test2 , and test3 ) as it was before. The Runner for example will not recognize three separate tests as it did in the previous version of the class.

My question is: how to make each test method invocation a separate test from the junit point of view ?


Though you can use Junit's Parameterized tests for this it is bit involved and ugly. I suggest you looking at the spockframework which simplifies this a lot. And there is also another option in TestNG.

链接地址: http://www.djcxy.com/p/64100.html

上一篇: 在一个方法中使用多个测试用例进行JUnit测试

下一篇: 如何编写动态测试用例