In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
[technology Salon 002] data Center: construction practice of Credit Agile data Center | Credit Technology Salon will be broadcast live online at 8: 00 p.m. on May 23. Click to sign up.
TestNG is an open source automated testing framework, and "NG" stands for the next generation (the initials of Next Generation). TestNG is similar to JUnit (especially JUnit 4), but it is not an extension of the JUnit framework. Compared with Junit, it is more powerful and easier to use, and is more suitable for testers to conduct integration testing or interface regression testing.
I. the characteristics of TestNG
Using java and object-oriented features
The name of the method does not have to be limited to a fixed format, and the method to be executed can be identified by comments.
Some of the parameters needed in the method can be passed through comments
Comments are strongly typed, so there are errors that can be reflected at compile time
Support group testing, dependency testing, parallel testing, load testing, etc.
Support for multithreaded testing II. TestNG commonly used annotation type annotation description @ Test marks the class or method as part of the test @ BeforeSuite only once before all tests in the suite are run on the annotated method. @ AfterSuite runs @ BeforeClass only once after all tests of the suite are run on annotated methods, and annotated methods run @ AfterClass only once after calling all test methods of the current class Annotation methods that run @ BeforeTest annotated methods only once will run @ AfterTest annotated methods before all test methods belonging to classes within the tag run. @ BeforeGroups this method ensures that before running the first test method belonging to any of these groups, the method is called @ AfterGroups. This method ensures that after running all test methods belonging to any of these groups, the method called @ BeforeMethod annotated method will execute @ AfterMethod before each test method. The annotated method will execute @ DataProvider after each test method to provide test data. If a test method wants to receive data from this DataProvider, it must use a DataProvider@Parameters whose name is equal to the annotation name to introduce how to pass parameters to the test method. 3. TestNG parametric testing
TestNG provides two ways to pass parameters.
The first kind: testng.xml
The advantage of this approach is that it separates the code from the test data and is easy to maintain; the disadvantage is that it is no longer applicable if the parameters you need to pass are not of the basic java type, or if the required values can only be created at run time.
The second kind: @ DataProvider
This can provide more complex parameters (also known as data-driven testing).
The second is used in our project.
As shown in the figure: first, add the property dataProvider with the value of WithdrawBatchQueryLoopData to the test method, and then provide a @ DataProvider method corresponding to name. The specific implementation of this method is to read data from the configuration file or database.
4. TestNG grouping test
Grouping testing is an innovative feature of TestNG. Grouping testing allows us to conduct a variety of flexible tests without recompiling when we want to run multiple sets of different tests in parallel.
Grouping can be applied to methods (a method can belong to one or more groups) or to a class. When applied to a class, all public methods in that class become test methods, and even if they are not annotated, you can continue to repeat @ Test annotations on methods that need to add attributes. There are many forms of testing methods for group execution:
You can specify the specific grouping to be performed by configuring it in testng.xml
You can also set the grouping to be performed by integrating it into jenkins and by parameterizing the build.
You can also set the grouping to be performed by running Run Configurations
One thing to note: the Group tag will invalidate @ BeforeMethod
The purpose of @ BeforeMethod is to indicate that the annotated method is executed once before each test method is run. For example:
@ BeforeMethodPublic void beforeMehod () @ TestPublic void testCase1 () @ TestPublic void testCase2 ()
The order of normal execution is: beforeMehod- > testCase1- > beforeMehod- > testCase2
However, @ BeforeMethod becomes invalid after putting testCase into a Group
@ BeforeMethodPublic void beforeMehod () @ Test (groups = "group1") Public void testCase1 () @ Test (groups = "group1") Public void testCase2 ()
Execute the script again, and the execution order changes to testCase1- > testCase2
Solution:
1. Add @ BeforeMethod to group1 as well
2. Set the property alwaysRun=true of @ BeforeMethod
5. TestNG dependency testing
We often encounter dependencies between multiple interfaces to be tested, that is, the execution of an interface depends on the returned results of the previous interface. For example, when executing a batch query, batch payment needs to be performed first, then TestNG dependency testing is used. TestNG uses dependsOnMethods with alwaysRun to set dependencies between test methods, and dependsOnGroups to set dependencies between groups.
Strong dependency: before the test method runs, all dependent methods must run and succeed, and even if there is only one failure, the test method will not be called (skip rather than fail)
Soft dependency (alwaysRun=true): test methods are always executed after dependent methods are run, even if some dependent methods fail. VI. Expected exception test of TestNG
In testing, sometimes we expect some code to throw an exception.
TestNG judges expected anomalies through @ Test (expectedExceptions) and judges Error Message
7. TestNG test report
After the test case is executed, the test report is generated under the project's test-output (default directory)
Open the index.html file to see a summary of the test results, including: suite name, number of test case successes, number of test case failures, number of test case omissions, execution time and testng.xml file.
If all the test cases are successful, the test results will be marked with a green background, and if the test cases fail, the test results will be marked with a red background. The test reports generated by testNG are not very beautiful and can be beautified with testng-xslt.
Author: Wang Haiyan
Source: Yixin Institute of Technology
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: 268
*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.