0
0
Kotlinprogramming~10 mins

Test fixtures and lifecycle in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Test fixtures and lifecycle
Start Test Class
BeforeAll: Setup shared resources
For each Test Method
BeforeEach: Setup test fixture
Run Test Method
AfterEach: Cleanup test fixture
Repeat for next Test Method
AfterAll: Cleanup shared resources
End Test Class
This flow shows how test fixtures are set up and cleaned up before and after tests run in a Kotlin test class.
Execution Sample
Kotlin
import org.junit.jupiter.api.*

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CalculatorTest {
    @BeforeAll
    fun setupAll() { println("Setup All") }

    @BeforeEach
    fun setup() { println("Setup Each") }

    @Test
    fun testAdd() { println("Test Add") }

    @AfterEach
    fun tearDown() { println("Tear Down Each") }

    @AfterAll
    fun tearDownAll() { println("Tear Down All") }
}
This code runs setup and teardown methods around each test and once for all tests.
Execution Table
StepAnnotationActionOutput
1@BeforeAllRun once before all testsSetup All
2@BeforeEachRun before testAddSetup Each
3@TestRun testAdd methodTest Add
4@AfterEachRun after testAddTear Down Each
5@AfterAllRun once after all testsTear Down All
6EndAll tests finishedNo more output
💡 All test lifecycle methods have run, tests complete.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
Test Class StateNot startedSetup All doneSetup Each doneTest Add doneTear Down Each doneTear Down All doneFinished
Key Moments - 3 Insights
Why does @BeforeAll run only once but @BeforeEach runs before every test?
@BeforeAll is for shared setup before any tests run (see Step 1), while @BeforeEach prepares the environment fresh for each test (see Step 2).
What happens if there are multiple test methods?
The @BeforeEach and @AfterEach methods run before and after each test method, repeating steps 2-4 for each test.
When does @AfterAll run?
@AfterAll runs once after all tests and their fixtures finish (see Step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at Step 3?
ATest Add
BSetup Each
CTear Down Each
DSetup All
💡 Hint
Check the 'Output' column for Step 3 in the execution_table.
At which step does the shared setup for all tests happen?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look for @BeforeAll annotation in the execution_table.
If a second test method is added, which lifecycle methods run again before it?
A@BeforeAll only
B@AfterAll only
C@BeforeEach and @AfterEach
DNo lifecycle methods run again
💡 Hint
Refer to key_moments about multiple test methods and steps 2-4 in execution_table.
Concept Snapshot
Test fixtures in Kotlin use annotations:
@BeforeAll runs once before all tests
@BeforeEach runs before each test
@Test marks test methods
@AfterEach runs after each test
@AfterAll runs once after all tests
This manages setup and cleanup cleanly.
Full Transcript
This visual trace shows how Kotlin test fixtures and lifecycle annotations work. First, @BeforeAll runs once to set up shared resources. Then for each test method, @BeforeEach runs to prepare the test environment, followed by the test method itself marked with @Test. After the test, @AfterEach cleans up. After all tests finish, @AfterAll runs once to clean up shared resources. The execution table shows the order and output of these steps. Variables track the test class state changing from not started to finished. Key moments clarify why some methods run once and others multiple times. The quiz tests understanding of the lifecycle order and method roles.