TestNG Soft Asserts

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 take a look at soft assertions provided by testan. g, it is really a very nice topic and very interesting. Let me just bring up Eclipse. And let's take a look. So what I did here is I just created a class called test Angie underscore soft assert.

And there's one test method which tests some. And it's printing something. And we created one class in previous lectures, which is some class to test. And what I'm doing here is I'm just calling the methods, some numbers from it and capturing the result. Now, we already learned, asserts the normal asserts, and that's how it works, write, test, assert dot assert equals, and these are the things we use. So let me just copy it here and be what happens with a cert is let's say this.

I use this code. Now, some numbers, I know one comma two will return me three, and I'm expecting three with the result, right? Let's say I am expecting to just to intentionally fail this test case. So it's going to fail the test case, right? And let's copy one more statement, and this one is successful. This is still expecting three.

So what I'm trying to say here is, imagine you have more than one assertion points in your test method. And if the first one fails, then your second assertion will not run because that's how asserts work. If any verification point of the assert fails, it assumes the test method is failed, and it skips out of the method it does not actually run the further lines at all. So this line is not even run in assertion. If we use assertions, let's actually just try to run this one and we will see it. You know what, let's add one print statement here.

And it will actually tell us that this is the line after assert one and we will copy one more. This will tell this is the line after assert. So if everything prints, then we know everything is getting printed, everything is getting executed. So run as test in G test. And it failed. What happened?

The test is failed, we know that we expected it. Only line that was printed is running tests to test some and then it just failed the test case. So the only line that was printed was this one. This assertion failed. So this line was not printed. And obviously, this line was not executed this line was not executed if I provide the correct expected result, which means I am now intentionally making the test case to pass, then everything will be printed, see, running Test, test some line after a third one in line after a third to.

So everything is printed here, everything was run fine. But that's not what I want, right? Maybe I might have a scenario where if my assertion the first assertion fails, I still want to run my code lines for in the test method. And I still want to see what happens to the second assertion and the remaining assertions. So at least we know, there are no other issues after the first assertion. Otherwise, what will happen is, we'll fix the first assertion we'll run the test again and then we figure out okay, there's again, there's again, an issue with assertion number 10.

So why don't we just have the capability to run everything at once and if Figure out where the issue is. So for that test, Angie has provided us soft assert. And I'm gonna remove this thing because it's already covered in the assert place. So we will import the soft assert. So I'm going to type or dot test and d dot assert dot soft assert. So this is the package that we want to import.

And we'll get rid of this line. And we'll get rid of this line. So it works a little different than normal asserts. What we have to do here is we will have to create an object for the softer cert so we can actually use it. So I'm gonna say soft cert and we'll give the name as S equals two New and soft asset. So this is the object that we created.

Now, what we will do here is we will do s a.us equals. So basically we have the same things available here. So our result is this and we are expecting to let's say we are expecting two because we want to fail this one. And this one, we know the output the output should be three, one plus two is three, but we are expecting two so we know that it's going to fail right. Now, let's copy this line, put it here and this time let's expect the correct output. And now let's run the test case.

What's gonna happen here is interesting. It ran the test case it says the test case is successful even though there is one failure right. And we know the failure. So running Test, test test some and the line after a certain one line after that, so everything is executed. Everything is executed, even though this assert should have failed, it did not fail the test case, and it executed all the lines from the code of this test method. and here also we see everything is successful.

But you must be wondering, even that is not good, right? It's good that it executed all the lines, but still, we want some way that should tell us that there is an issue with the test case. Otherwise, we would not know that there is an issue with the test method and we will never fix it. So for that, what functionality here is, we will say, as a dot, assert all this is the magic This statement is gonna do everything for us. what it's gonna do here is it's gonna run the complete code. And when it says a start all it's gonna figure out if any of the asserts from the test method fail.

And if there is even a single assert failure, then this method will fail. test method. So let's right click run as test in G test. And now we see the magic. So it ran the complete test case running test tests, line after assert one line after assert, which means everything is run. And then it tells us the following assert fail and be expected to but the actual result was three.

And from the logs, we can actually figure out where it fails. So it says test and soft assert dot java line number 20. And this line failed, right? This is coming from basically the Assad. So we know what happened. We figured out how we can fail the test case also.

And we figured out how we can run the complete test case also. So basically, test case test method is the same thing. So now with the help of soft assert, we can run the complete test method We can still know the output and the result of the test method. Right? So that's about it. Please download the code files practices as much as possible.

And let me know if you have any questions. I'll always be more than happy to help. And I'll see in the next lectures thanks for ending 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.