onqtam | 8126b56 | 2016-05-27 17:01:15 +0300 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <title>main</title> |
| 4 | <xmp theme="united" style="display:none;"> |
| 5 | |
| 6 | ## The ```main()``` entry point |
| 7 | |
| 8 | The 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 | |
| 15 | This 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 | |
onqtam | 1435c01 | 2016-09-21 15:29:11 +0300 | [diff] [blame] | 17 | However 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). |
onqtam | 8126b56 | 2016-05-27 17:01:15 +0300 | [diff] [blame] | 18 | |
onqtam | 1435c01 | 2016-09-21 15:29:11 +0300 | [diff] [blame] | 19 | All 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. |
onqtam | 8126b56 | 2016-05-27 17:01:15 +0300 | [diff] [blame] | 20 | |
| 21 | ``` |
| 22 | #define DOCTEST_CONFIG_IMPLEMENT |
| 23 | #include "doctest.h" |
| 24 | |
| 25 | int main(int argc, char** argv) { |
onqtam | 1435c01 | 2016-09-21 15:29:11 +0300 | [diff] [blame] | 26 | 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); |
onqtam | 8126b56 | 2016-05-27 17:01:15 +0300 | [diff] [blame] | 34 | |
| 35 | // overrides |
onqtam | 1435c01 | 2016-09-21 15:29:11 +0300 | [diff] [blame] | 36 | context.setOption("no-breaks", true); // don't break in the debugger when assertions fail |
onqtam | 8126b56 | 2016-05-27 17:01:15 +0300 | [diff] [blame] | 37 | |
| 38 | int res = context.run(); // run |
| 39 | |
onqtam | e8bdda4 | 2016-06-01 15:12:36 +0300 | [diff] [blame] | 40 | if(context.shouldExit()) // important - query flags (and --exit) rely on the user doing this |
onqtam | 8126b56 | 2016-05-27 17:01:15 +0300 | [diff] [blame] | 41 | 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 | |
onqtam | 1435c01 | 2016-09-21 15:29:11 +0300 | [diff] [blame] | 46 | return res + client_stuff_return_code; // the result from doctest is propagated here as well |
onqtam | 8126b56 | 2016-05-27 17:01:15 +0300 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | ``` |
| 50 | |
| 51 | Note 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 | |
| 55 | When 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 | |
| 57 | Check 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> |