2015-03-26 23:04:02 +01:00
|
|
|
Changing the internals
|
|
|
|
======================
|
|
|
|
|
|
|
|
Providing your own main
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
If you are not satisfied with the default CLI or environment variables, you
|
|
|
|
can define your own main function.
|
|
|
|
|
|
|
|
Configuring the test runner
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-09-07 15:55:36 +02:00
|
|
|
First and foremost, you need to generate the test set; this is done by calling
|
|
|
|
``criterion_initialize()``. The function returns a ``struct criterion_test_set *``,
|
|
|
|
that you need to pass to ``criterion_run_all_tests`` later on.
|
|
|
|
|
|
|
|
At the very end of your main, you also need to call ``criterion_finalize`` with
|
|
|
|
the test set as parameter to free any ressources initialized by criterion earlier.
|
|
|
|
|
2015-03-26 23:04:02 +01:00
|
|
|
You'd usually want to configure the test runner before calling it.
|
|
|
|
Configuration is done by setting fields in a global variable named
|
|
|
|
``criterion_options`` (include criterion/options.h).
|
|
|
|
|
|
|
|
Here is an exhaustive list of these fields:
|
|
|
|
|
|
|
|
=================== ================================== ==============================================================
|
|
|
|
Field Type Description
|
|
|
|
=================== ================================== ==============================================================
|
|
|
|
logging_threshold enum criterion_logging_level The logging level
|
|
|
|
------------------- ---------------------------------- --------------------------------------------------------------
|
2015-11-04 20:18:47 +01:00
|
|
|
logger struct criterion_logger * The logger (see below)
|
2015-03-26 23:04:02 +01:00
|
|
|
------------------- ---------------------------------- --------------------------------------------------------------
|
|
|
|
always_succeed bool True iff criterion_run_all_tests should always returns 1
|
|
|
|
------------------- ---------------------------------- --------------------------------------------------------------
|
|
|
|
use_ascii bool True iff the outputs should use the ASCII charset
|
|
|
|
------------------- ---------------------------------- --------------------------------------------------------------
|
|
|
|
fail_fast bool True iff the test runner should abort after the first failure
|
|
|
|
------------------- ---------------------------------- --------------------------------------------------------------
|
|
|
|
pattern const char * The pattern of the tests that should be executed
|
|
|
|
=================== ================================== ==============================================================
|
|
|
|
|
2015-09-07 15:27:15 +02:00
|
|
|
if you want criterion to provide its own default CLI parameters and environment
|
2015-09-07 15:55:36 +02:00
|
|
|
variables handling, you can also call ``criterion_handle_args(int argc, char *argv[], bool handle_unknown_arg)``
|
2015-09-07 15:27:15 +02:00
|
|
|
with the proper ``argc/argv``. ``handle_unknown_arg``, if set to true, is here
|
|
|
|
to tell criterion to print its usage when an unknown CLI parameter is encountered.
|
|
|
|
If you want to add your own parameters, you should set it to false.
|
|
|
|
|
|
|
|
The function returns 0 if the main should exit immediately, and 1 if it should
|
|
|
|
continue.
|
|
|
|
|
2015-03-26 23:04:02 +01:00
|
|
|
Starting the test runner
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The test runner can be called with ``criterion_run_all_tests``. The function
|
|
|
|
returns 0 if one test or more failed, 1 otherwise.
|
2015-04-14 14:53:18 +02:00
|
|
|
|
2015-09-07 15:27:15 +02:00
|
|
|
Example main
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
#include <criterion/criterion.h>
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2015-09-07 15:55:36 +02:00
|
|
|
struct criterion_test_set *tests = criterion_initialize();
|
|
|
|
|
2015-11-30 10:29:40 +01:00
|
|
|
int result = 0;
|
|
|
|
if (criterion_handle_args(argc, argv, true))
|
2016-08-31 16:36:17 +02:00
|
|
|
result = !criterion_run_all_tests(tests);
|
2015-09-07 15:55:36 +02:00
|
|
|
|
2016-08-31 16:36:17 +02:00
|
|
|
criterion_finalize(tests);
|
2015-09-07 15:55:36 +02:00
|
|
|
return result;
|
2015-09-07 15:27:15 +02:00
|
|
|
}
|
|
|
|
|
2015-11-04 20:18:47 +01:00
|
|
|
Implementing your own logger
|
|
|
|
----------------------------
|
2015-04-14 14:53:18 +02:00
|
|
|
|
2015-11-04 20:18:47 +01:00
|
|
|
In case you are not satisfied by the default logger, you can implement
|
|
|
|
yours. To do so, simply set the ``logger`` option to your custom
|
|
|
|
logger.
|
2015-04-14 14:53:18 +02:00
|
|
|
|
|
|
|
Each function contained in the structure is called during one of the standard
|
|
|
|
phase of the criterion runner.
|
|
|
|
|
2015-11-04 20:18:47 +01:00
|
|
|
For more insight on how to implement this, see other existing loggers
|
2015-04-14 14:53:18 +02:00
|
|
|
in ``src/log/``.
|