Testing / Cucumber Interview Questions
What is the difference between global hooks and tagged (conditional) hooks?
A hook with no tag expression applies unconditionally to every single scenario in the suite. A tagged hook only runs for scenarios matching its associated tag expression, letting different categories of scenarios receive different setup or teardown behavior without every hook needing to apply universally.
@Before public void globalSetup() { // runs before every scenario in the entire suite testStartTime = System.currentTimeMillis(); } @Before("@ui") public void uiSetup() { // runs only before scenarios tagged @ui driver = new ChromeDriver(); } @After("@ui") public void uiTeardown() { // runs only after scenarios tagged @ui driver.quit(); }
Tagged hooks are what make it practical to mix genuinely different kinds of scenarios (API tests, UI tests, database-only tests) in the same project without every scenario paying the setup cost of every other category's needs; a purely API-focused scenario shouldn't have to spin up a browser just because some unrelated @Before hook applies globally.
More Related questions...