JUnit与TestNG
时间:2020-02-23 14:35:25 来源:igfitidea点击:
JUnit和TestNG是Java应用程序最流行的测试框架。
这两个框架都非常好并且易于使用。
因此,在为您的应用程序选择测试框架时,最好是对一个或者另一个功能具有较高的了解,然后根据您的项目要求做出明智的决定。
JUnit与TestNG
JUnit的当前版本是5.2.0,并且仍在不断发展并努力包含更多功能。
TestNG当前版本为6.14.3,已经成熟并且功能丰富。
下表比较了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 |