Skip to content

DrCpp/DrMock

Repository files navigation

DrMock

Windows Linux macOS

Introduction to DrMock

DrMock is a C++17 testing and mocking framework for Windows, Linux and macOS.

Features

  • Unit test framework
  • On-the-fly mock object source code generation at compile time
  • State machine-like mock objects
  • Qt5 integration

Example

DRTEST_TEST(launch)
{
  auto rocket = std::make_shared<drmock::samples::RocketMock>();

  // Define rocket's state behavior.
  rocket->mock.toggleLeftThruster().state()
                  // Source state    // Destination state   // Input value
      .transition("",                "leftThrusterOn",      true )
      .transition("leftThrusterOn",  "",                    false)
      .transition("rightThrusterOn", "allThrustersOn",      true )
      .transition("allThrustersOn",  "rightThrusterOn",     false);
  rocket->mock.toggleRightThruster().state()
      .transition("",                "rightThrusterOn",     true )
      .transition("rightThrusterOn", "",                    false)
      .transition("leftThrusterOn",  "allThrustersOn",      true )
      .transition("allThrustersOn",  "leftThrusterOn",      false);
  rocket->mock.launch().state()
      .transition("",                "failure")
      .transition("*",               "liftOff");  // "*" = catch-all

  // Check that the pad launches the rocket with at least one thruster enabled.
  drmock::samples::LaunchPad launch_pad{rocket};
  launch_pad.launch();
  DRTEST_ASSERT(rocket->mock.verifyState("liftOff"));
}

Note. Only the state of rocket after the test matters. It's fine to keep toggeling the thrusters as long as at least one is enabled at launch. DrMock allows tests to be designed without a particular implementation in mind.

Building DrMock

See BUILD.md for instructions.

Documentation

Contributing to DrMock

See CONTRIBUTING.md.

Acknowledgments

During the configuration of DrMock's build system, we have profited greatly from the following sources:

Projects that use DrMock