Understanding Dependent Test Methods

10 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.68
List Price:  €93.84
You save:  €28.15
£56.66
List Price:  £80.95
You save:  £24.28
CA$95.92
List Price:  CA$137.04
You save:  CA$41.11
A$108.49
List Price:  A$154.99
You save:  A$46.50
S$95.39
List Price:  S$136.27
You save:  S$40.88
HK$548.43
List Price:  HK$783.50
You save:  HK$235.07
CHF 63.83
List Price:  CHF 91.19
You save:  CHF 27.36
NOK kr770.92
List Price:  NOK kr1,101.37
You save:  NOK kr330.44
DKK kr490.07
List Price:  DKK kr700.13
You save:  DKK kr210.06
NZ$118.44
List Price:  NZ$169.21
You save:  NZ$50.76
د.إ257.05
List Price:  د.إ367.23
You save:  د.إ110.18
৳7,675.43
List Price:  ৳10,965.37
You save:  ৳3,289.94
₹5,833.71
List Price:  ₹8,334.23
You save:  ₹2,500.52
RM334.55
List Price:  RM477.95
You save:  RM143.40
₦87,974.63
List Price:  ₦125,683.43
You save:  ₦37,708.80
₨19,463.37
List Price:  ₨27,806
You save:  ₨8,342.63
฿2,592.21
List Price:  ฿3,703.32
You save:  ฿1,111.11
₺2,281.84
List Price:  ₺3,259.92
You save:  ₺978.07
B$361.60
List Price:  B$516.59
You save:  B$154.99
R1,346.51
List Price:  R1,923.67
You save:  R577.16
Лв128.31
List Price:  Лв183.31
You save:  Лв54.99
₩96,439.05
List Price:  ₩137,775.97
You save:  ₩41,336.92
₪264.71
List Price:  ₪378.18
You save:  ₪113.46
₱4,028.06
List Price:  ₱5,754.62
You save:  ₱1,726.55
¥10,834.45
List Price:  ¥15,478.45
You save:  ¥4,644
MX$1,198.27
List Price:  MX$1,711.89
You save:  MX$513.62
QR255.04
List Price:  QR364.36
You save:  QR109.32
P968.68
List Price:  P1,383.88
You save:  P415.20
KSh9,413.65
List Price:  KSh13,448.65
You save:  KSh4,035
E£3,365.09
List Price:  E£4,807.48
You save:  E£1,442.38
ብር3,981.35
List Price:  ብር5,687.89
You save:  ብር1,706.54
Kz58,437.56
List Price:  Kz83,485.81
You save:  Kz25,048.25
CLP$66,674.57
List Price:  CLP$95,253.47
You save:  CLP$28,578.90
CN¥507.20
List Price:  CN¥724.60
You save:  CN¥217.40
RD$4,125.63
List Price:  RD$5,894.01
You save:  RD$1,768.38
DA9,411.41
List Price:  DA13,445.45
You save:  DA4,034.04
FJ$159.39
List Price:  FJ$227.71
You save:  FJ$68.32
Q544.08
List Price:  Q777.29
You save:  Q233.21
GY$14,631.85
List Price:  GY$20,903.54
You save:  GY$6,271.69
ISK kr9,872.78
List Price:  ISK kr14,104.58
You save:  ISK kr4,231.80
DH709.20
List Price:  DH1,013.18
You save:  DH303.98
L1,247.93
List Price:  L1,782.83
You save:  L534.90
ден4,043.10
List Price:  ден5,776.11
You save:  ден1,733
MOP$564.45
List Price:  MOP$806.39
You save:  MOP$241.94
N$1,335.12
List Price:  N$1,907.39
You save:  N$572.27
C$2,574.01
List Price:  C$3,677.32
You save:  C$1,103.30
रु9,327.88
List Price:  रु13,326.11
You save:  रु3,998.23
S/257.70
List Price:  S/368.16
You save:  S/110.46
K265.79
List Price:  K379.71
You save:  K113.92
SAR262.52
List Price:  SAR375.04
You save:  SAR112.52
ZK1,806.09
List Price:  ZK2,580.25
You save:  ZK774.15
L326.86
List Price:  L466.96
You save:  L140.10
Kč1,659.60
List Price:  Kč2,370.96
You save:  Kč711.36
Ft25,883.91
List Price:  Ft36,978.60
You save:  Ft11,094.68
SEK kr762.07
List Price:  SEK kr1,088.71
You save:  SEK kr326.64
ARS$61,049.92
List Price:  ARS$87,217.91
You save:  ARS$26,167.99
Bs484.99
List Price:  Bs692.87
You save:  Bs207.88
COP$273,464.60
List Price:  COP$390,680.46
You save:  COP$117,215.86
₡35,005.20
List Price:  ₡50,009.58
You save:  ₡15,004.37
L1,726.48
List Price:  L2,466.51
You save:  L740.02
₲518,221.78
List Price:  ₲740,348.57
You save:  ₲222,126.78
$U2,696.53
List Price:  $U3,852.35
You save:  $U1,155.82
zł283.79
List Price:  zł405.43
You save:  zł121.64
Already have an account? Log In

Transcript

Hello all Welcome back. In this one, we'll take a look at dependent test cases. So basically, we can have test cases which depends on each other, right, we saw something a little bit in the lecture where we discussed about priority. But there could be an actual scenario where one of my test case is actually dependent on other test case to finish. And it only makes sense to run that test case if the previous test case is done completed, right? In those kind of scenarios, we can actually make a test case dependent on a different test case, and test center gives us that power and then test it we'll make sure that the dependent test case will only run if the previous test case is done executing.

So this gives us a very good option. Now let's take an exam look at the example I've created the file called test and j underscore dependent test. And then we have a setup, this is a before class. And then this is an after class. And I should just remove this is just from a previous class. So now we have these two classes, sorry, these two methods for before class after class.

And then we have four test method, test method 1234. Now, let's say my test method one depends on test method two. So by default, they'll run in the order of one test method 1234. But if my test method one depends on test method, two, and this method three depends on test method one, then I can actually make them dependent and let's test whether four is independent. It doesn't need any other test case to run. So we can actually leave it so we don't have to give priority to every single test method.

That's a very good option. We don't maybe I have hundred test cases and don't want to give to give a priority to all Hundred test cases like 01234, and all that. So what I can do here is I can only add an attribute for the test cases which are really dependent and I can leave everything aside. So other test cases would just run in any order. But my dependent test cases would be run in the order, not exactly the order, but they will run based on their requirement. If test method one has to be run before test method two, then it will run based on the dependent attribute that we will provide.

Now you remember that we had a class to test some class to test and now we already have the set of classes and everything before we were actually creating the object and every test method. So now what we can do here is we can actually create the objects in the before class, so we don't have to create object in every test method. What I can do here is, I can first I have to write the input statement. So game board, app code dot some class to test. And then I can create an object here. So some class to test, and then OBJ equals to new, some class to test now this is an object created.

So sorry, I should not be creating the object here. So I should only be defining it here. And then I should be creating the object inside the before class. And then this guy is setup. So I created the object and the before class. Now what I can do here is I can use that object and every method so I'm just going to call a couple of maybe one test here.

So maybe we will use end result, and we will call OBJ dot. Some numbers. This is the most simple one and I'm going Some one and two, so we expect an output as three. So let's do assert dot assert. Sorry, for the type of assert dot assert equals, and any type we're gonna do and expected three, actual three. So it should run a test case, sorry, I should just put the result here.

So expected is the result. And, sorry, actual is the result and expected is three, what I'm expecting is three. And result is going to give me the sum of these numbers returned from the method. So obviously, one and two, if the method is correct, it's gonna return one plus two is three. So this is just a sample test method. I have used it for a particular purpose that I'll show you.

So now let's make this method one depends on test method two depends on test method one. So what I can use here is that depends on methods. So this is the attribute that I can use and in curly braces, and we can use double quotes, we can provide the name of the method where it depends. So you know what actually, I should be doing the other way. So, I shouldn't make test method, one dependent on test method two, just to showcase that it is honored. Otherwise test method one is obviously going to run before test method two because of the name alphabetical order.

And let's copy these statements down to our test method two. So now we have it like this. So what we are Here is our test method one is dependent on test method two. And now let's make this guy test method three depends on test method one, which makes sense, right. So now test method should do should execute first then test method one and then test method three. So and test method four is independent children just wherever it feels like.

So now let me just right click, and run the code, run as test in G test. And everything is successful test method tour and first, then four, because four can take any place and then one so it doesn't have to run exactly after two. test method one ran after two in between any other method can execute. So four are executed then one got executed and three depends on one. So then three got executed. So test Angie on So, whatever we provided here, and it will make sure that the dependent test method will execute afterwards.

So one was dependent on two. So that's why it got executed afterwards. Now let's take a scenario what happens. So currently, everything was successful. Everything is green to four, one and three. And now let's make this also may be dependent on one.

So this guy depends on maybe three. So now this is gonna run at the last, so two, and this guy doesn't depend on anybody. And then this one depends on one and this one depends on three. Now let's make sure that test method two fails. Let's do it. So instead of three, I'm expecting let's say two and then it will fail.

Then let's run the test case then as a test engine test case and what happens so, this method got fail this method two is fail it is expecting to but it found three which is actually correct. And at the bottom we can say, we can see that test method one got skip test method three got skip, and test method for God skip because they were all dependent on each other. And if this method got fail, then the dependent method will be skipped. The dependent one is not failed, it's just skipped. And if I remove it from test method for let's say, just for fun, we remove it then we will see a different output. So now, what happens is test method four is executed it is passed because it was not dependent on anything.

Test methods always fail. Obviously we failed it intentionally. One is skipped because by is dependent on two and three escape because three is dependent on one. Finally, we can see here, four test methods were there, one failed and two escaped. That's what we can see here also. So this is the output.

Now, let's say that I still want to run my other test cases, I don't want to skip them, maybe let them fail if there is some issue, but I don't want to skip them. So what we can do here is we can actually use the always run attribute. So this is actually very helpful always run equals to true. So if I put always run for test method one, it is dependent on test method two, even if the test method two fails, it should run test method one. Let me just right click and run it. And now let's see the output.

So what happens is this method For a successful one still got executed. Because we say we still want to run it. Please always run, it doesn't matter if test method to fail, or it's successful, still run it. And since test method three was dependent on test method one, it's ran test method three also and finally test method to fail. And that's what we intentionally fail. So I wanted to show you two things here that we can make test cases dependent.

And if test method, the main test method fails, then the dependent test method will be escaped. But if we still want to run it, always, then we can actually use always done attribute. So that's about it. Please download the code, practice and let me know if you have any questions. I'll see in the next lecture, guys, and 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.