Plugins¶
Plugins let you run code before and after every test, parse custom command-line flags, and create additional output formatters. mu::tiny ships several built-in plugins; you can also write your own.
Plugin Base Class¶
Subclass mu::tiny::test::Plugin to intercept the test lifecycle.
// include/mu/tiny/test/Plugin.hpp
class Plugin
{
public:
Plugin(const String& name);
virtual void pre_test_action(Shell&, Result&) {}
virtual void post_test_action(Shell&, Result&) {}
virtual bool parse_arguments(int argc, const char* const* argv)
{ return false; }
virtual String get_help() const { return ""; }
virtual Output* create_output() { return nullptr; }
};
Override the methods you need:
Method |
When called |
Typical use |
|---|---|---|
Before each test body |
Reset global state, start timers |
|
After each test body |
Restore pointers, verify mock expectations |
|
During argument parsing |
Inspect |
|
When |
Show plugin’s flag description |
|
At startup |
Return a |
Installing Plugins¶
Call CommandLineRunner::install_plugin()
in main() before run_all_tests():
#include "mu/tiny/test/CommandLineRunner.hpp"
#include "MyPlugin.hpp"
using mu::tiny::test::CommandLineRunner;
int main(int argc, char** argv)
{
MyPlugin my_plugin;
CommandLineRunner::install_plugin(my_plugin);
return CommandLineRunner::run_all_tests(argc, argv);
}
Plugins form a linked list; they run in installation order.
Writing a Custom Plugin¶
Example: a plugin that logs the name of every test that runs.
// LogPlugin.hpp
#include "mu/tiny/test/Plugin.hpp"
#include "mu/tiny/test/Shell.hpp"
#include <cstdio>
class LogPlugin : public mu::tiny::test::Plugin
{
public:
LogPlugin() : mu::tiny::test::Plugin("LogPlugin") {}
void pre_test_action(mu::tiny::test::Shell& test, mu::tiny::test::Result&) override
{
std::printf("[LOG] running %s::%s\n",
test.get_group(), test.get_name());
}
};
Use it in main():
LogPlugin log;
CommandLineRunner::install_plugin(log);
Built-in Plugins¶
Plugin |
Header |
Purpose |
Docs |
|---|---|---|---|
Restore overwritten pointers after each test |
|||
Auto-verify and clear mock expectations |
|||
Write JUnit XML output via |
|||
Write TAP version 13 output to stdout via |
SetPointerPlugin is automatically
created and available through the MUTINY_PTR_SET macro — you do not need to
install it manually if you only use the macro.
Examples¶
Installing SupportPlugin with a custom
comparator alongside other plugins:
#include "IEEE754ExceptionsPlugin.hpp"
#include "TeamCityOutputPlugin.hpp"
#include "mu/tiny/mock/SupportPlugin.hpp"
#include "mu/tiny/test/CommandLineRunner.hpp"
using mu::tiny::test::CommandLineRunner;
class MyDummyComparator : public mu::tiny::mock::NamedValueComparator
{
public:
bool is_equal(const void* object1, const void* object2) override
{
return object1 == object2;
}
mu::tiny::String value_to_string(const void* object) override
{
return mu::tiny::string_from(object);
}
};
int main(int argc, char** argv)
{
mu::tiny::mock::SupportPlugin mock_plugin;
MyDummyComparator dummy_comparator;
mock_plugin.install_comparator("MyDummyType", dummy_comparator);
CommandLineRunner::install_plugin(mock_plugin);
IEEE754ExceptionsPlugin ieee754_plugin;
CommandLineRunner::install_plugin(ieee754_plugin);
TeamCityOutputPlugin tc_plugin;
CommandLineRunner::install_plugin(tc_plugin);
return mu::tiny::test::CommandLineRunner::run_all_tests(argc, argv);
}