4. Testing
image/svg+xml
4. Testing
Zidarics Zoltán
zamek@vili.pmmf.hu
2020
Testing
01101100
01101111
01110110
01100101
01101100
01101111
01110110
01100101
01101100
01101111
01110110
01100101
Embedded
Programming
3.make
5.doxygen
Problem
is the finished software correct?
"No flawless software, just under-tested" Murphy
banana software
embedded systems
mission critical systems
transfer to the customer
does it fulfill the terms of reference?
Solution
creating a test environment
white box if the code to be tested is known
black box (specification based) if testable
code is unknown, but the specification is
what to test?
individual modules (component test)
behavior of multiple modules (integration test)
full sofware / hardware (system test)
compliance with specification (acceptance test)
Component test
unit testing
tests the functions of each module
module test
tests the complex behavior of modules
CUnit
lightweight unit test system
installing: apt-get install libcunit1-dev
documetation
Test registry
Suite 1
Suite n
...
test 11
test 1m
test n1
test nm
...
...
demo
CUnit registry management
CU_initialize_registry()
CU_cleanup_registry()
the registry must be initialized before use
deleting the registry at the end of the tests to free up memory
if (CUE_SUCCESS!=CU_initialize_registry())
return (CU_get_error());
CU_cleanup_registry();
CU_get_registry()
CU_pTestRegistry CU_get_registry(void)
pointer to registry, NULL, if not already CU_initialize_registry ()
CU_set_registry()
CU_pTestRegistry CU_set_registry(CU_pTestRegistry pTestRegistry)
replace the current registry
CUnit suite management
CU_add_suite()
CU_pSuite CU_add_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean)
pSuite=CU_add_suite("Suite 1", init_test, close_test);
if (NULL==pSuite) {
CU_cleanup_registry();
return (CU_get_error());
}
CUnit body treatment
CU_add_test()
CU_BRM_NORMAL errors and output summary
CU_BRM_SILENT only errors on the output
CU_BRM_VERBOSE most information is output
CU_pTest CU_add_test ( CU_pSuite pSuite, const char * strName, CU_TestFunc pTestFunc )
if ((NULL==CU_add_test(pSuite, "Create person test", create_person_test)) ||
(NULL==CU_add_test(pSuite, "Add person test", add_person_test)) ||
(NULL==CU_add_test(pSuite, "find first person by level", find_first_person_by_level_test)) ||
(NULL==CU_add_test(pSuite, "find first person by age", find_first_person_by_age_test))) {
CU_cleanup_registry();
return (CU_get_error());
}
Automated mode
not interactive, test results will be stored in XML file
void CU_automated_run_tests (void)
CU_ErrorCode CU_list_tests_to_file (void)
void CU_set_output_filename (const char * szFilenameRoot)
not interactive, test results are passed to stdout
CU_ErrorCode CU_basic_run_tests (void)
CU_ErrorCode CU_basic_run_suite (CU_pSuite pSuite)
CU_ErrorCode CU_basic_run_test (CU_pSuite pSuite, CU_pTest pTest)
void CU_basic_set_mode (CU_BasicRunMode mode)
interactive, the user can control the progress of the tests
void CU_console_run_tests (void)
interactive, the user can control the progress of the tests
void CU_curses_run_tests (void)
Basic mode
Interactive console mode
Interactive curses mode
CUnit assert-ek
CU_ASSERT_DOUBLE_EQUAL(actual,expected,granularity)
CU_ASSERT_DOUBLE_EQUAL_FATAL(actual,expected,granularity)
verifies that the actual and expected values are granularity
within the limit?
every ASSERT also has a FATAL version which in case of an error
quits the test system.
CU_ASSERT_DOUBLE_NOT_EQUAL(actual,expected,granularity)
CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(actual,expected,granularity)
verifies that the actual and expected values are granularity
is it outside the limit?
CU_ASSERT_EQUAL(actual,expected)
CU_ASSERT_EQUAL_FATAL(actual,expected)
verifies that the actual and expected values are the same?
CU_ASSERT_FALSE(value)
CU_ASSERT_FALSE_FATAL(value)
verifies that the value is FALSE?
CU_ASSERT(value)
CU_ASSERT_FATAL(value)
error message on output
CU_ASSERT_NOT_EQUAL(actual,expected)
CU_ASSERT_NOT_EQUAL_FATAL(actual,expected)
check that the actual and expected values do not match?
CU_ASSERT_NSTRING_EQUAL(actual,expected,count)
CU_ASSERT_NSTRING_EQUAL_FATAL(actual,expected,count)
verifies that the actual and expected strings match the count length?
CU_ASSERT_NSTRING_NOT_EQUAL(actual,expected,count)
CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(actual,expected,count)
verifies that actual and expected strings do not match count count?
CUnit asserts
CU_ASSERT_PTR_EQUAL(actual,expected)
CU_ASSERT_PTR_EQUAL_FATAL(actual,expected)
verifies that actual and expected pointers match?
CU_ASSERT_PTR_NOT_EQUAL(actual,expected)
CU_ASSERT_PTR_NOT_EQUAL_FATAL(actual,expected)
verifies that actual and expected pointers do not match
CU_ASSERT_PTR_NULL(value)
CU_ASSERT_PTR_NULL_FATAL(value)
verifies that the value pointer is NULL?
CU_ASSERT_PTR_NOT_NULL(value)
CU_ASSERT_PTR_NOT_NULL_FATAL(value)
verifies that the value pointer is not NULL
CU_ASSERT_STRING_EQUAL(actual,expected)
CU_ASSERT_STRING_EQUAL_FATAL(actual,expected)
verifies that actual and expected string are equal?
CU_ASSERT_STRING_NOT_EQUAL(actual,expected)
CU_ASSERT_STRING_NOT_EQUAL_FATAL(actual,expected)
verifies that actual and expected string are not equal
CU_ASSERT_TRUE(value)
CU_ASSERT_TRUE_FATAL(value)
error if value is TRUE
CU_FAIL(msg)
CU_FAIL_FATAL(msg)
error, msg is output
CU_PASS(msg)
test is passed, msg message is output
CMocka
mock object supporting
tests memory leaks,buffer
under / overflow
installing: apt-get install libmocka-dev
documentation
demo
CMocka asserts
void assert_false(scalar expression)
verifies that expression is false?
void assert_in_range(LargestIntegralType value,LargestIntegralType minimum,
LargestIntegralType maximum)
verifies that value is greater than or equal to minimum and less than or equal to maximum
void assert_in_set(LargestIntegralType value,LargestIntegralType values[], size_t count)
verifies that value is an element of the received values?
void assert_int_equal(int a, int b)
verifies that a is equal to b?
void assert_int_not_equal(int a, int b)
checks that the value of a does not match b
void assert_memory_equal(const void * a,const void * b,size_t size )
verifies that the memory content matches size a from memory addresses a and b
void assert_memory_not_equal(const void * a,const void * b,size_t size )
verifies that the memory content does not match the size of memory addresses a and b
void assert_null(void *pointer)
verifies that the pointer contains a NULL value
void assert_not_in_range(LargestIntegralType value,LargestIntegralType minimum,
LargestIntegralType maximum)
verifies that value is less than minimum and greater than maximum?
void assert_not_in_set(LargestIntegralType value,LargestIntegralType values[], size_t count)
verifies that value is not an element of values
void assert_non_null(void *pointer)
verifies that the contents of the pointer are not NULL
CMocka asserts
void assert_ptr_equal(void *a,void *b)
checks that a and b pointers are the same
void assert_ptr_not_equal(void *a,void *b)
verifies that the values of pointer a and b are not the same
void assert_string_equal(const char *a,const char *b)
verifies that the contents of a and b are the same
void assert_string_not_equal(const char *a,const char *b)
verifies that the contents of strings a and b are not the same
void assert_true(scalar expression)
verifies that the expression value is true
Unit test rules
write tests for each parameter of each function
test with values outside the accepted range of the parameter
Also test the pointer parameter with a NULL pointer
After calling a memory allocation function, check the memory loss
Task
create tests for the P3_ListDemo project
you can use the CUnit or CMocka framework
upload the finished project to your own gitlab repository
P3_ListDemo.zip
1
Main
Problem
Solution
Component test
CUnit
CUnit registry
CUnit suite
CUnit body treatment
CUnit basic/interactive
CUnit asserts I.
CUnit asserts II.
CMocka
CMocka asserts I.
CMocka asserts II.
Unit test rules
Task
next