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

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


On Monday 29 July 2013 13:44:07 James Grenning wrote:
> Hi
> 
> 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.
> 
> <code>
> 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);
> }
> </code>
> 
> If I comment in //1 and comment out //2 I get two test failures, when there
> should be zero.

Cause you can't do that in a setup routine. The setup routine is for setting 
the environment and maybe creating a memory context. You can the pass it to 
the test using the 'state' parameter.

The expect_* functions are setup at the start of a test. Setting them up 
earlier will not work (like in a setup function).


	-- andreas


-- 
Andreas Schneider                   GPG-ID: F33E3FC6
www.cryptomilk.org                asn@xxxxxxxxxxxxxx


References:
unit_test_setup()James Grenning <james@xxxxxxxxxxxx>
problem refactoring tests, expect can't be set in setup()James Grenning <james@xxxxxxxxxxxx>