简体   繁体   English

Junit测试创建其他测试

[英]Junit test that creates other tests

Normally I would have one junit test that shows up in my integration server of choice as one test that passes or fails (in this case I use teamcity). 通常情况下,我会在我选择的集成服务器中显示一个junit测试作为一个通过或失败的测试(在这种情况下,我使用teamcity)。 What I need for this specific test is the ability to loop through a directory structure testing that our data files can all be parsed without throwing an exception. 我需要进行此特定测试的是能够遍历目录结构测试,我们的数据文件都可以在不抛出异常的情况下进行解析。

Because we have 30,000+ files that that 1-5 seconds each to parse this test will be run in its own suite. 因为我们有30,000多个文件,解析这个测试每个1-5秒将在自己的套件中运行。 The problem is that I need a way to have one piece of code run as one junit test per file so that if 12 files out of 30,000 files fail I can see which 12 failed not just that one failed, threw a runtimeexception and stopped the test. 问题是我需要一种方法让一个代码作为每个文件的一个junit测试运行,这样如果30个文件中的12个文件失败,我可以看到哪个12个失败而不仅仅是那个失败,抛出了一个运行时异常并停止了测试。

I realize that this is not a true "unit" test way of doing things but this simulation is very important to make sure that our content providers are kept in check and do not check in invalid files. 我意识到这不是一种真正的“单元”测试方式,但这种模拟对于确保我们的内容提供商得到检查并且不检查无效文件非常重要。

Any suggestions? 有什么建议么?

I think what you want is parameterized tests. 我想你想要的是参数化测试。 It's available if you're using JUnit4 (or TestNG). 如果您使用的是JUnit4(或TestNG),则可以使用它。 Since you mention JUnit, you'll want to look at the @RunWith(Parameterized.class) and @Parameters annotations' documentation. 由于您提到了JUnit,因此您需要查看@RunWith(Parameterized.class)@Parameters annotations文档。

I'd write one test that read all the files, either in a loop or some other means, and collected all the failed files in a collection of some kind for reporting. 我会编写一个测试来读取所有文件,无论是循环还是其他方式,并收集某些集合中的所有失败文件进行报告。

Maybe a better solution would be a TestNG test with a DataProvider to pass along the list of file paths to read. 也许更好的解决方案是使用DataProvider进行TestNG测试,以传递要读取的文件路径列表。 TestNG will create and run one test for each file path parameter passed in. TestNG将为传入的每个文件路径参数创建并运行一个测试。

A Junit3 answer: Create a TestSuite, that creates the instances of the TestCases that you need, with each TestCase initialized according to your dynamic data. Junit3答案:创建一个TestSuite,创建所需的TestCase实例,每个TestCase根据您的动态数据进行初始化。 The suite will run as a whole within a single JVM instance, but the individual TestCases are independent of each other (setUp, tearDown get called, the error handling is correct, reporting gives what you asked for, etc). 该套件将在单个JVM实例中作为一个整体运行,但各个TestCase彼此独立(setUp,tearDown调用,错误处理正确,报告提供您要求的内容等)。

The actual implementation can be a bit clumsy, because TestCase conflates the Name of the test with the METHOD to be run, but that can be worked around. 实际的实现可能有点笨拙,因为TestCase将测试的名称与要运行的METHOD混淆,但可以解决这个问题。

We normally just combine the suite with the dynamic testcases in the same class, and use the suite() method to get the TestSuite. 我们通常只将该套件与同一类中的动态测试用例相结合,并使用suite()方法获取TestSuite。 Ant's JUnit task is smart enough to notice this, for example. 例如,Ant的JUnit任务足够聪明,可以注意到这一点。

public class DynamicTest extends TestCase {
   String filename ;

   public DynamicTest ( String crntFile ) {
      super("testMethod");
      filename = crntFile ;
   }

   // This is gross, but necessary if you want to be able to
   // distinguish which test failed - otherwise they all share
   // the name DynamicTest.testMethod.
   public String getName() {
      return this.getClass().getName() + " : " + filename ;
   }



   // Here's the actual test
   public void testMethod() {
      File f = new File( filename ) ;
      assertTrue( f.exists() ) ;
   }

   // Here's the magic
   public static TestSuite suite() {

      TestSuite s = new TestSuite() ;

      for ( String crntFile : getListOfFiles() ) {
          s.addTest( new DynamicTest(crntFile ) ) ;
      }

      return s ;
   }
}

You can, of course, separate the TestSuite from the TestCase if you prefer. 当然,如果您愿意,可以将TestSuite与TestCase分开。 The TestCase doesn't hold up well stand alone, though, so you'll need to have some care with your naming conventions if your tests are being auto-detected. 但是,TestCase不能很好地保持独立性,因此如果您的测试是自动检测的,那么您需要注意一下您的命名约定。

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

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