In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Briefly describe several ways of parameterization of testng:
@ Parameters
DataProvider
If the parameters used are relatively simple, you can use @ Parameters and define the parameters in an .xml file, such as creating a new testng.xml.
Considering the following test scenario, I want to open the 51cto website and quit
I can define url in testng.xml.
Then create a new PPTest class under the pp package:
Package pp
Import org.openqa.selenium.WebDriver
Import org.openqa.selenium.firefox.FirefoxDriver
Import org.testng.annotations.Optional
Import org.testng.annotations.Parameters
Import org.testng.annotations.Test
/ * *
* @ author Anthony
*
, /
Public class PPTest {
Public WebDriver driver
/ * *
* @ param domainURL @ Optional is to use the value in Optional if you don't read url in xml.
, /
@ Parameters ({"url"})
@ Test
Public void ppTest (@ Optional ("http://www.baidu.com/") String domainURL) {
Driver = new FirefoxDriver ()
Driver.get (domainURL)
Driver.quit ()
}
}
@ Parameters can be placed in the following location:
(1) anywhere that has been annotated by @ Test, @ Before/After or @ Factory.
(2) at most one constructor tag is placed in the test class. In this way, TestNG can instantiate this class with specific parameters in testng.xml when needed. This property can be used to initialize values in some classes so that they can be used later by other methods in the class.
Note:
The parameters in XML are mapped in the order in which the Java parameters appear in the annotation, and if the number does not match, TestNG will report an error.
The parameter has a scope. In testng.xml, you can declare either under the tag or below. If both parameters have the same name, the one defined in takes precedence. This is convenient when you need to override the values of specific parameters in some tests.
DataProvider returns a two-dimensional array of Object, and each one-dimensional array in the two-dimensional array is passed to the calling function as an argument. When running, you will find that the number of times that the test method identified by @ Test is executed is the same as the number of one-dimensional arrays contained in object [] [], and the number of parameters of the function identified by @ Test is also the same as the number of elements in the one-dimensional array in object.
Declare that the data source can look like this:
Package com.example.testdata
Import org.testng.annotations.DataProvider
/ * *
* @ author Anthony
*
, /
Public class TestProvider {
@ DataProvider (name = "account")
Public static Object [] accountInfo () {
Return new Object [] [] {new Object [] {"Gurema", "1234qwer"}
New Object [] {"Gulei", "1234qwer"}}
}
}
When called, you can do this:
Import org.testng.Assert
Import org.testng.annotations.Test
Import com.example.testdata.TestProvider
/ * *
* @ author Anthony
*
, /
Public class TestPractice {
@ Test (dataProvider = "account", dataProviderClass = TestProvider.class)
Public void testAgain (String userName, String password) {
System.out.println ("I'm test again")
Assert.assertEquals ("Gurema", userName)
}
}
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.