Check Tutorial
Prev |  | Next

# Chapter 4. Advanced Features

# 4.1. Running multiple cases

What happens if we pass -1 as the amount in money_create? What should happen? Let's write a unit test. Since we are testing limits, we should also test what happens when we create money with amount 0:

    START_TEST (test_neg_create)
    {
      Money *m = money_create(-1, "USD");
      fail_unless(m == NULL,
                  "NULL should be returned on attempt to create with a negative amount");
    }
    END_TEST

    START_TEST (test_zero_create)
    {
      Money *m = money_create(0, "USD");
      fail_unless(money_amount(m) == 0, "Zero is a valid amount of money");
    }
    END_TEST

Let's put these in a separate test case, called “Limits” so that money_suite looks like so:

    Suite *money_suite (void) {
      Suite *s = suite_create("Money");
      TCase *tc_core = tcase_create("Core");
      TCase *tc_limits = tcase_create("Limits");
      suite_add_tcase(s, tc_core);
      suite_add_tcase(s, tc_limits);
      tcase_add_test(tc_core, test_create);
      tcase_add_test(tc_limits, test_neg_create);
      tcase_add_test(tc_limits, test_zero_create);
      return s;
    }

Now we can rerun our suite, and fix the problem(s). Note that errors in the Core test case will be reported as “Core” and errors in the Limits test case will be reported as “Limits,” giving you additional information about where things broke.

Prev | Home | Next
SRunner output |  | No fork mode