blob: 852c5d7e7b4c57e41adb1d03b7bbcfd21f56035b [file] [log] [blame]
onqtam8126b562016-05-27 17:01:15 +03001<!DOCTYPE html>
2<html>
3<title>main</title>
4<xmp theme="united" style="display:none;">
5
6## The ```main()``` entry point
7
8The usual way of writing tests in C++ has always been into separate source files from the code they test that form an executable containing only tests. In that scenario the default ```main()``` provided by **doctest** is usually sufficient:
9
10```
11#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
12#include "doctest.h"
13```
14
15This should be done in exactly one source file and is even a good idea to do this in a separate file with nothing else in it.
16
17However if you need more control - want to set options with code to the execution context or want to integrate the framework in your production code - then the default ```main()``` just won't do the job. In that case use **```DOCTEST_CONFIG_IMPLEMENT```**.
18
19All the [**command line**](commandline.html) options can be set like this (flags cannot because it wouldn't make sense). Filters can only be appended - the user cannot remove a filter given from the command line with code.
20
21```
22#define DOCTEST_CONFIG_IMPLEMENT
23#include "doctest.h"
24
25int main(int argc, char** argv) {
26 doctest::Context context(argc, argv); // initialize
27
28 // overrides
29 context.addFilter("test-case-exclude", "*math*"); // exclude test cases with "math" in their name
30 context.setOption("no-breaks", true); // don't break in the debugger when assertions fail
31 context.setOption("abort-after", 5); // stop test execution after 5 failed assertions
32 context.setOption("sort", "name"); // sort the test cases by their name
33
34 int res = context.run(); // run
35
onqtame8bdda42016-06-01 15:12:36 +030036 if(context.shouldExit()) // important - query flags (and --exit) rely on the user doing this
onqtam8126b562016-05-27 17:01:15 +030037 return res; // propagate the result of the tests
38
39 int client_stuff_return_code = 0;
40 // your program - if the testing framework is integrated in your production code
41
42 return res + client_stuff_return_code;
43}
44
45```
46
47Note the call to ```.shouldExit()``` on the context - that is very important - it will be set when a query flag has been used (or the ```--no-run``` option is set to ```true```) and it is the user's responsibility to exit the application in a normal way.
48
49### Dealing with shared objects (DLLs)
50
51When integrating the framework in production code which gets built as a shared object (dll) everything still works. Many shared objects and an executable can have tests in them and can even use different versions of the **doctest** framework.
52
53Check out [**this**](../../examples/dll_and_executable/) example which showcases how to call the tests in a shared object from the executable (and it also showcases that if a file with a test case is included both in the shared object and the executable then the test is registered in both places).
54
55---------------
56
57[Home](readme.html#reference)
58
59
60</xmp>
61<script src="strapdown.js/strapdown.js"></script>
62</html>