DataProviders - Part 1

8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.26
List Price:  €93.23
You save:  €27.97
£55.95
List Price:  £79.94
You save:  £23.98
CA$95.75
List Price:  CA$136.79
You save:  CA$41.04
A$107.23
List Price:  A$153.20
You save:  A$45.96
S$95.15
List Price:  S$135.94
You save:  S$40.78
HK$547.94
List Price:  HK$782.80
You save:  HK$234.86
CHF 63.91
List Price:  CHF 91.30
You save:  CHF 27.39
NOK kr766.40
List Price:  NOK kr1,094.90
You save:  NOK kr328.50
DKK kr486.75
List Price:  DKK kr695.39
You save:  DKK kr208.64
NZ$117.46
List Price:  NZ$167.81
You save:  NZ$50.35
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,670.29
List Price:  ৳10,958.03
You save:  ৳3,287.74
₹5,832.01
List Price:  ₹8,331.80
You save:  ₹2,499.79
RM334.37
List Price:  RM477.70
You save:  RM143.32
₦88,678.72
List Price:  ₦126,689.32
You save:  ₦38,010.60
₨19,457.85
List Price:  ₨27,798.13
You save:  ₨8,340.27
฿2,591.43
List Price:  ฿3,702.20
You save:  ฿1,110.77
₺2,274.88
List Price:  ₺3,249.97
You save:  ₺975.09
B$360.25
List Price:  B$514.66
You save:  B$154.41
R1,330.75
List Price:  R1,901.16
You save:  R570.40
Лв127.68
List Price:  Лв182.41
You save:  Лв54.73
₩96,191.02
List Price:  ₩137,421.64
You save:  ₩41,230.61
₪265.67
List Price:  ₪379.55
You save:  ₪113.87
₱4,044.82
List Price:  ₱5,778.57
You save:  ₱1,733.74
¥10,886.62
List Price:  ¥15,552.99
You save:  ¥4,666.36
MX$1,193.70
List Price:  MX$1,705.36
You save:  MX$511.66
QR254.90
List Price:  QR364.16
You save:  QR109.26
P969.33
List Price:  P1,384.82
You save:  P415.49
KSh9,413.65
List Price:  KSh13,448.65
You save:  KSh4,035
E£3,352.26
List Price:  E£4,789.16
You save:  E£1,436.89
ብር3,973.96
List Price:  ብር5,677.33
You save:  ብር1,703.36
Kz58,616.62
List Price:  Kz83,741.62
You save:  Kz25,125
CLP$66,678.07
List Price:  CLP$95,258.47
You save:  CLP$28,580.40
CN¥507.14
List Price:  CN¥724.52
You save:  CN¥217.38
RD$4,110.44
List Price:  RD$5,872.31
You save:  RD$1,761.87
DA9,401.90
List Price:  DA13,431.87
You save:  DA4,029.96
FJ$160.42
List Price:  FJ$229.18
You save:  FJ$68.76
Q543.26
List Price:  Q776.12
You save:  Q232.86
GY$14,630.45
List Price:  GY$20,901.55
You save:  GY$6,271.09
ISK kr9,795.80
List Price:  ISK kr13,994.60
You save:  ISK kr4,198.80
DH709.23
List Price:  DH1,013.23
You save:  DH303.99
L1,246.59
List Price:  L1,780.93
You save:  L534.33
ден4,020.35
List Price:  ден5,743.61
You save:  ден1,723.25
MOP$563.94
List Price:  MOP$805.67
You save:  MOP$241.72
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,572.43
List Price:  C$3,675.06
You save:  C$1,102.63
रु9,316.74
List Price:  रु13,310.19
You save:  रु3,993.45
S/259.29
List Price:  S/370.43
You save:  S/111.14
K265.67
List Price:  K379.55
You save:  K113.87
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,825.88
List Price:  ZK2,608.51
You save:  ZK782.63
L324.76
List Price:  L463.96
You save:  L139.20
Kč1,644.30
List Price:  Kč2,349.11
You save:  Kč704.80
Ft25,615.12
List Price:  Ft36,594.60
You save:  Ft10,979.47
SEK kr760.35
List Price:  SEK kr1,086.27
You save:  SEK kr325.91
ARS$61,118.20
List Price:  ARS$87,315.45
You save:  ARS$26,197.25
Bs482.97
List Price:  Bs689.99
You save:  Bs207.01
COP$272,384.35
List Price:  COP$389,137.18
You save:  COP$116,752.82
₡35,055.79
List Price:  ₡50,081.85
You save:  ₡15,026.05
L1,725.31
List Price:  L2,464.83
You save:  L739.52
₲519,197.15
List Price:  ₲741,742.01
You save:  ₲222,544.85
$U2,684.12
List Price:  $U3,834.62
You save:  $U1,150.50
zł281.58
List Price:  zł402.27
You save:  zł120.69
Already have an account? Log In

Transcript

Hello all welcome back to the next one. And this one will learn a very interesting concept of testan G, and that's known as data providers. So let's, what I've done here is I've created a class called test mg underscore data providers on the left here. And that's this class. I don't have a lot of things here, just the class name. And I created one test method named it as test method one.

And it's printing two things here, input one and input two. And you can see here that the method takes two arguments string input one and string input two. That's the current setup. Now let's talk about data providers and when we need it, so we have seen that sn has parameters which can provide the values from the XML file, and we can instead of hard coding the values and test methods, we can provide them from XML file. But what if there is a scenario that is You want to test something multiple times with different set of data. Let's say first time you test this method with ABC and D EF.

And then you want to test it with CBE and XYZ. So basically, these are two different set of data. But you only have one test method, I'm pretty sure that you don't want to write different test methods for different data sets, right? You want to write one test method, which tests which test the functionality. So basically, test method should focus on the functionality of the application, then somehow, we should be able to pass different kind of inputs to the same test method. And we should be able to execute that test method with all those different set of input data.

So basically, let's say if I take an example of web automation, I have Login page, let's imagine and I want to test the login functionality with different data set. Once that is valid, I have a valid username and password. And this applies to rest case also, if we have REST API testing, or even for back end testing, because we log in pretty much everywhere. So we have different multiple data sets. One is valid set where we have a valid login and valid password. So my output should be I should be able to log in.

And then for that valid set, I may have different kinds of input like one is the username with the maximum available characters one username with the minimum available characters because a lot of times user name has limit, like the character could be between eight to 12. And the same thing is with password also, a lot of websites or rest API's have a limit of password that password could be between 10 to 20 characters. So we may have different data set. I may have one password With 10 characters, and then I have another password with 20 characters then another password I may have with special characters inside it. So it's pretty common to have different data sets for the same functionality. And what we want to understand is how do we test all those things with just one single method.

So here comes test NGS data provider. And now let's take a look at it, how we can do it. So what I'm doing here is one test method. And we already saw it accepts two inputs. So I'm gonna first write a method called public object arrays. So basically, it's a 2d array, and it returns a 2d array.

That's why we don't have a type as wide, we have a type as object array. And I'm going to name it as get data. We can name this method anything. So we are creating this method which is gonna give us the data for our test map. So since it returns an object to the array, we're gonna return a new object and 2d array. And inside it, I'm gonna write the arrays.

So let's see the first input. So we can provide different set of data, right? So input one, and input two is one set of data for them, both of them, let's provide two values. So I am providing BMW and M three. So let's say this is my first data set, BMW is input one, and M three is input. So that's how it goes sequentially.

And then I'll provide the second set of data. Maybe I want to run my test case with a different car value. So let's say Audi, and I'm going to provide a six. And let's take one more set of data. So this time I'm going to take maybe Ben And I don't know, maybe c 300. So C 300.

So now what we have here is, and we will end this written thing with a semicolon and we save the file and everything is fine. So what we have is, we have three rows here, right? So, three rows means we have three set of data. So this test case is gonna run three times with these data sets. So first data set is BMW M three, then the second one is already a six, and the third one is Benz c 300. Now how do we say that it's a data provider?

What we do here is we add the attribute here, we add the annotation. So at the rate data provider, so I'm going to type I type da and hit control space. And then I will select at the rate data provider from our dot test ng g dot annotations and I will given name to the data provider. And this name could be anything. Since I'm keeping this as inputs or data for my this method, I'm just giving it a name inputs. And you can actually give it inputs ABC, XYZ, whatever you like.

But we have to give a name to this data provider. So now we have a name. Now how does this add the rate test annotation or this test method knows that I need to use this get data method as my data input, so it knows with the help of the attributes, so inside at the rate test annotation, we'll use parentheses here, and we'll type data and so please notice these small here and then hit control space. And we'll use data provider equals two and then here we'll give the name so these two names should match. This method name can be anything but these two names should match. Once this is done, The method test method one knows that I need a data provider with the name inputs, and everything will be fine.

So let's right click and run this code file as test in G test and see the output. So what I have here is, let's first see this guy. So I had one method only test method one, and we can see that this method was run three times, right? First time it was run with the value BMW and M three. Second time it was run with the values already and asex. And third time it was run with the values Benz and C 300.

So the same method was executed three times. That's the power of data provider. And now let's see the console. We can see what we are printing up here we are just printing the inputs. So basically, it's going to take the inputs here, right. So input one and input two First time is BMW and M three.

Then again, second time, the inputs are already NSX. Third time the inputs are Benz and C 300. And it says that it passed three methods and it shows the values. And here also we can see there are three methods. So that's about it. That's pretty cool, right?

Data providers are really powerful, and we can really use them in lots of different ways. So please download the code file and practice it. Let me know if you have any questions, and I'll see you in the next lectures. Thanks for attending this one. Thanks.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.