In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to understand TestNG parameterization". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
A set of parameterized data
Define parameterized data as follows:
@ DataProvider public Object [] [] dp1 () {return new Object [] [] {new Object [] {1jue 1,0}, new Object [] {2,1jue 1}, new Object [] {2,1Magne2},};}
Parameterized data (defined by dp1) is used in the use case, and the code is as follows:
@ Test (dataProvider = "dp1") public void F1 (Integer N1, Integer N2, Integer n3) {Integer result=n2+n3; assertEquals (N1 result);}
Multiple sets of parametric data
In a test class, you can define multiple sets of parameterized data (different number of parameterized data, three dp1, two dp2). The code is as follows:
@ DataProvider public Object [] [] dp2 () {return new Object [] [] {new Object [] {1,1}, new Object [] {2,1}, new Object [] {2,1},};}
Parameterized data (defined by dp2) is used in the use case, and the code is as follows:
@ Test (dataProvider = "dp2") public void f2 (Integer N1, Integer N2) {assertEquals (N1 meme N2);}
The test method is referenced by DataProvider
We can customize some methods to read external files, and then reference the read data as parameters in TestNG, as follows:
The specific code for writing a method to read all kinds of files (txt, excel, db) is not given, but be sure to note that the return value of the method must be Object [] [] (the knowledge of manipulating excel will be described in a later article).
Public Object [] [] readfile (String p_file) {return new Object [] [] {{1,1}, {1,2}};}
Call this method in a method modified by @ DataProvider (or you can implement it directly)
@ DataProvider (name = "dp_func") public Object [] [] testData (Method testMethod) {return readfile (");}
Parameterized data (dp_func) is used in the use case, with the following code:
@ Test (dataProvider = "dp_func") public void f3 (Integer N1, Integer N2) {assertEquals (N1 meme N2);}
Complete code
Import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; import java.lang.reflect.Method; public class NewTest {@ DataProvider public Object [] [] dp1 () {return new Object [] [] {new Object [] {1rect 1,0}, new Object [] {2,1jue 1}, new Object [] {2,1jue 2},} @ DataProvider public Object [] [] dp2 () {return new Object [] [] {new Object [] {1,1}, new Object [] {2,1}, new Object [] {2,1},} } @ DataProvider (name = "dp_func") public Object [] [] testData (Method testMethod) {return readfile ("");} / / reads all kinds of files. The return value is Object [] [] public Object [] [] readfile (String p_file) {return new Object [] [] {{1,1}, {1,2}}]. } @ Test (dataProvider = "dp1") public void F1 (Integer N1, Integer N2, Integer N2) {Integer result=n2+n3; assertEquals (N1 dp2) public void f2 (Integer N1, Integer N2) {assertEquals (N1 dp_func N2);} @ Test (dataProvider = "dp_func") public void f3 (Integer N1, Integer N2) {assertEquals (N1);}
When you run the test case, you can see that the use case was successfully parameterized, with F1 executed 3 times, f2 executed 3 times, and f3 executed 2 times, as shown in the following figure:
That's all for "how to understand TestNG Parameterization". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.