Re: g_assert_handler for unit-tests



Stefan Kost <kost imn htwk-leipzig de> writes:

> Hi Michael,
>
> I am aware that the the g_return_if_fail() macros log the failed tests like
> g_assert() does. In combination with the fatal_mask the same behaviour as the
> asserts can be enforced.
> But all of this does not change the situation of the problem. The unit test
> cannot check wheter the error is handled correctly. Functions that return
> something can be checked a little (the should return e.g. NULL), but what about
> void functions?

Once you installed that handler, you can figure if it's called
while a unit test is run. Maybe I don't exactly understand the
problem, but it looks like you are looking for something like:


void
function_to_test (args)
{
  g_return_if_fail (precondition);

  ...
}

gboolean test_failed = FALSE;

void
unit_test_log_func (const gchar   *log_domain,
                    GLogLevelFlags log_level,
                    const gchar   *message,
                    gpointer       user_data)
{
  print_message ();

  test_failed = TRUE;
}

void
do_unit_tests ()
{
  guint handler_id;

  handler_id = g_log_set_handler ("my_domain", G_LOG_LEVEL_CRITICAL,
                                  unit_test_log_func, NULL);

  test_failes = FALSE;

  function_to_test (wrong_args);

  if (test_failed)
    g_printerr ("eek, the test failed on params: %s", wrong_args);
}


ciao,
--mitch



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]