-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestNG Annotations & Assertions
170 lines (119 loc) · 9.59 KB
/
TestNG Annotations & Assertions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
TestNG Annotations :- TestNG uses annotations to define & control the flow of test methods and allow you to define
the order in which your methods execute. Annotations allows you to customize the behavior of your test cases, set
up preconditions, manage resources, and define data sources for your test methods. You can use them to create
well-organized, controlled and well-documented test suites using TestNG. Some of the commonly used TestNG annotations are.....
👉 @Test :- Marks a method as a test method. Test methods are the actual test cases you want to execute.
👉 @BeforeSuite :- Indicates that a method should run before all test within a test suit XML file.
👉 @AfterSuite :- Specifies that a method should run after all test suites within a test suit XML file.
👉 @BeforeTest :- Denotes a method that should run before all test methods belonging to a <test> tag in the test suite XML.
👉 @AfterTest :- Indicates that a method should run after all test methods belonging to a <test> tag in the test suite XML.
👉 @BeforeClass :- Marks a method to run once before any test methods in the same class. Useful for setting up class-level resources.
👉 @AfterClass :- Specifies that a method should run once after all test methods in the same class. Useful for performing cleanup or finalization task for
class-level resources.
👉 @BeforeMethod :- Denotes a method that should run before each test method within the class. Useful for setting up common preconditions for multiple test methods.
👉 @AfterMethod :- Specifies that a method should run after each test method within the class. Useful for performing cleanup or resetting state between test methods.
👉 @DataProvider :- Supplies data to test methods.
➡ Example :
@DataProvider
public Object[][] testData() {
// provide test data as two dimentional array.
}
👉 @Parameters :- Provides parameters to test methods from the test suite XML.
➡Example :
@Parameters({"username", "password"})
@Test
public void loginTest(String username, String password) {
//Test logic using the provided parametes.
}
👉 @Factory :- Used to create instances of test classes. Useful for creating multiple instances of a test class with different configurations.
➡Example :
@Factory
public Object[] createInstances(){
//create and return instance of test class
}
👉 @Listeners :- Specifies listeners to be used with the test class. Listeners can monitor and report events during test execution.
➡Example:
@Listners(MyTestListeners.class)
public class MyTestClass {
//Test methods and annotations
}
To be Continued.. to topic TestNG Assertions
Irrespective of any programming language, every test automation framework offers a mechanism of assertions for validating
the end result of a test scenarios. TestNG provides an Assert class that has multiple methods to raise asserts and to
implement assertions it is important to import required package which is " org.testng.Assert ".
❄ Syntax of Assertion in TestNG :-
==> Assert.methodName( actual, expected ) ;
📌 Assert :- This is the class inbuilt in the TestNG framework.
📌 methodName :- This is the name of the Assert class method.
📌 actual :- First parameter of the assert method in which the value is passed that the user gets from the application under test.
📌 expected :- Second parameter of the assert method in which the user passes the expected value.
⚡ Types of Assertions in TestNG :-
🚀 Hard Assertion :-
Whenever a hard assertion is applied and an assertion statement fails, the assertion in TestNG throws an exception
immediately and terminates the further execution of the same test case. It will continue with the execution of the next
test case in the test suite. As soon as the hard assertion condition fails, the test case is marked as failed.
🚀 Soft Assertion :-
To implement soft assertion in TestNG, we use a SoftAssert class and its method assertAll() to throw all the exceptions
collected during the test case execution. The soft assert basically performs assertion and if a condition fails to meet,
it doesn't throw an exception immediately, instead it continues with the next statement of the same test case until the
method assertAll () gets called to throw all the caught exceptions.
🎯 When to use Hard & Soft Assertion?
🔰 Use Hard Assertion :- Scenario would be like login test scenario where if the login test fails, the test case execution must be terminated with an exception as
there is no point of moving further without logging into the system.
🔰 Use Soft Assertion :- Soft Assertion is best used in case where the test statements of a test case are not dependent on each other. E.g. if you are validating a
form in which there are multiple fields to be validated, hence it is recommended to soft assert all the fields and then call assertAll() to throw all exceptions at
the end of the test case.
✴ TestNG Assert Methods :- Most commonly used assertions in testing framework is like mentioned below..
👉 Assert.assertEqual (String actual, String expected) :- It accepts 2 parameters i.e. the actual value & expected value to validate if the actual string is equal to
the expected string. The exception is thrown if both the strings are not equal.
👉 Assert.assertEqual (String actual, String excepted, String message) :- This is similar to the assertion method mentioned above, the only difference is that this
method can accept one more string parameter as a message. In case assertion condition not met, the exception thrown along with error message passed here.
👉 Assert.assertEquals (boolean actual, boolean expected ) :- This assertion method accepts two boolean values and validates if both are equal or not.
👉 Assert.assertTrue (condition) :- This assertion method is used to assert whether the condition passed in a parameter returns true or not. If the condition returns
false, the assertion error is thrown.
👉 Assert.assertTrue (condition, message ) :- It is similar to assertion method discussed in the previous one, the only difference is that here this method accepts one
more parameter as a message. In case the assert condition is passed as false, the assertion error is thrown along with a message passed here.
👉 Assert.assertFalse (condition) :- This assertion method is used to assert whether the condition passed in a parameter returns false or not. If the condition returns
true, the assertion error is thrown.
👉 Assert.assertFalse (condition, message) :- It is similar to previous one only difference is that this method can accept one more string parameter as message. In case the assertion condition is passed as true, the assertion error is thrown along with a message passed here.
👉 Assert.assertNull (condition) :- This assertion method is used to assert whether the condition passed in a parameter returns null or not. If the condition doesn't return null, the assertion error is thrown.
👉Assert.assertNotNull (condition) :- This assertion method is used to assert whether the condition passed in a parameter returns value except null or not. If the condition returns null, the assertion error is thrown.
||Soft and hard assertions || - Difference. Important
1. Hard Assertion:
Hard assertions are the traditional assertions where the test execution stops immediately when an assertion failure occurs. If an assertion fails, the subsequent lines of code in the test method are not executed, and the test is marked as failed.
import org.testng.Assert;
import org.testng.annotations.Test;
public class HardAssertionExample {
@Test
public void testHardAssertion() {
int actualValue = 10;
int expectedValue = 20;
Assert.assertEquals(actualValue, expectedValue); // Assertion failure
System.out.println("This line will not be executed due to assertion failure");
}
}
In this example, the assertion fails, and the line of code after the assertion (`System.out.println(...)`) is not executed. Hard assertions are straightforward and effective when you want the test to stop immediately upon a failure.
2. Soft Assertion:
Soft assertions allow you to continue executing the remaining lines of code in a test method even if an assertion fails. All assertion failures are collected, and the test is marked as failed only after all assertions have been evaluated.
To use soft assertions, you typically need to create an instance of the soft assertion class provided by the testing framework (e.g., `SoftAssert` in TestNG . After completing all assertions, you call a method to assert all the collected failures.
import org.testng.asserts.SoftAssert;
import org.testng.annotations.Test;
public class SoftAssertionExample {
@Test
public void testSoftAssertion() {
SoftAssert softAssert = new SoftAssert();
int actualValue = 10;
int expectedValue = 20;
softAssert.assertEquals(actualValue, expectedValue); // Assertion failure
System.out.println("This line will be executed despite assertion failure");
// Assert all collected failures
softAssert.assertAll(); // Marks the test as failed if any assertion failed
}
}
In this example, the assertion fails, but the subsequent lines of code are still executed. The test result is marked as
failed only when "softAssert.assertAll()"" is called, indicating that all the assertions have been evaluated.
Choosing Between Hard and Soft Assertions:
- Use hard assertions when you want the test to stop immediately upon a failure. This can be helpful to prevent further
issues caused by unexpected failures.
- Use soft assertions when you want to continue executing the entire test, collecting all assertion failures before
marking the test as failed. This can be useful to gather comprehensive test results without stopping the test at the first failure.