In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
I read an English post on the Internet, explaining the difference between JUnit and TestNG framework. It's good. I'll translate it here.
1. Support for annotation classes
Function
JUnitTestNG is marked as a class / method and test classes and methods @ Test@Test are marked to run before all tests in suite
No @ BeforeSuite marked as running without @ AfterSuite after all tests in suite marked as running without @ BeforeTest before testing as running without @ AfterTest after testing as running without @ BeforeGroups before the first test method in test Group as running without @ AfterGroups after the last test method in test Group as current before running @ BeforeClass@BeforeClass after the first test method in the current test class Running @ AfterClass@AfterClass after the last test method in the test class is marked as running @ Before@BeforeMethod before each test method and running @ After@AfterMethod after each test method to ignore a test. Let it not execute @ Ignore@Test (enable=false) expect the test to throw any exception @ Test (expected=XXXException.class) @ Test (exceptedExceptions=XXXException.class) test timeout, if the test execution time exceeds the time set in milliseconds, then stop the test and mark it as a failure
@ Test (timeout=1000) @ Test (timeout=1000)
two。 Test parameterization
Both TestNG and JUnit have different parameterized setting functions, but they have different forms of expression. Parameterized settings mean that parameters can be passed into the test unit externally when the test is run.
For TestNG, there are two parts: one is to use @ Parameters (value=XXX) to indicate what parameters the test needs when constructing the test code, which has the advantage of making the test more flexible and can get different expected results according to different parameters, such as:
Public class TestNGTest6_1_0 {@ Test @ Parameters (value= " Number ") public void parameterIntTest (int number) {System.out.println (" Parameterized Number is: "+ number) }}
Then, in our XML file, we set the specific values of the parameters as follows:
So, what if the parameters we're giving are complex and we can't "push" the test method in a simple String or atomic type in the XML file? At this point, we are going to use the "pull" model, we use a DataProvider attribute to declare the data it needs on the method under test, and then in parallel, use the @ DataProvider annotation to mark a method, so that the return value of this method returns specific data, so that the two of us are perfect.
For example: the following code declares that our test class needs a DataProvider:
@ Test (dataProvider = "Data-Provider-Function") public void parameterIntTest (TestNGTest6_3_0 clzz) {System.out.println ("Parameterized Number is:" + clzz.getMsg ()); System.out.println ("Parameterized Number is:" + clzz.getNumber ());}
Then we label a method with @ DataProvider accordingly, which provides the data:
/ / This function will provide the patameter data @ DataProvider (name = "Data-Provider-Function") public Object [] [] parameterIntTestProvider () {TestNGTest6_3_0 obj = new TestNGTest6_3_0 (); obj.setMsg ("Hello"); obj.setNumber (123) Return new Object [] {{obj}};}
3. Test dependencies:
For JUnit, all the tests are independent and independent of each other.
But for TestNG, we can make the tests dependent on each other by using the dependsOnMethods attribute to identify a dependent test method:
Test public void method1 () {System.out.println ("This is method1");} @ Test (dependsOnMethods= {"method1"}) public void method2 () {System.out.println ("This is method2");}
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.