Lifecycle hooks

Before/after each/all

Some times you may want to execute some code before or after a category of tests run. This can be easily achieved with CST's hooks. CST offers four different hooks, all provided by the cst.h file.

But before knowing each hook, it's important to know how they work with categories.

  • All hooks have this syntax: HOOK_NAME("Test category") { <code> }

  • A NULL category implies the CST lifecycle itself.

  • An empty category implies tests without a category

#include "cst.h"

// Category handling demonstration. Applies to every hook type

CST_BEFORE_ALL(NULL) {
    // Code runs once before executing any test
}

CST_BEFORE_ALL("") {
    // Code runs once before running tests that don't have a category
}
 
// Hook types demonstration

CST_BEFORE_ALL("Example") {
    // Code runs before tests of the "Example" category
}

CST_BEFORE_EACH("Example") {
    // Code runs before EACH test of the "Example" category
}

CST_AFTER_ALL("Example") {
    // Code runs after all tests of the "Example" category
}

CST_AFTER_EACH("Example") {
    // Code runs after EACH test of the "Example" category
}

Last updated

Was this helpful?