JUnit与TestNG

时间:2020-02-23 14:35:25  来源:igfitidea点击:

JUnit和TestNG是Java应用程序最流行的测试框架。
这两个框架都非常好并且易于使用。
因此,在为您的应用程序选择测试框架时,最好是对一个或者另一个功能具有较高的了解,然后根据您的项目要求做出明智的决定。

JUnit与TestNG

JUnit的当前版本是5.2.0,并且仍在不断发展并努力包含更多功能。
TestNG当前版本为6.14.3,已经成熟并且功能丰富。

下表比较了JUnit与TestNG在不同功能上的差异。
请注意,我排除了这两个框架中都存在的非常特定或者常见的功能,例如测试异常,超时设置,生命周期回调方法等。

FeatureJUnit 5TestNGConclusionReferences
AnnotationsAnnotations BasedAnnotations BasedBoth JUnit 5 and TestNG are annotation based and they are similar in nature and behavior.JUnit Annotations, TestNG Annotations
Ease of UseJUnit 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 featuresTestNG is better in terms of ease of use.JUnit Maven Dependency, TestNG Maven Dependency
IDE SupportSupported on major IDEs such as Eclipse and IntelliJ IDEASupported on major IDEs such as Eclipse and IntelliJ IDEABoth of them are similar and provides easy integration with major IDEs.JUnit Tests Eclipse, TestNG Eclipse Plugin
Data ProviderSupports 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 dataJUnit Parameterized Tests, TestNG DataProvider, TestNG Parameters
Test SuiteJUnit 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 suitesTestNG @Factory, TestNG XML Suite
HTML ReportsWe need external plugin maven-surefire-report-plugin to generate HTML reportsTestNG 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 MethodWe 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
AssertionsJUnit provides enough assertion methods to compare expected and actual test resultsTestNG provides enough assertion methods to compare expected and actual test resultsBoth of them are similar in terms of Assertions supportJUnit Assertions
AssumptionsJUnit supports assumptions to skip tests based on certain conditionsTestNG doesn't support assumptionsJUnit is better if you want to skip tests based on conditionsJUnit Assumptions
Test OrderJUnit 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 attributeTestNG is better when you want to execute tests in specific order.
Disable TestsJUnit supports many ways to disable and enable tests. For example, based on OS, JRE, system propertiesTestNG supports disabling tests but it's limited in functionalityJUnit is better when you want to disable or enable tests based on conditionsJUnit Disable Tests, TestNG Disable Tests
Parallel ExecutionJUnit 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
ListenersJUnit 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