简体   繁体   English

junit测试用例的问题! 避免代码重复

[英]issue with junit test case! avoid code duplication

I am writting jnuit test case for testing API. 我正在编写用于测试API的jnuit测试用例。

my class is as follows 我的班级如下

class MyTest extends TestCase{
    List<String>  argList;
    public MyTest(){
     //read argList from File
    }



     testMyTest(){
       //callmy api
         myApi(arg1);
       }

}

Now i want make a seperate testcase for each of the 50 args.Args are read from File. 现在我想为50个args中的每一个制作一个单独的测试用例。从File中读取Args。 I dont want to write a seperate method for calling myApi with different args.How can i do it? 我不想写一个单独的方法用不同的args调用myApi。我怎么能这样做? I dont want to write sperate methods like 我不想写像sperate这样的方法

testMyTest1(){
   //callmy api
     myApi(arg1);
   }

testMyTest1(){
   //callmy api
     myApi(arg2);
   }

您可以使用参数化测试

private static final String[] args = new String[] {.....};

@Test
public void myTest(){
   for (int i=0; i<args.length; i++){
      myApi(args[i];
   }
}

The above answers your question I think, however it is not good JUnit practice. 以上回答了我的问题,但是JUnit的做法并不好。 It is best that each test method only invokes the method under test one time with one test condition. 最好每个测试方法仅使用一个测试条件调用一次测试方法。 That way if multiple things are wrong, you get a separate error for each rather than dealing with one at a time. 这样,如果出现多个错误,每个错误都会产生一个单独的错误,而不是一次只处理一个错误。 This would suggest the following: 这表明如下:

private static final String[] args = new String[] {.....};

private void testMyTest(String arg){
    myApi(arg);
}

@Test
public void myTest0(){
  testMyTest(args[0]);
}
@Test
public void myTest1(){
  testMyTest(args[1]);
}

Probably the best mechanism is to do the first option above but using the ErrorCollector rule to allow for multiple errors to be reported. 可能最好的机制是执行上面的第一个选项,但使用ErrorCollector规则允许报告多个错误。

Edit I stand corrected, jordao's answer regarding parameterized tests is really the best way to do this. 编辑我更正了,jordao关于参数化测试的答案确实是最好的方法。

You can use a Parameterized tests or Theories (since JUnit 4.4). 您可以使用参数化测试或理论(自JUnit 4.4起)。 For more details use 有关详细信息,请使用

Unit testing usually is made with assertions. 单元测试通常使用断言进行。 You don't need to write a method for each argument, but execute different assertions based on your arguments. 您不需要为每个参数编写方法,而是根据您的参数执行不同的断言。

One way for doing it would be: 一种方法是:

class MyApiTest extends TestCase {
    List<String>  argList;

    public MyApiTest() {}

    public testMyApi() {
        assertTrue(testMyApi(arg1));
        assertFalse(testMyApi(arg2));
        assertNull(testMyApi(arg3));
        assertEquals(testMyApi(arg4), testMyApi(arg5));
    }
}

I'd even prefer using annotations, like 我甚至更喜欢使用注释,比如

class MyApiTest {
    @Before
    public setUp() {}

    @After
    public tearDOwn() {}

    @Test
    public testMyApi() {
        Assert.assertTrue(testMyApi(arg1));
        Assert.assertFalse(testMyApi(arg2));
        Assert.assertNull(testMyApi(arg3));
        Assert.assertEquals(testMyApi(arg4), testMyApi(arg5));
    }
}

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

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