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

problem refactoring tests, expect can't be set in setup()


I am doing an example using cmocka on github.  Here is the link to the repo.  http://bit.ly/tddec-cmocka

I'm in the process of refactoring tests and I discovered something.  If I extract common code into a helper function it needs the parameter 'void state**'.  If I try to use setup() to call  expect_enter_programming_mode(), the test fails.  The code passes its one test.

static void expect_enter_programming_mode(void **state)
    expect_value(io_write, offset, 0);
    expect_value(io_write, data, 0x40);
    expect_value(io_write, offset, 0xdead);
    expect_value(io_write, data, 0xbeef);

static void setup(void **state) {
//	expect_enter_programming_mode(state); //1

static void teardown(void **state) {

static void test_program_succeeds_ready_immediately(void **state) {
	expect_enter_programming_mode(state); //2
	expect_value(io_read, offset, 0);
    will_return(io_read, 1<<7);

    assert_int_equal(FLASH_SUCCESS, flash_program(0xdead, 0xbeef));

static const UnitTest tests[] = {
		unit_test_setup_teardown(test_program_succeeds_ready_immediately, setup, teardown),

int run_flash_tests() {
    print_message("\n============ starting %s\n", __FILE__);
    return run_tests(tests);

If I comment in //1 and comment out //2 I get two test failures, when there should be zero.

============ starting flash_tests.c
[==========] Running 3 test(s).
io_write.offset parameter still has values that haven't been checked.
  Remaining item(s) declared at...
data parameter still has values that haven't been checked.
  Remaining item(s) declared at...
[  FAILED  ] test_program_succeeds_ready_immediately_setup
[ RUN      ] test_program_succeeds_ready_immediately
No entries for symbol io_write.
ERROR: io_mock.c:16 - Could not get value to check parameter offset of function io_write
There were no previously declared parameter values for this test.
[  FAILED  ] test_program_succeeds_ready_immediately
[==========] 2 test(s) run.
[  PASSED  ] 0 test(s).
[  FAILED  ] 2 test(s), listed below:
[  FAILED  ] test_program_succeeds_ready_immediately_setup
[  FAILED  ] test_program_succeeds_ready_immediately

thanks, James

James Grenning					Author of TDD for Embedded C
www.renaissancesoftware.net			http://pragprog.com/titles/jgade/

Re: problem refactoring tests, expect can't be set in setup()Andreas Schneider <asn@xxxxxxxxxxxxxx>
unit_test_setup()James Grenning <james@xxxxxxxxxxxx>