How shUnit2 test lifecycles work
mastershUnit2 follows an xUnit-style lifecycle. It automatically identifies and executes any function in your script that is prefixed with test.
To manage the environment, you can define several optional lifecycle hooks:
oneTimeSetUp(): Runs once before any tests are executed. Use this for global setup like creating directories or setting environment variables.oneTimeTearDown(): Runs once after all tests have completed. Use this for global cleanup.setUp(): Runs before each individual test function. Use this to ensure each test starts with a clean state.tearDown(): Runs after each individual test function. Use this to clean up files or state created during a specific test.
The execution flow for each test is: setUp() $\rightarrow$ test<Name>() $\rightarrow$ tearDown().
sequenceDiagram
participant unit_test as unit test
unit_test-->>shUnit2: shUnit2 loaded from unit test
note over unit_test,shUnit2: shUnit2 identifies test*() functions
shUnit2->>unit_test: oneTimeSetUp()
loop for each test function
shUnit2->>unit_test: setUp()
shUnit2->>unit_test: testSomeFunction()
unit_test-->>script: code called from testSomeFunction()
shUnit2->>unit_test: tearDown()
end
shUnit2->>unit_test: oneTimeTearDown()