Giter Club home page Giter Club logo

mtest's Introduction

mtest

A minimalist C unit testing framework

  • A single header-file with plain C and #define macros.
  • Plays well with CTest with a discover_tests() CMake function.

mtest is a simple, C-only unit testing framework inspired by CuTest with some modern features inspired by doctest.

For mature and feature-complete C/C++ testing frameworks, take a look at Boost.Test, Catch2, doctest, or Google Test.

Installation (CMake)

Recommended installation using CMake's FetchContent:

include(FetchContent)
FetchContent_Declare(mtest
    GIT_REPOSITORY https://github.com/MortenSchou/mtest.git
    GIT_TAG        main  # Or specify a specific branch, tag, or commit hash.
)
FetchContent_MakeAvailable(mtest)

Now the CMake library target mtest is available. For example:

add_executable(my_example_test my_test.c)
target_link_libraries(my_example_test mtest)

# Automatically discover test cases in my_example_test and add them to CTest.
discover_tests(my_example_test)

Usage

Example test file my_test.c:

#include "mtest.h"

TEST_CASE(my_first_test_case, {
  int i = 42;
  CHECK_EQ_INT(1, i);    // Test fails, but we continue running.
  REQUIRE_GT_INT(i, 0);  // This test is executed and succeeds, but the test case failed due to the previous line.
})

TEST_CASE(my_other_test_case, {
  double i = 3.14;
  // When comparing doubles, we need to specify a tolerance - here we choose 0.01
  REQUIRE_EQ_DOUBLE(4.0, i, 0.01);  // Test fails and the test case exits.
  CHECK_TRUE(1);                    // Not executed but it would succeed.
})

// This macro creates a main function that can call each test case, and it tells CTest which test cases are available. 
MAIN_RUN_TESTS(my_first_test_case, my_other_test_case)

See mtest.h for more test macros.

mtest's People

Contributors

mortenschou avatar soupglasses avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.