Jon Test
C++ Unit Test Tool. Not for production -- sample code only
Loading...
Searching...
No Matches

Demonstration C++ Unit Test Library

See also
LICENSE
Github Repository
Note
This page duplicates the doxygen main page.

Introduction

JonTest is a rapid demonstration of certain techniques for the benefit of a particular person. Given that it functioned, it is presented as a modern example of the author's (rushed) work.

Warning
This is not intended for production work.
  • Genreal coding standards conventions are violated out of necessity and convenience to the user of this tool. For example:
    • Macro functions are used to facilitate reporting of filename and line number.
  • The author did not create this to be used and maintained.
    • Better alternatives exist, such as boost test and google test exist.
    • Use them.
  • Extensive real-world testing has not been performed.

This is a unit test tool providing the following:

  • Minimial boilerplate for individual test suites and cases
    • single definition within a .cpp file
    • automatic test suite and case registration
  • General assertions based around:
    • boolean values
    • pointer nullptr / valid (non-nullptr)
    • numeric-style comparison: == != < <= > >=
    • specific exception class (including base class) thrown
    • assertions that are known to fail
  • Expected Failures of asserts can be defined:
    • Failing each contained assert will pass the test.
    • If all contained asserts pass, the expected failure will then fail the test.

This is constrained by the following:

  • The build system only has GNU Make definitions to build under a GNU environment using g++.
  • The build system is only configured for –std=c++23

Overview

The TestRunne::get() singleton is used to run tests. Typically, this is from a main() function; however, it could be integrated into a larger testing system.

See also
main.cpp

Testing is organized a tree containing:

  • Test Suites created with TEST_SUITE(suiteName), containing:
    • (Optional) Reusable Test Case data, unique for each Test Suite that is:
      • Located in the class generated by/between TEST_SUITE(suitName) ... TEST_SUITE_END()
      • (Optional) Initialized before each test case by the automatically run setup() { ... }
      • Used in each Test Case method generated by TEST_CASE(caseName) { ... }
      • (Optional) Cleaned up after each test case by the automatically run teardown()
    • (Optional) Test Case setup(), unique for each Test Suite, run before each Test Case in the Test Suite, which may contain:
      • Assertions of validatity, such as assert()
    • Test Cases created with TEST_CASE(caseName), verified by/containing:
      • Assertions of validatity, such as assert()
    • (Optional) Test Case teardown(), unique for each Test Suite, run before after Test Case in the Test Suite, which may contain:
      • Assertions of validatity, such as assert()

Source Code Examples

assertBool_test.cpp

1#include "JonTest/TestSuite.h"
2#include "JonTest/Assert.h"
3
4TEST_SUITE(AssertBool)
5
6TEST_CASE(assert_test)
7{
8 assert(true, "true should never fail");
9 EXPECTED_FAILURE(assert(false, "False should fail"), "verifies false failed");
10}
11
12TEST_CASE(assertTrue_test)
13{
14 assertTrue(true, "true should never fail");
15 EXPECTED_FAILURE(assertTrue(false, "False should fail"), "verifies false failed");
16}
17
18TEST_CASE(assertFalse_test)
19{
20 assertFalse(false, "false should never fail");
21 EXPECTED_FAILURE(assertFalse(true, "true should fail"), "verifies true failed");
22}
23
#define assertTrue(first, message)
Fail this test if (first) is not true with the given message as explanation.
Definition Assert.h:33
#define assert(first, message)
Fail this test if (first) is not true with the given message as explanation.
Definition Assert.h:30
#define EXPECTED_FAILURE(expression, message)
Fail this test if evaluating (expression) does not result itself in a failed test.
Definition Assert.h:83
#define assertFalse(first, message)
Fail this test if (first) is not false with the given message as explanation.
Definition Assert.h:36
#define TEST_SUITE_END()
Finish definition of a Test Suite started with TEST_SUITE().
Definition TestSuite.h:77
#define TEST_CASE(testCaseName)
Defines a Test Case method named testCaseName within a Test Suite defined by TEST_SUITE().
Definition TestSuite.h:91
#define TEST_SUITE(testSuiteName)
Defines a Test Suite named testSuiteName.
Definition TestSuite.h:63

Primary header files:

See also
Examples

Installation

  1. Clone this repostiroy
  2. Update Make/Makefile.compile.incl as necessary
  3. make
  4. (Optional) include Makefile.lib.incl
  5. Include headers in include/JonTest such as #include "JonTest/Assert.h"
  6. Link in the statically compiled lib/libJonTest.a