🧪 JUnit5 & TestNG — Test Frameworks
JUnit5 & TestNG — Test Frameworks: A test framework is an "automatic referee" that discovers your tests, runs them in order, reports results, and performs setup-teardown via hook
A test framework is an "automatic referee" that discovers your tests, runs them in order, reports results, and performs setup-teardown via hooks like `@BeforeEach`/`@AfterEach` — like a race referee who lines runners up, blows the start whistle, times the finish, and enforces the same rule for everyone: you only write the test, and the referee manages all the remaining choreography. But if you could run tests in a plain `main` method with `if (result != expected) System.out.println("error")`, why do you need JUnit5/TestNG? Because hand-written checks stop at the first failure, do not report which test passed or failed, and do not integrate with CI; the framework runs each test in isolation and produces a green/red report. JUnit5 stands out with its modular, modern assertion API while TestNG is strong in `@DataProvider` and advanced parallel execution — the choice depends on your employer's stack. For a QA engineer this difference has concrete consequences: in a 50-scenario regression suite the framework reports each scenario independently, so one broken test does not crash the entire run and you instantly see which feature actually regressed.
JUnit5 Core Annotations
JUnit5 — Basic structure
Micro Lab: Code practice
Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.
Which TestNG annotation runs before each test method?
@BeforeMethod runs before each test method — the equivalent of @BeforeEach in JUnit5.
Which TestNG annotation should be used for a setup method that needs to run only once before the entire class is initialized?
@BeforeClass runs once before any test methods in the current class are executed. It is the equivalent of @BeforeAll in JUnit5.
🎬 The Lifecycle of a @Test Method
Test 2: If driver Stays Dirty
If a test class has 5 `@Test` methods, what code does JUnit5 run BEFORE and AFTER each of them? In this film you will see why @BeforeEach/@AfterEach start every test from a "clean page".
Step 1 — `@BeforeAll` runs ONLY ONCE for the class (before it starts) — typically used for an expensive, shared resource like WebDriver setup.
Step 2 — `@BeforeEach` runs BEFORE EVERY `@Test` method: for example `driver.get(loginUrl)` makes every test start from the SAME clean page.