Getting Started with mu::tiny¶
Building mu::tiny¶
mu::tiny uses CMake. Clone the repository and configure:
cmake -B build
cmake --build build
cmake --install build # optional system install
Adding mu::tiny to Your Project¶
The recommended approach uses CMake FetchContent
with FIND_PACKAGE_ARGS.
This first attempts find_package; if mu::tiny
is not installed, CMake fetches and builds it from source automatically:
cmake_minimum_required(VERSION 3.24)
project(my_tests)
include(FetchContent)
FetchContent_Declare(
mu.tiny
GIT_REPOSITORY https://github.com/thetic/mu.tiny.git
GIT_TAG v0.8.0
FIND_PACKAGE_ARGS 0.8
)
FetchContent_MakeAvailable(mu.tiny)
add_executable(my_tests main.cpp widget.test.cpp)
target_link_libraries(my_tests PRIVATE mu::tiny)
include(CTest)
include(mu.tiny)
mutiny_discover_tests(my_tests)
If mu::tiny is already installed and you prefer not to use
FetchContent, use
find_package directly:
find_package(mu.tiny 0.8 REQUIRED)
Headers¶
All public headers live under include/mu/tiny/. The main headers you’ll use:
Header |
Purpose |
|---|---|
|
Test and assertion macros ( |
|
|
|
C interface (include in |
|
|
|
|
Writing main()¶
Every test executable needs a main(). The simplest form uses
CommandLineRunner:
#include "mu/tiny/test/CommandLineRunner.hpp"
using mu::tiny::test::CommandLineRunner;
int main(int argc, char** argv)
{
return CommandLineRunner::run_all_tests(argc, argv);
}
To add plugins (e.g.
JUnitOutputPlugin,
SetPointerPlugin,
SupportPlugin), call
CommandLineRunner::install_plugin()
before run_all_tests():
#include "mu/tiny/test/CommandLineRunner.hpp"
#include "mu/tiny/test/JUnitOutputPlugin.hpp"
#include "mu/tiny/mock/SupportPlugin.hpp"
using mu::tiny::test::CommandLineRunner;
int main(int argc, char** argv)
{
mu::tiny::test::JUnitOutputPlugin junit;
mu::tiny::mock::SupportPlugin mock_plugin;
CommandLineRunner::install_plugin(junit);
CommandLineRunner::install_plugin(mock_plugin);
return CommandLineRunner::run_all_tests(argc, argv);
}
Your First Test¶
#include "mu/tiny/test.hpp"
namespace {
void (*real_one)();
void stub() {}
} // namespace
/* Declare TestGroup with name CheatSheet */
TEST_GROUP(CheatSheet)
{
/* declare a setup method for the test group. Optional. */
void setup() override
{
/* Set method real_one to stub. Automatically restore in teardown */
MUTINY_PTR_SET(real_one, stub);
}
/* Declare a teardown method for the test group. Optional */
void teardown() override {}
}; /* Do not forget semicolumn */
/* Declare one test within the test group */
TEST(CheatSheet, TestName)
{
/* Check two longs are equal */
CHECK_EQUAL(1L, 1L);
/* Check a condition */
CHECK(true == true);
/* Check a string */
STRCMP_EQUAL("HelloWorld", "HelloWorld");
}
TEST_GROUPdeclares a group; the struct implicitly inherits frommu::tiny::test::Test.setup()runs before each test body;teardown()runs after.TEST(group, name)defines a single test. The body is the{ ... }block that follows.A failing assertion immediately exits the test body (via
longjmpor exception).
Running Tests¶
Run the binary directly:
./build/tests/my_tests # run all
./build/tests/my_tests -v # verbose: print each test name
./build/tests/my_tests -g CheatSheet # only group "CheatSheet"
./build/tests/my_tests -n TestName # only tests whose name contains this
Via CTest (after mutiny_discover_tests() in CMakeLists):
ctest --preset GNU
ctest --preset GNU -R CheatSheet
See Command-Line Reference for all flags.
Examples¶
File |
Demonstrates |
|---|---|
Minimal |
|
Full group with setup/teardown, helper methods, and multiple assertion styles |