简体   繁体   English

可继承的“通用”JUnit 4测试套件

[英]Inheritable “Generic” JUnit 4 test suite

I'd like to create a "generic" JUnit 4 test that can be used as follows: 我想创建一个“通用”JUnit 4测试,可以按如下方式使用:

  • It is an abstract base class 它是一个抽象的基类
  • It can be packaged into a jar file 它可以打包成jar文件
  • Users can include the jar as a dependency in their own projects 用户可以将jar包含在他们自己的项目中作为依赖项
  • Users can create their own JUnit tests by extending the abstract class and adding implementations for the key methods 用户可以通过扩展抽象类并为关键方法添加实现来创建自己的JUnit测试
  • The JUnit tests will then run when the users run JUnit on their own projects (along with any other JUnit tests the user may have written) 当用户在他们自己的项目上运行JUnit时 (以及用户可能编写的任何其他JUnit测试),JUnit测试将运行
  • The test suite may return multiple results from running different sub-tests , ideally the user should be able to see this breakdown of results in their IDE 测试套件可以通过运行不同的子测试返回多个结果,理想情况下,用户应该能够在IDE中看到结果的细分

The motivation for this is that the class defines a standard test procedure that may be useful in many different projects, but require some customisation by the user in each case. 这样做的动机是该类定义了一个标准测试过程,该过程可能在许多不同的项目中有用,但在每种情况下都需要用户进行一些定制。

Is this possible with JUnit 4, and if so how should I do it? 这可能与JUnit 4有关,如果是这样,我该怎么做?

Yes it's possible. 是的,这是可能的。 You would simply have to create your abstract class, package it in a jar, give this jar to the users or store it somewhere where they can download it. 您只需创建抽象类,将其打包在jar中,将此jar提供给用户或将其存储在可以下载的位置。

Users would then put the jar in their own project and create subclasses of your abstract class. 然后,用户将jar放在他们自己的项目中并创建抽象类的子类。 They would use their IDEs (or any other way) to run these test classes extending your abstract class, and the IDE would display the result of all the tests, typically using a tree. 他们将使用他们的IDE(或任何其他方式)来运行扩展抽象类的这些测试类,IDE将显示所有测试的结果,通常使用树。

This looks like a repetition of everything you asked in your question, but the way to do it is just as you have explained in your question. 这看起来像是重复了你在问题中提到的所有内容,但是这样做的方式正如你在问题中所解释的那样。 There's not much to add. 没有太多要补充的。

Perhaps this can provide you a starting point: Writing base class for your tests . 也许这可以为您提供一个起点: 为您的测试编写基类
Just pack it as every other class in a jar and you are ready to go. 只需将它打包成罐子里的其他所有类,你就可以开始了。

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

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