Video summary
Session 43 - Selenium with Java | TestNG Annotations | Types Of Assertions
Main summary
Key takeaways
Main Ideas and Lessons
- The session continues TestNG basics by introducing TestNG annotations (especially lifecycle annotations) and then explains assertions (validations) in TestNG.
- Key takeaway: Annotations control execution order. Only methods annotated with
@Testare treated as “test methods” and appear as passed/failed in reports. Other annotated helper methods may run, but they won’t show as test results unless they are@Test. - After understanding annotation scopes, the session explains why assertions are preferred over plain
if/elsechecks for correct pass/fail reporting. - It distinguishes between hard assertions and soft assertions, including their behavioral differences and when to use them.
Methodical Concepts: TestNG Annotation Hierarchy & Usage
1) Core @Test Annotation (Main Tests)
- A method is treated as a test method only if it is annotated with
@Test. - Optional:
prioritycan be specified; TestNG uses it to decide execution order among test methods.
Validation rule
- Put actual validations inside
@Testmethods. - Helper methods (e.g., login/logout) should not contain validations if you don’t want them to appear in reports as test results.
2) @BeforeMethod / @AfterMethod (Runs Before/After Each @Test)
Use case demonstrated
- Repeated steps needed around each test method, such as:
- Entry (e.g., login) before each test
- Exit (e.g., logout) after each test
Example scenario
- Two tests:
searchandadvancedSearch - Login/logout act as prerequisites/teardown and are repeated around each test.
Execution behavior
- If there are N test methods in the class:
@BeforeMethodruns N times@AfterMethodruns N times
Implementation idea from the session
- Mark the main tests with
@Test, e.g.:search(priority example)advancedSearch(priority example)
- Mark prerequisite/teardown once in code but let TestNG repeat it:
@BeforeMethodforlogin()@AfterMethodforlogout()
3) @BeforeClass / @AfterClass (Runs Once Per Class)
Use case demonstrated
- Shared setup/teardown used by both main tests:
- login only once before any test methods in the class
- logout only once after all test methods in the class
Execution behavior
- For a given class:
@BeforeClassruns once@AfterClassruns once
- This happens regardless of the number of
@Testmethods in that class.
Rule highlighted
- It’s not mandatory to use both:
- You may use only
@BeforeClassif you don’t need explicit teardown. - Use
@AfterClassonly if you must perform actions after all tests in the class.
- You may use only
4) @BeforeTest / @AfterTest (Runs Once Per TestNG <test> in XML)
Important conceptual point (from XML hierarchy)
- In XML, a
<test>tag groups one or more classes. - Therefore:
@BeforeTest/@AfterTestoperate at the XML<test>level, not the Java class level.
Execution behavior described
- If XML has:
- one
<test>containing classesC1,C2:@BeforeTestruns once before those classes execute@AfterTestruns once after they finish
- two
<test>entries (Test1 and Test2), each with different classes:@BeforeTestruns once per XML test@AfterTestruns once per XML test
- one
Method placement
@BeforeTest/@AfterTestmethods can be placed in any of the involved classes (e.g.,C1orC2).- TestNG decides timing based on annotation + XML grouping.
5) @BeforeSuite / @AfterSuite (Runs Once Per Suite in XML)
Use case
- Suite-level actions before/after running all tests contained in a suite.
Execution behavior
@BeforeSuiteruns once at the start of the suite@AfterSuiteruns once at the end of the suite
Session note
- Teams often use multiple separate suites (multiple XML files), so before/after suite usage is rarer in practice.
6) Final Hierarchy (Execution Level Concept)
The session summarizes the hierarchy as:
- Suite level:
@BeforeSuite/@AfterSuite - Test level:
@BeforeTest/@AfterTest - Class level:
@BeforeClass/@AfterClass - Method level:
@BeforeMethod/@AfterMethod - Test method level: methods annotated with
@Test
Key reporting principle
- Reports primarily reflect the status of
@Testmethods (not helper methods), even though helper methods run during execution.
Assertions: Validations and Reporting Correctness
Why assertions are needed
- Earlier approach used
if/else:- compare
expectedTitlevsactualTitle - print “test passed/failed”
- compare
- Problem:
- Even if your
ifcondition fails, TestNG may still mark the@Testmethod as PASS unless you explicitly fail it using TestNG assertions.
- Even if your
Assertions ensure
- pass/fail status is correctly reflected in TestNG reports.
Hard Assertions vs Soft Assertions
A) Hard Assertions (Common Case)
- Implemented using TestNG’s
Assertclass. - These assertions are static calls (no object needed), for example:
Assert.assertEquals(...)Assert.assertNotEquals(...)Assert.assertTrue(...)Assert.assertFalse(...)Assert.fail()
Core behavior
- If a hard assertion fails:
- the test method effectively stops further execution
- remaining lines/prints after the failing assertion do not run
Common hard assertion types described
- Equality / inequality:
assertEquals(expected, actual)assertNotEquals(expected, actual)
- Boolean checks:
assertTrue(conditionOrExpected, message?)assertFalse(condition)
- Explicit failure:
fail()to force failure immediately
B) Soft Assertions (Continue After Failures)
- Implemented via a
SoftAssertobject (not static). - Example pattern:
SoftAssert softAssert = new SoftAssert();- use soft assertion methods like:
softAssert.assertEquals(...)softAssert.assertTrue(...)- etc.
- IMPORTANT: call at the end:
softAssert.assertAll();
Core behavior
- If a soft assertion fails:
- execution continues for remaining steps
- But correct failure reporting happens only when you call:
assertAll()at the end
Important rule emphasized
- Without
assertAll(), soft assertions may not properly fail the test in results.
Speakers / Sources Featured
- No explicit individual speakers are identified in the subtitles.
- Source: The content appears to be from a YouTube/TestNG tutorial video titled:
- “Session 43 - Selenium with Java | TestNG Annotations | Types Of Assertions”
Rate this summary
Your feedback will help improve summaries.
Improve this summary
Reprocess with a stronger model when the summary feels incomplete or inaccurate.
Translate summary in another language
Ask questions to this video
Chat for follow-up questions, clarifications, and source-backed answers.