Skip to content
uleon edited this page Apr 2, 2014 · 5 revisions

inspired by material published by Tomasz Kaczanowski, Steven Sanderson and other fine sources.

Purpose of unit tests

  • high internal quality
  • not necessarily end-to-end (external quality)

Basic principles

  • a test is a specification of behavior
  • a test is a valuable asset and as such should be treated as first class citizen of our code
  • writing a test is not a one-time activity, therefore concentrate on the important tests – what do you really need to test (see test method names)

Basic structure of tests

START_TEST(UA_DateTime_calcSize_NullArgumentShouldReturnStorageSize) 
{
// **given** data and/or context
 UA_DateTime arg = UA_NULL;
// **when** contract is fullfilled correctly by function
 UA_Int32 storageSize = UA_DateTime_calcSize(&arg);
// **then** this behaviour should be observed
 ck_assert_int_eq(storageSize, 8);
}
END_TEST

How to write good tests

Concentrate on one feature in a single test

  • split valid / invalid
  • split happy-path / error-path

Readability is the king

  • Use speaking defines instead of simple values (do not teach the API)
  • Test shall tell the whole story

Test method names are important

  • Use describing names! A good name tells which part of the system fails if the test fails.
  • UA_DateTime_test tells nothing, use something like UA_DateTime_calcSize_NullArgumentShouldReturnStorageSize

How to structure tests cases, suites and packages

test case

A test case is the smallest unit of testing. It checks for a specific response to a particular set of inputs (https://docs.python.org/2/library/unittest.html#unittest.TestCase), see section concentrate.

test suite

A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together (https://docs.python.org/2/library/unittest.html#unittest.TestCase)

For instance testing the relevant aspects of all the basic data types that can be encoded in an IEC 62541 structure (i.e. UA_Variant) makes up a perfect suite which still has a manageable volume.

test package

A test package is a build target for an executable in the tests-directory.

If you want to introduce a new abstract data type such as a list or a namespace you might want to start with specifying the normative behavior in a corresponding test package (Test Driven Development).