In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "case analysis of TestNG and JUnit testing framework in Java". In daily operation, I believe that many people have doubts about the case analysis of TestNG and JUnit testing framework in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "case analysis of TestNG and JUnit testing framework in Java". Next, please follow the editor to study!
What is a unit test?
Testing is not a single activity, but covers a variety of test scenarios. It is classified in different ways, one of which is based on the test level, such as integration, unit, and system testing. Goal-based application testing includes mobile testing, Web, and hybrid testing.
Unit testing involves testing the tiniest code in a software product. The goal is to check that the quality of each component of the code performs as expected. It is executed during the development phase. A piece of code is isolated to ensure its validity and accuracy. A single component of code can be a function, module, object, or method. Consider any software engineering; unit tests are always performed before integration tests. It helps identify and resolve errors early in the application development life cycle. Developers use different unit test frameworks to create automated test cases for unit tests. There are different tools on the market to perform unit tests, such as JUnit, NUnit, PHPUnit, JMockit, and so on.
The company uses different unit testing frameworks to manage test costs and improve speed, efficiency, and accuracy. Among Selenium-Java, JUnit and TestNG are the most popular unit testing frameworks.
JUnit was launched in 1997 as an open source unit testing framework based on Java. It is part of XUnit, and XUnit represents a series of unit testing frameworks. It allows developers to write and run repeatable tests. Together with Selenium, it is widely used to write Web automated tests. Its latest programmer-friendly version is JUnit 5, which creates a strong foundation for developer-based testing on the Java virtual machine.
TestNG is also a Java-based unit testing framework, developed on the same line of JUnit in 2007, but with new and improved features. These new features include flexible test configuration, parameter support, data-driven testing, annotations, various integrations, and so on. TestNG performs unit, end-to-end, and integration tests. TestNG generates reports to help developers understand the pass, failure, and skip status of all test cases. With TestNG in Selenium, you can use the estng-failed.xml file to run failed tests separately to run only failed test cases. According to mvnrepository.com, as of February 2021, the latest version of TestNG is 7.4.0.
You must understand the difference between the TestNG and JUnit testing frameworks to determine which of the two automated testing frameworks works best for your project.
The difference between TestNG and JUnit
Although there is no obvious difference between TestNG and JUnit, they are both state-of-the-art Java-based automation frameworks with their own advantages and disadvantages. However, let's try to understand the main differences between JUnit and TestNG frameworks in Selenium WebDriver:
1. Test suite
The test suite consists of a set of JUnit and TestNG tests that allow you to execute tests at the same time. The test suite feature is not allowed in earlier versions of JUnit, but it was introduced in JUnit 5, and this feature is always present in TestNG. Although both have test suites, there are key differences in the way they perform tests on each suite. Let's take a look at the code snippet that shows how the test suite runs in both frameworks.
The test suite in TestNG runs from the XML file:
In JUnit, annotations such as @ RunWith and @ Suite are used, as shown in the following code snippet. The two classes JUnit 1 and 2 are written using the annotation @ Suite.
RunWith (Suite.class) @ Suite.SuiteClasses ({JUnit1.class, JUnit2.class}) public class JunitTest5 {/ / code}
2. Notes
It is easier for testers to use TestNG because it gives them several options to use the test suite. For example, you can execute a test suite by bundling classes into groups.
The annotations used in the JUnit and TestNG frameworks work in a similar way, but with slightly different names. The following table shows the differences between JUnit and TestNG comments.
The feature wing 5 test marks the method as the test method @ Test@Test, which executes @ BeforeAll@BeforeClass before the first test method of the class, and executes after all the test methods of the current class have been executed. @ AfterAll@AfterClass it executes @ BeforeEach@BeforeMethod before each test method and @ AfterEach@AfterMethod after each test method it executes before all tests in the suite are run. Does not apply to @ BeforeSuite it executes after all tests in the suite have been run. Does not apply @ AfterSuite executes before testing. Does not apply to execution after @ BeforeTest test. @ AfterTest does not apply before the first test method of any of these groups. @ BeforeGroups does not apply after the first test method of any of these groups. Does not apply to @ AfterGroups ignore test @ Disabled (@ ignore in JUnit4) @ Test (Enable=false) expected exception @ Test (expected=ArithmeticException0.class) @ Test (expectedException)
= ArithmeticException.class) pause @ TimeOut@Test (timeout = 1000)
In JUnit 4, the @ BeforeClass and @ AfterClass methods are considered static, but there is no such restriction in TestNG.
JUnit
TestNGTestNG vs. JUnit-- test case management
Managing test execution is an important task; TestNG makes this task easier than JUnit. TestNG achieves this with the help of:
Grouping test cases: this is a feature that applies only to TestNG. It involves performing tasks by creating multiple groups. Each contains categories, and you can run tests in separate groups instead of orphaned tests. It uses parameters in the @ Test annotation.
@ Test (groups= {"groupname1",..,})
Ignore testing: you don't need to perform some tests from a large test suite, especially if you only want to test specific functionality. This feature clarifies whether specific unit tests should be ignored or considered. Both JUnit and TestNG are equipped with this feature, as well as all the comments discussed earlier. In JUnit, this feature uses @ ignore annotations: in TestNG, groups are included in the XML file under the "groups" tag and can be easily identified under the or tag.
@ Ignorepublic void method1 () {/ / code}
In TestNG, it runs using the @ Test (enabled = false) annotation.
@ Test (enabled=false) public void TestWithException () {/ / code}
Parameterization: this means populating the values / parameters each time the test is executed. As a result, we get improved code reusability. It is a data-driven test that reduces the length of code and improves its readability. TestNG and JUnit provide this feature in a different way. TestNG has an easy way to fix the parameters in the test case. It leverages the @ Parameter annotation and adds parameters to the given test method. The value of the browser is declared in the XML file (such as testng.xml), while JUnit uses the @ ParameterizedTest annotation.
Dependency testing: this feature shows when one test method depends on another. JUnit does not support this feature, but TestNG does. Because TestNG is up to date, it supports many types of testing. In TestNG, the dependency method uses the @ DependsOnMethods annotation.
@ Test (dependsOnMethods = {"LoginBrowser"}) / / code
Exception testing: this feature verifies the exception to use when an error is encountered during test execution. Both TestNG and JUnit provide this functionality, but the way exceptions are handled is slightly different. TestNG uses the expectedException parameter in the @ Test annotation
In JUnit, assertThrows API is used for exception handling
@ Test (expectedExceptions = ArithmeticException.class) public void DivideByZero () {int I = 10 + 0;}
Timeout testing: this allows the timeout function in test execution to set a time limit, and when it is exceeded, the test automatically fails. Both TestNG and JUnit provide this functionality in the same syntax.
@ Test (expected = ArithmeticException.class) public void DivideByZero () {int I = 10apper0;} TestNG and JUnit-parallel test execution
One of the most effective testing methods is to perform parallel tests. Running tests simultaneously rather than sequentially on a cloud-based Selenium Grid supports more parallelism than running it on a local Selenium Grid. Both TestNG and JUnit 5 support parallel testing.
TestNG and JUnit-report
Report is an important requirement to analyze the test results in the test. TestNG generates HTML and index reports by default. JUnit does not create any such reports for test execution, but instead provides data in XML file format. You must use an additional external plug-in with JUnit to create the report.
TestNG and JUnit-Community support
TestNG and JUnit are both popular frameworks in their respective communities. JUnit was introduced before TestNG, which is why it has relatively broader and stronger community support. TestNG is gradually catching up, and its user base is growing faster every day.
At this point, the study on "case analysis of TestNG and JUnit testing framework in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.