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
€66.52
List Price:  €95.03
You save:  €28.51
£55.29
List Price:  £78.99
You save:  £23.70
CA$98.01
List Price:  CA$140.02
You save:  CA$42.01
A$108.26
List Price:  A$154.67
You save:  A$46.40
S$94.28
List Price:  S$134.69
You save:  S$40.41
HK$544.68
List Price:  HK$778.14
You save:  HK$233.46
CHF 62.24
List Price:  CHF 88.92
You save:  CHF 26.68
NOK kr781.73
List Price:  NOK kr1,116.81
You save:  NOK kr335.07
DKK kr496.25
List Price:  DKK kr708.95
You save:  DKK kr212.70
NZ$119.48
List Price:  NZ$170.70
You save:  NZ$51.21
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳8,430.24
List Price:  ৳12,043.71
You save:  ৳3,613.47
₹5,907.88
List Price:  ₹8,440.19
You save:  ₹2,532.31
RM313.59
List Price:  RM448
You save:  RM134.41
₦117,896.75
List Price:  ₦168,431.15
You save:  ₦50,534.40
₨19,601.13
List Price:  ₨28,002.81
You save:  ₨8,401.68
฿2,456.64
List Price:  ฿3,509.64
You save:  ฿1,053
₺2,405.01
List Price:  ₺3,435.88
You save:  ₺1,030.86
B$408.16
List Price:  B$583.12
You save:  B$174.95
R1,278.38
List Price:  R1,826.34
You save:  R547.95
Лв129.87
List Price:  Лв185.54
You save:  Лв55.66
₩98,547.64
List Price:  ₩140,788.38
You save:  ₩42,240.74
₪262.83
List Price:  ₪375.48
You save:  ₪112.65
₱4,119.38
List Price:  ₱5,885.08
You save:  ₱1,765.70
¥10,926.10
List Price:  ¥15,609.38
You save:  ¥4,683.28
MX$1,438.75
List Price:  MX$2,055.45
You save:  MX$616.69
QR257.25
List Price:  QR367.52
You save:  QR110.26
P957.42
List Price:  P1,367.80
You save:  P410.38
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,455.33
List Price:  E£4,936.40
You save:  E£1,481.07
ብር8,611.91
List Price:  ብር12,303.26
You save:  ብር3,691.35
Kz63,807.12
List Price:  Kz91,156.94
You save:  Kz27,349.81
CLP$68,583.90
List Price:  CLP$97,981.20
You save:  CLP$29,397.30
CN¥506.99
List Price:  CN¥724.30
You save:  CN¥217.31
RD$4,249.83
List Price:  RD$6,071.45
You save:  RD$1,821.61
DA9,380.49
List Price:  DA13,401.28
You save:  DA4,020.78
FJ$159.39
List Price:  FJ$227.72
You save:  FJ$68.32
Q545.12
List Price:  Q778.78
You save:  Q233.65
GY$14,759.55
List Price:  GY$21,085.97
You save:  GY$6,326.42
ISK kr9,759.40
List Price:  ISK kr13,942.60
You save:  ISK kr4,183.20
DH701.60
List Price:  DH1,002.33
You save:  DH300.73
L1,265.07
List Price:  L1,807.32
You save:  L542.25
ден4,092.25
List Price:  ден5,846.32
You save:  ден1,754.07
MOP$565.22
List Price:  MOP$807.49
You save:  MOP$242.27
N$1,267.59
List Price:  N$1,810.92
You save:  N$543.33
C$2,596.85
List Price:  C$3,709.94
You save:  C$1,113.09
रु9,521.24
List Price:  रु13,602.36
You save:  रु4,081.11
S/267.43
List Price:  S/382.06
You save:  S/114.63
K283.56
List Price:  K405.11
You save:  K121.54
SAR262.97
List Price:  SAR375.69
You save:  SAR112.72
ZK1,929.77
List Price:  ZK2,756.93
You save:  ZK827.16
L331.05
List Price:  L472.96
You save:  L141.90
Kč1,682
List Price:  Kč2,402.96
You save:  Kč720.96
Ft27,138
List Price:  Ft38,770.23
You save:  Ft11,632.23
SEK kr772.63
List Price:  SEK kr1,103.81
You save:  SEK kr331.17
ARS$69,869.10
List Price:  ARS$99,817.28
You save:  ARS$29,948.18
Bs487.60
List Price:  Bs696.60
You save:  Bs209
COP$315,227.33
List Price:  COP$450,344.06
You save:  COP$135,116.73
₡36,031.09
List Price:  ₡51,475.19
You save:  ₡15,444.10
L1,781.45
List Price:  L2,545.04
You save:  L763.59
₲550,680.18
List Price:  ₲786,719.69
You save:  ₲236,039.51
$U2,973.65
List Price:  $U4,248.26
You save:  $U1,274.60
zł288.46
List Price:  zł412.11
You save:  zł123.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.