blob: e56840bf600b39c55db8eae08c1a13d30209647f [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
onqtam1435c012016-09-21 15:29:11 +030017However 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```**](configuration.html#doctest_config_implement).
onqtam8126b562016-05-27 17:01:15 +030018
onqtam1435c012016-09-21 15:29:11 +030019All the [**command line**](commandline.html) options can be set like this (flags cannot because it wouldn't make sense). Filters can only be appended or cleared with ```Context::clearFilters()``` - the user cannot remove a specific filter with code.
onqtam8126b562016-05-27 17:01:15 +030020
21```
22#define DOCTEST_CONFIG_IMPLEMENT
23#include "doctest.h"
24
25int main(int argc, char** argv) {
onqtam1435c012016-09-21 15:29:11 +030026 doctest::Context context; // initialize
27
28 // defaults
29 context.addFilter("test-case-exclude", "*math*"); // exclude test cases with "math" in their name
30 context.setOption("abort-after", 5); // stop test execution after 5 failed assertions
31 context.setOption("sort", "name"); // sort the test cases by their name
32
33 context.applyCommandLine(argc, argv);
onqtam8126b562016-05-27 17:01:15 +030034
35 // overrides
onqtam1435c012016-09-21 15:29:11 +030036 context.setOption("no-breaks", true); // don't break in the debugger when assertions fail
onqtam8126b562016-05-27 17:01:15 +030037
38 int res = context.run(); // run
39
onqtame8bdda42016-06-01 15:12:36 +030040 if(context.shouldExit()) // important - query flags (and --exit) rely on the user doing this
onqtam8126b562016-05-27 17:01:15 +030041 return res; // propagate the result of the tests
42
43 int client_stuff_return_code = 0;
44 // your program - if the testing framework is integrated in your production code
45
onqtam1435c012016-09-21 15:29:11 +030046 return res + client_stuff_return_code; // the result from doctest is propagated here as well
onqtam8126b562016-05-27 17:01:15 +030047}
48
49```
50
51Note 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.
52
53### Dealing with shared objects (DLLs)
54
55When 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.
56
57Check 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).
58
59---------------
60
61[Home](readme.html#reference)
62
63
64</xmp>
65<script src="strapdown.js/strapdown.js"></script>
66</html>