Skip to content
16 changes: 14 additions & 2 deletions docs/junit-output.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ Install the plugin via :cpp:class:`Registry <mu::tiny::test::Registry>` before c

int main(int argc, char** argv)
{
mu::tiny::test::JUnitOutputPlugin junit;
// Pass the executable basename so that bare -pjunit writes
// <basename>.xml rather than mutiny.xml.
mu::tiny::test::JUnitOutputPlugin junit("my_suite");
mu::tiny::test::Registry::get_current_registry()->install_plugin(&junit);
return mu::tiny::test::CommandLineRunner::run_all_tests(argc, argv);
}

The string passed to the constructor is used as the package name when
``-pjunit`` is given without an explicit ``=<name>`` suffix. It can be
overridden at runtime with ``-pjunit=<name>``. Passing an empty string
(the default) makes bare ``-pjunit`` write to ``mutiny.xml`` with no
package prefix in classnames.

With the plugin installed, ``-pjunit`` on the command line enables the
XML output. Without the flag, no XML is written and the console output
is unchanged.
Expand All @@ -60,7 +68,11 @@ When running the executable directly, a single file is produced for the
entire run. The file is named ``<name>.xml``, where ``<name>`` comes from:

- the ``-pjunit=<name>`` argument if given, or
- the executable basename (e.g. ``my_tests``) if only ``-pjunit`` is used.
- the default package name supplied to the :cpp:class:`JUnitOutputPlugin
<mu::tiny::test::JUnitOutputPlugin>` constructor (the executable basename
when going through :cpp:func:`CommandLineRunner::run_all_tests
<mu::tiny::test::CommandLineRunner::run_all_tests>`), or
- ``mutiny`` if no default was provided.

When using :cmake:variable:`MUTINY_JUNIT_REPORT` with :cmake:command:`mutiny_discover_tests`,
CTest runs each test group as a separate process and gives each one a unique
Expand Down
4 changes: 2 additions & 2 deletions docs/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Subclass :cpp:class:`mu::tiny::test::Plugin` to intercept the test lifecycle.
virtual void pre_test_action(Shell&, Result&) {}
virtual void post_test_action(Shell&, Result&) {}

virtual bool parse_arguments(int argc, const char* const* argv, int index)
virtual bool parse_arguments(int argc, const char* const* argv)
{ return false; }

virtual String get_help() const { return ""; }
Expand All @@ -44,7 +44,7 @@ Override the methods you need:
- Restore pointers, verify mock expectations
* - :cpp:func:`parse_arguments() <mu::tiny::test::Plugin::parse_arguments>`
- During argument parsing
- Activate plugin via a :option:`-p\<plugin\>` argument
- Inspect ``argv[0]``; activate plugin via a :option:`-p\<plugin\>` argument
* - :cpp:func:`get_help() <mu::tiny::test::Plugin::get_help>`
- When :option:`-h` is printed
- Show plugin's flag description
Expand Down
5 changes: 2 additions & 3 deletions examples/tests/TeamCityOutputPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ TeamCityOutputPlugin::TeamCityOutputPlugin()

bool TeamCityOutputPlugin::parse_arguments(
int /*argc*/,
const char* const* argv,
int index
const char* const* argv
)
{
mu::tiny::String arg = argv[index];
mu::tiny::String arg = argv[0];
if (arg.size() > 2 && mu::tiny::String(arg.c_str() + 2) == "teamcity") {
active_ = true;
return true;
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/TeamCityOutputPlugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TeamCityOutputPlugin : public mu::tiny::test::Plugin
public:
TeamCityOutputPlugin();

bool parse_arguments(int argc, const char* const* argv, int index) override;
bool parse_arguments(int argc, const char* const* argv) override;
mu::tiny::String get_help() const override;
mu::tiny::test::Output* create_output() override;

Expand Down
38 changes: 14 additions & 24 deletions include/mu/tiny/test/CommandLineArguments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,41 +116,31 @@ class MUTINY_EXPORT CommandLineArguments
unsigned int shuffle_seed_{ 0 };
Filter* group_filters_{ nullptr };
Filter* name_filters_{ nullptr };
void set_repeat_count(int argc, const char* const* argv, int& index);
bool set_shuffle(int argc, const char* const* argv, int& index);
void add_group_filter(int argc, const char* const* argv, int& index);
bool add_group_dot_name_filter(
int set_repeat_count(int argc, const char* const* argv);
int set_shuffle(int argc, const char* const* argv);
int add_group_filter(int argc, const char* const* argv);
int add_group_dot_name_filter(
int argc,
const char* const* argv,
int& index,
const String& parameter_name,
bool strict,
bool exclude
);
void add_strict_group_filter(int argc, const char* const* argv, int& index);
void add_exclude_group_filter(int argc, const char* const* argv, int& index);
void add_exclude_strict_group_filter(
int add_strict_group_filter(int argc, const char* const* argv);
int add_exclude_group_filter(int argc, const char* const* argv);
int add_exclude_strict_group_filter(int argc, const char* const* argv);
int add_name_filter(int argc, const char* const* argv);
int add_strict_name_filter(int argc, const char* const* argv);
int add_exclude_name_filter(int argc, const char* const* argv);
int add_exclude_strict_name_filter(int argc, const char* const* argv);
int add_test_to_run_based_on_verbose_output(
int argc,
const char* const* argv,
int& index
);
void add_name_filter(int argc, const char* const* argv, int& index);
void add_strict_name_filter(int argc, const char* const* argv, int& index);
void add_exclude_name_filter(int argc, const char* const* argv, int& index);
void add_exclude_strict_name_filter(
int argc,
const char* const* argv,
int& index
);
void add_test_to_run_based_on_verbose_output(
int argc,
const char* const* argv,
int& index,
const char* parameter_name
);
bool parse_simple_flag(const String& argument);
bool parse_prefix_arg(const String& argument, Plugin* plugin, int& index);
bool parse_argument(const String& argument, Plugin* plugin, int& index);
int parse_prefix_arg(int argc, const char* const* argv, Plugin* plugin);
int parse_argument(int argc, const char* const* argv, Plugin* plugin);
};

} // namespace test
Expand Down
1 change: 1 addition & 0 deletions include/mu/tiny/test/CommandLineRunner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class MUTINY_EXPORT CommandLineRunner
private:
CommandLineArguments* arguments_{ nullptr };
Registry* registry_;
String program_name_;

bool parse_arguments(Plugin* plugin);
int run_all_tests();
Expand Down
19 changes: 13 additions & 6 deletions include/mu/tiny/test/JUnitOutputPlugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,24 @@ class Output;
class MUTINY_EXPORT JUnitOutputPlugin : public Plugin
{
public:
/** @brief Construct and name the plugin. */
JUnitOutputPlugin();
/**
* @brief Construct the plugin with an optional default package name.
*
* @param default_package_name Used as the JUnit package name when
* @c -pjunit is given without an explicit @c =name suffix.
* Pass the executable basename to reproduce the standard behaviour.
* Defaults to empty, which leaves the package name unset.
*/
explicit JUnitOutputPlugin(const String& default_package_name = "");

/**
* @brief Handle the `-pjunit[=<name>]` command-line argument.
*
* @param argc Argument count.
* @param argv Argument vector.
* @param index Current argument index.
* @param argc Remaining argument count (@p argv[0] through end).
* @param argv Pointer to the current argument.
* @return true if the argument was consumed.
*/
bool parse_arguments(int argc, const char* const* argv, int index) override;
bool parse_arguments(int argc, const char* const* argv) override;

/**
* @brief Return help text for the `-pjunit` option.
Expand All @@ -56,6 +62,7 @@ class MUTINY_EXPORT JUnitOutputPlugin : public Plugin

private:
bool active_{ false };
String default_package_name_;
String package_name_;
};

Expand Down
32 changes: 13 additions & 19 deletions include/mu/tiny/test/Plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ class MUTINY_EXPORT Plugin
/**
* @brief Handle a custom command-line argument.
*
* Called for each argument position @p index. Return true to indicate the
* argument was consumed (the runner will skip to the next one).
* Called with a pre-sliced view where @p argv[0] is the argument to inspect
* and @p argc is the number of remaining arguments (including @p argv[0]).
* Return true to indicate the argument was consumed.
*
* @param argc Total argument count.
* @param argv Argument vector.
* @param index Current argument index to inspect.
* @return true if the argument at @p index was consumed.
* @param argc Remaining argument count (@p argv[0] through end).
* @param argv Pointer to the current argument.
* @return true if @p argv[0] was consumed.
*/
virtual bool parse_arguments(int argc, const char* const* argv, int index);
virtual bool parse_arguments(int argc, const char* const* argv);

/**
* @brief Return a help string for this plugin's command-line arguments.
Expand Down Expand Up @@ -110,24 +110,18 @@ class MUTINY_EXPORT Plugin

/**
* @brief Invoke parse_arguments() on each plugin in the chain.
* @param argc Argument count.
* @param argv Argument vector (const).
* @param index Current argument index.
* @param argc Remaining argument count (@p argv[0] through end).
* @param argv Pointer to the current argument.
* @return true if any plugin consumed the argument.
*/
virtual bool parse_all_arguments(
int argc,
const char* const* argv,
int index
);
virtual bool parse_all_arguments(int argc, const char* const* argv);
/**
* @brief Overload accepting a non-const argv.
* @param argc Argument count.
* @param argv Argument vector.
* @param index Current argument index.
* @param argc Remaining argument count (@p argv[0] through end).
* @param argv Pointer to the current argument.
* @return true if any plugin consumed the argument.
*/
virtual bool parse_all_arguments(int argc, char** argv, int index);
virtual bool parse_all_arguments(int argc, char** argv);

/** @return Concatenated help strings from all plugins in the chain. */
virtual String get_all_help() const;
Expand Down
7 changes: 3 additions & 4 deletions include/mu/tiny/test/TapOutputPlugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ class MUTINY_EXPORT TapOutputPlugin : public Plugin
/**
* @brief Handle the @c -ptap command-line argument.
*
* @param argc Argument count.
* @param argv Argument vector.
* @param index Current argument index.
* @param argc Remaining argument count (@p argv[0] through end).
* @param argv Pointer to the current argument.
* @return true if the argument was consumed.
*/
bool parse_arguments(int argc, const char* const* argv, int index) override;
bool parse_arguments(int argc, const char* const* argv) override;

/**
* @brief Return help text for the @c -ptap option.
Expand Down
Loading