skip to content

Map kotlin.test's @BeforeTest, @AfterTest, and @Ignore to their JUnit 5 equivalents, and explain how @Ignore behaves at the class level versus the function level.

level: middleimportance: should knowfreq 40%

answer

  1. @BeforeTest=@BeforeEach, @AfterTest=@AfterEach
  2. @Ignore=@Disabled
  3. no common @BeforeClass/@BeforeAll in kotlin.test
  4. class-level @Ignore skips whole class
  5. skipped != passed; stays visible in report

basics

~10 s

@BeforeTest is like JUnit 5's @BeforeEach, @AfterTest is like @AfterEach, and @Ignore is like @Disabled. @Ignore on a class skips every test in it; on one function it skips just that test.

solid answer

~30 s

kotlin.test's lifecycle annotations map to JUnit 5 Jupiter as: @BeforeTest -> @BeforeEach (run before each test), @AfterTest -> @AfterEach (run after each test). kotlin.test deliberately has no common per-class @BeforeClass/@AfterClass equivalent. @Ignore maps to JUnit 5's @Disabled. Placed on a single test function, @Ignore skips only that function; placed on the class, it skips the entire class's tests. Skipped tests are reported as skipped/disabled, not as passes or failures, so they stay visible in reports. On the JVM these mappings happen via kotlin.test's actual typealiases to the corresponding Jupiter annotations.

go deeper

for a junior

Knows @Ignore skips a test and that there are before/after hooks.

for a middle

Produces the correct JUnit 5 mapping and the class-vs-function @Ignore scoping.

for a senior

Explains the absence of a common per-class hook and the platform reason behind it.

for a principal

Reasons about reporting semantics, CI quarantine strategy, and why visible skips beat deleted tests.

## The mapping table | kotlin.test (common) | JUnit 5 (Jupiter) | JUnit 4 | Runs | |---|---|---|---| | `@Test` | `@Test` | `@Test` | the test case | | `@BeforeTest` | `@BeforeEach` | `@Before` | before **each** test | | `@AfterTest` | `@AfterEach` | `@After` | after **each** test | | `@Ignore` | `@Disabled` | `@Ignore` | skips the test | On the JVM these are realized through kotlin.test's `actual typealias` declarations to the chosen framework's annotations. ## Per-test, not per-class `@BeforeTest`/`@AfterTest` are the **per-test** hooks — they run once around every `@Test`, giving each test a fresh fixture. kotlin.test's common API intentionally does **not** expose a per-class (`@BeforeClass`/`@BeforeAll`) hook, because that lifecycle isn't uniform across all platform runners. If you need once-per-class setup, do it in the JVM source set with the framework's own annotation, or use a top-level `val`/lazy initializer. ## @Ignore scope ```kotlin import kotlin.test.Test import kotlin.test.Ignore @Ignore // skips EVERY test in this class class FlakyIntegrationTest { @Test fun a() { /* skipped */ } @Test fun b() { /* skipped */ } } class UnitTest { @Test fun works() { /* runs */ } @Ignore // skips ONLY this function @Test fun notReady() { /* skipped */ } } ``` - **Function-level `@Ignore`** — skips just that test method. - **Class-level `@Ignore`** — skips all tests in the class. Skipped tests are reported as **skipped/disabled**, keeping them in the report (a visible reminder) rather than silently vanishing like a deleted or commented-out test. ## Why prefer @Ignore over commenting out A commented-out test disappears from the suite; an `@Ignore`'d test still shows up as skipped, so it isn't forgotten and can carry a reason in the JUnit 5 case (`@Disabled("flaky on CI")`). Note kotlin.test's common `@Ignore` does not take a message argument.

  • What's the JUnit 4 equivalent of @BeforeTest?
    @Before. JUnit 4's @Before runs before each test, matching @BeforeTest's per-test lifecycle.
  • How would you do once-per-class setup in a multiplatform test?
    There's no common per-class hook; use a JVM-only source set with @BeforeAll, or a lazily initialized top-level property shared across tests.

saying these in an interview costs you the question

  • Mapping @BeforeTest to @BeforeAll/@BeforeClass instead of @BeforeEach
  • Saying @Ignore on a class only affects the first test
  • Claiming kotlin.test common API has a @BeforeClass equivalent
  • Thinking skipped tests are reported as passing

context