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

pre_test_action()

Before each test body

Reset global state, start timers

post_test_action()

After each test body

Restore pointers, verify mock expectations

parse_arguments()

During argument parsing

Inspect argv[0]; activate plugin via a -p<plugin> argument

get_help()

When -h is printed

Show plugin’s flag description

create_output()

At startup

Return a Output for an additional output format

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

SetPointerPlugin

mu/tiny/test/SetPointerPlugin.hpp

Restore overwritten pointers after each test

SetPointerPlugin

SupportPlugin

mu/tiny/mock/SupportPlugin.hpp

Auto-verify and clear mock expectations

Mocking

JUnitOutputPlugin

mu/tiny/test/JUnitOutputPlugin.hpp

Write JUnit XML output via -pjunit

JUnit XML Output

TapOutputPlugin

mu/tiny/test/TapOutputPlugin.hpp

Write TAP version 13 output to stdout via -ptap

TAP Output

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);
}