| 1 | [](https://app.travis-ci.com/github/cgreen-devs/cgreen) |
| 2 | [](https://coveralls.io/github/cgreen-devs/cgreen?branch=master) |
| 3 | |
| 4 |  |
| 5 | |
| 6 | |
| 7 | Cgreen - The Modern Unit Test and Mocking Framework for C and C++ |
| 8 | ================================================================= |
| 9 | |
| 10 | Do you TDD? In C or C++? Maybe you want to have your tests read out in |
| 11 | a fluent fashion? Like this |
| 12 | |
| 13 | Ensure(Converter, converts_XIV_to_14) { |
| 14 | assert_that(convert_roman_to_decimal("XIV"), is_equal_to(14)); |
| 15 | } |
| 16 | |
| 17 | And you want output like this |
| 18 | |
| 19 | roman_test.c:12: Failure: Converter -> converts_XIV_to_14 |
| 20 | Expected [convert_roman_to_decimal("XIV")] to [equal] [14] |
| 21 | actual value: [0] |
| 22 | expected value: [14] |
| 23 | |
| 24 | Then *Cgreen* is the thing for you! |
| 25 | |
| 26 | **TLDR;** The full tutorial is on |
| 27 | [github.io](https://cgreen-devs.github.io/cgreen/cgreen-guide-en.html). |
| 28 | Or have a look at the [cheat sheet](https://cgreen-devs.github.io/cgreen/cheat-sheet.html). |
| 29 | |
| 30 | ## What It Is |
| 31 | |
| 32 | Cgreen is a modern unit test and mocking framework for C and C++. |
| 33 | Here are some of Cgreens unique selling points: |
| 34 | |
| 35 | - fast build, clean code, highly portable |
| 36 | - auto-discovery of tests without the abuse of static initializers or globals |
| 37 | - extensible without recompiling |
| 38 | - fluent, expressive and readable API with the same modern syntax across C and C++ |
| 39 | - process isolation for each test preventing intermittent failures |
| 40 | and cross-test dependencies |
| 41 | - built-in mocking for C, compatible with mockitopp and other C++ mocking libraries |
| 42 | - expressive and clear output using the default reporter |
| 43 | - fully functional mocks, both strict, loose and learning |
| 44 | - mocks with side effects |
| 45 | - extensive and expressive constraints for many datatypes |
| 46 | - custom constraints can be constructed by user |
| 47 | - bdd-flavoured test declarations with Before and After declarations |
| 48 | - extensible reporting mechanism |
| 49 | - fully composable test suites |
| 50 | - a single test can be run in a single process for easier debugging |
| 51 | |
| 52 | ## Getting It |
| 53 | |
| 54 | Cgreen is hosted on [GitHub](https://github.com/cgreen-devs/cgreen). |
| 55 | As of now there are no pre-built packages to download, but Cgreen is available in [Debian, Fedora and some other package repositories](https://repology.org/project/cgreen/versions), although some are lagging. |
| 56 | |
| 57 | There are also some other packaging scripts available, not all official: |
| 58 | |
| 59 | - [PACMAN script](https://github.com/voins/cgreen-pkg) |
| 60 | - [MacOS packagesbuild](https://github.com/cgreen-devs/cgreen-macosx-packaging) |
| 61 | - [Cygwin package script](https://github.com/cgreen-devs/cgreen-cygport) |
| 62 | |
| 63 | You can also clone the repository or download the source zip from [GitHub](http://www.github.com/cgreen-devs/cgreen) and build it yourself. |
| 64 | |
| 65 | ## Building It |
| 66 | |
| 67 | You need the [CMake](http://www.cmake.org) build system. |
| 68 | Most standard C/C++ compilers should work. GCC definitely does. |
| 69 | |
| 70 | Perl, diff, find and sed are required to run Cgreen's own |
| 71 | unit-tests. Most distro will have those already installed. |
| 72 | |
| 73 | In the root directory run ``make``. That will configure and build the |
| 74 | library and the `cgreen-runner`, both supporting both C and C++. See |
| 75 | also the documentation. |
| 76 | |
| 77 | ## Using It |
| 78 | |
| 79 | Tests are fairly easy write, as shown by the examples in the beginning |
| 80 | of this readme. You should probably read the |
| 81 | [tutorial](https://cgreen-devs.github.io/cgreen/cgreen-guide-en.html) once before writing your |
| 82 | first test, though. |
| 83 | |
| 84 | Basically you can run your tests in two ways |
| 85 | |
| 86 | 1. Compile and link all your tests with a test driver (as shown in the |
| 87 | fist chapters of the tutorial) |
| 88 | 2. Link your tests into separate shared libraries (`.so`, `.dylib` or |
| 89 | similar) and run them with the `cgreen-runner` (described in chapter |
| 90 | 6 of the tutorial) |
| 91 | |
| 92 | Option 2 is very handy, you can run multiple libraries in the same |
| 93 | run, but also specify single tests that you want to run. And with the |
| 94 | completion script available for bash you can get TAB-completion not |
| 95 | only for files and options but also for tests inside the libraries. |
| 96 | |
| 97 | `cgreen-debug` is a small script that you invoke in the same way as |
| 98 | the runner but runs a single, specified, test and puts you in the |
| 99 | debugger at the start of that test. Awesome! |
| 100 | |
| 101 | ## Using Cgreen in other CMake projects |
| 102 | Once Cgreen is installed you can use ``find_package(cgreen)`` in your CMake |
| 103 | projects to get access to useful variables like ``${CGREEN_LIBRARIES}``, |
| 104 | ``${CGREEN_EXECUTABLE}`` and ``${CGREEN_INCLUDE_DIRS}``. Version can be |
| 105 | specified in ``find_package`` as well. For example, in order to enforce a minimum |
| 106 | version of Cgreen in your project use ``find_package(cgreen 1.1.0)`` |
| 107 | |
| 108 | ## Reading Up! |
| 109 | |
| 110 | You can read the extensive tutorial directly on |
| 111 | [GitHub](https://cgreen-devs.github.io/cgreen/cgreen-guide-en.html). |
| 112 | |
| 113 | There is a [cheat sheet](https://github.com/cgreen-devs/cgreen/blob/master/doc/cheat-sheet.md) |
| 114 | available. |
| 115 | |
| 116 | You can also build the documentation yourself in HTML and PDF format. |
| 117 | Generate it using Asciidoctor, which can be done using the CMake |
| 118 | configuration. Of course you need |
| 119 | [Asciidoctor](http://www.asciidoctor.org). |
| 120 | |
| 121 | make doc |
| 122 | make pdf |
| 123 | |
| 124 | (Generating PDF also requires [asciidoctor-pdf](https://asciidoctor.org/docs/asciidoctor-pdf/).) |
| 125 | |
| 126 | ## License |
| 127 | |
| 128 | Cgreen is licensed under the ISC License |
| 129 | (http://spdx.org/licenses/ISC), sometimes known as the OpenBSD |
| 130 | license. If there is no licence agreement with this package please |
| 131 | download a version from the location above. You must read and accept |
| 132 | that licence to use this software. The file is titled simply LICENSE. |
| 133 | |
| 134 | ## The Original Version |
| 135 | |
| 136 | What is it? It's a framework for unit testing, written in C. A tool |
| 137 | for C developers writing tests of their own code. |
| 138 | |
| 139 | If you have used JUnit, or any of the xUnit clones, you will find |
| 140 | the concept familiar. In particular the tool supports a range of |
| 141 | assertions, composable test suites and setup/teardown facilities. |
| 142 | Because of the peculiarities of C programming, each test function |
| 143 | is normally run in it's own process. |
| 144 | |
| 145 | This project is very close in scope to the "Check" unit tester and |
| 146 | was initially influenced by it. |
| 147 | |
| 148 | The main difference from this tool and other xUnit tools, such as |
| 149 | "Check", is that test results are not stored. Instead they are |
| 150 | streamed to the reporter psuedo-class, one that is easily |
| 151 | overridden by the end user. |
| 152 | |
| 153 | The other main extra feature is the support for writing mock |
| 154 | callbacks. This includes generating sequences for return values |
| 155 | or parameter expectations. |
| 156 | |
| 157 | Feedback, queries and request should be put to the cgreen developers |
| 158 | through https://github.com/cgreen-devs/cgreen. |
| 159 | |
| 160 | This tool is basically a spin off from a research project at |
| 161 | Wordtracker and would not have happened without the generous |
| 162 | financial support of the Wordtracker keyword tool... |
| 163 | http://www.wordtracker.com/ |
| 164 | |
| 165 | Substantial inital work by Marcus Baker <[email protected]>. Recent |
| 166 | additions by Matt Hargett <[email protected]>, Thomas Nilefalk |
| 167 | <[email protected]>, João Freitas <[email protected]> and others. |