junit testng
JUnit and TestNG are the most popular testing frameworks for Java applications. Both of these frameworks are really good and easy to use. So when it comes to chose the testing framework for your application, it’s better to have a high-level idea of what features are present in one or the other and then take the informed decision based on your project requirements.
JUnit和TestNG是Java应用程序最流行的测试框架。 这两个框架都非常好并且易于使用。 因此,在为您的应用程序选择测试框架时,最好是对一个或另一个中存在哪些功能有一个高层次的了解,然后根据您的项目需求做出明智的决定。
JUnit与TestNG (JUnit vs TestNG)
JUnit current version is 5.2.0 and it’s still evolving and working on to include more features. TestNG current version is 6.14.3 and it’s mature and features rich.
JUnit当前版本为5.2.0,并且仍在不断发展并努力包含更多功能。 TestNG当前版本是6.14.3,它已经成熟并且功能丰富。
Below table compares JUnit vs TestNG on different features. Note that I have excluded very specific or common features that are present in both these frameworks, such as testing exceptions, timeout settings, lifecycle callback methods etc.
下表比较了JUnit与TestNG在不同功能上的差异。 请注意,我排除了这两个框架中都存在的非常特定或常见的功能,例如测试异常,超时设置,生命周期回调方法等。
Feature | JUnit 5 | TestNG | Conclusion | References |
---|---|---|---|---|
Annotations | Annotations Based | Annotations Based | Both JUnit 5 and TestNG are annotation based and they are similar in nature and behavior. | JUnit Annotations, TestNG Annotations |
Ease of Use | JUnit 5 is built into various modules, you need JUnit Platform and JUnit Jupiter to write test cases. If you want more features such as Parameterized Tests then you need to add junit-jupiter-params module. | Single module to get all TestNG features | TestNG is better in terms of ease of use. | JUnit Maven Dependency, TestNG Maven Dependency |
IDE Support | Supported on major IDEs such as Eclipse and IntelliJ IDEA | Supported on major IDEs such as Eclipse and IntelliJ IDEA | Both of them are similar and provides easy integration with major IDEs. | JUnit Tests Eclipse, TestNG Eclipse Plugin |
Data Provider | Supports multiple ways to provide test data, such as methods, Enum, CSV, CSV Files etc. | Supports data provider methods and from test suite xml file. | JUnit is better for injecting test methods input data | JUnit Parameterized Tests, TestNG DataProvider, TestNG Parameters |
Test Suite | JUnit 5 doesn’t support test suites yet, it’s work in progress as of writing this post. Follow this GitHub Issue to check the current status. | TestNG test cases are executed as test suite. We can use @Factory annotation to run multiple test classes. TestNG XML support is awesome in creating complex tests suites that are loosely coupled from your test cases. | TestNG is better for test suites | TestNG @Factory, TestNG XML Suite |
HTML Reports | We need external plugin maven-surefire-report-plugin to generate HTML reports | TestNG automatically creates HTML reports for the test run. | TestNG HTML reports look outdated but it’s simple to use. If you have to share HTML reports with others, I would suggest to use JUnit. | JUnit HTML Report, TestNG Tutorial |
Running from Java Main Method | We can use JUnit 5 launcher API to run tests from java main method. | We can use TestNG run() method to execute tests from the java main method. | Both of them supports execution of test cases from java main method. | JUnit Launcher API Docs, TestNG Tests from Java Main Method |
Assertions | JUnit provides enough assertion methods to compare expected and actual test results | TestNG provides enough assertion methods to compare expected and actual test results | Both of them are similar in terms of Assertions support | JUnit Assertions |
Assumptions | JUnit supports assumptions to skip tests based on certain conditions | TestNG doesn’t support assumptions | JUnit is better if you want to skip tests based on conditions | JUnit Assumptions |
Test Order | JUnit 5 doesn’t support test order yet, it’s planned for 5.3 release. Follow this GitHub Issue to check the current status. | TestNG supports ordering of test methods through priority attribute | TestNG is better when you want to execute tests in specific order. | |
Disable Tests | JUnit supports many ways to disable and enable tests. For example, based on OS, JRE, system properties | TestNG supports disabling tests but it’s limited in functionality | JUnit is better when you want to disable or enable tests based on conditions | JUnit Disable Tests, TestNG Disable Tests |
Parallel Execution | JUnit 5 doesn’t support parallel execution as of today. Follow this GitHub Issue to check the current status. | TestNG supports parallel execution if run through XML suite. | TestNG is better for parallel execution as of now, JUnit 5 development is going on to support this feature. | TestNG Parallel Execution |
Listeners | JUnit supports listeners through Launcher API, there is no mechanism to add listeners using annotations. | TestNG supports various types of listeners and can be added using annotations. | TestNG listener support is much better compared to JUnit 5. | TestNG Listeners |
特征 | JUnit 5 | 测试NG | 结论 | 参考资料 |
---|---|---|---|---|
注解 | 基于注释 | 基于注释 | JUnit 5和TestNG都是基于注释的,它们的性质和行为都相似。 | JUnit注释 , TestNG注释 |
使用方便 | JUnit 5内置于各种模块中,您需要JUnit Platform和JUnit Jupiter来编写测试用例。 如果需要更多功能(例如参数化测试),则需要添加junit-jupiter-params模块。 | 单个模块即可获得所有TestNG功能 | 就易于使用而言,TestNG更好。 | JUnit Maven依赖性 , TestNG Maven依赖性 |
IDE支持 | 在主要的IDE(例如Eclipse和IntelliJ IDEA)上受支持 | 在主要的IDE(例如Eclipse和IntelliJ IDEA)上受支持 | 两者都很相似,并且可以轻松与主要的IDE集成。 | JUnit测试Eclipse , TestNG Eclipse插件 |
资料提供者 | 支持多种提供测试数据的方式,例如方法,枚举,CSV,CSV文件等。 | 支持数据提供者方法和测试套件xml文件中的数据。 | JUnit更适合注入测试方法输入数据 | JUnit参数化测试 , TestNG DataProvider , TestNG参数 |
测试套件 | JUnit 5目前还不支持测试套件,在撰写本文时,它还在进行中。 按照此GitHub问题检查当前状态。 | TestNG测试用例作为测试套件执行。 我们可以使用@Factory批注来运行多个测试类。 TestNG XML支持在创建与测试用例之间松散耦合的复杂测试套件方面非常出色。 | TestNG更适合测试套件 | TestNG @Factory , TestNG XML套件 |
HTML报告 | 我们需要外部插件maven-surefire-report-plugin来生成HTML报告 | TestNG自动为测试运行创建HTML报告。 | TestNG HTML报告看起来已经过时了,但是使用起来很简单。 如果您必须与他人共享HTML报告,则建议使用JUnit。 | JUnit HTML报告 , TestNG教程 |
从Java Main方法运行 | 我们可以使用JUnit 5启动器API从Java main方法运行测试。 | 我们可以使用TestNG run()方法从java main方法执行测试。 | 它们都支持从java main方法执行测试用例。 | JUnit Launcher API文档 , 来自Java Main方法的TestNG测试 |
断言 | JUnit提供了足够的断言方法来比较预期的测试结果和实际的测试结果 | TestNG提供了足够的断言方法来比较预期和实际测试结果 | 两者在断言支持方面相似 | JUnit断言 |
假设条件 | JUnit支持基于特定条件跳过测试的假设 | TestNG不支持假设 | 如果要根据条件跳过测试,则JUnit更好 | JUnit假设 |
测试订单 | JUnit 5目前还不支持测试订单,它计划在5.3版本中发布。 按照此GitHub问题检查当前状态。 | TestNG通过优先级属性支持测试方法的排序 | 当您要按特定顺序执行测试时,TestNG更好。 | |
禁用测试 | JUnit支持多种禁用和启用测试的方法。 例如,基于OS,JRE,系统属性 | TestNG支持禁用测试,但功能有限 | 当您想根据条件禁用或启用测试时,JUnit会更好 | JUnit禁用测试 , TestNG禁用测试 |
并行执行 | 到目前为止,JUnit 5不支持并行执行。 按照此GitHub问题检查当前状态。 | 如果通过XML套件运行,TestNG支持并行执行。 | 到目前为止,TestNG更适合并行执行,JUnit 5开发正在支持该功能。 | TestNG并行执行 |
听众 | JUnit通过Launcher API支持侦听器,没有使用注释添加侦听器的机制。 | TestNG支持各种类型的侦听器,可以使用注释添加。 | 与JUnit 5相比,TestNG侦听器支持要好得多。 | TestNG侦听器 |
摘要 (Summary)
Above features comparison of TestNG vs JUnit should help you in finding the perfect fit for your application. Both of these are good testing frameworks or java application. However, JUnit 5 is still evolving and it will have all the missing features in the upcoming releases.
TestNG与JUnit的上述功能比较应该可以帮助您找到最适合您的应用程序的方式。 两者都是好的测试框架或Java应用程序。 但是,JUnit 5仍在发展中,它将具有即将发布的版本中所有缺少的功能。
junit testng