Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? © 2007 Christian Hujer
What is AceUnit? Why Unit Test? Requirements, Relationship to other Frameworks © 2007 Christian Hujer2
Find bugs early Prevent regression Speedup development Embrace change / Refactoring Grey Box Testing © 2007 Christian Hujer
Easy to use As close to JUnit 4.x as possible Highly Configurable and Adoptable Suits "normal" and embedded development Logging compatible with JUnit Low memory consumption and footprint High degree of convenience © 2007 Christian Hujer
© 2007 Christian Hujer
Advanced C and Embedded Unit Unit Test Framework for C (C89 and C99) Open Source (License: BSD (revised)) Target environment: Desktop Embedded Architecture based on JUnit 4.x: Uses Annotations Uses Reflection © 2007 Christian Hujer
Further Information AceUnit: CUnit: EmbUnit: JUnit: Questions? © 2007 Christian Hujer
AceUnit Components AceUnit Framework © 2007 Christian Hujer8
© 2007 Christian Hujer
© 2007 Christian Hujer10 SuiteFixture Runner Logger Assert Tests Data
Further Information AceUnit: Questions? © 2007 Christian Hujer
Writing Tests Using Assertions Integrating AceUnit in your build Configuring AceUnit © 2007 Christian Hujer12
Annotations Fixtures Assertions Suites Logging © 2007 Christian Hujer
Programming Languages: Java, C# Metadata about members and other accessible elements Examples (Java): private String key; private String name; void final Color color); © 2007 Christian Hujer14
Like JUnit 4.x. No in C -> 'A_' instead. Default: #defined as empty macros A_Test Test case A_Before, A_After Executed before / after each test case A_BeforeClass, A_AfterClass Executed at start / end of fixture © 2007 Christian Hujer
Collection of test cases with same preconditions Test source = test class = fixture © 2007 Christian Hujer
fooTest.c */ /* fooTest.h generated by AceUnit Generator */ #include "fooTest.h" /** Some test case. */ A_Test void testFoo1() {…} /** Another test case. */ A_Test void testFoo2() {…} © 2007 Christian Hujer
Verification is done with Assertions See AceUnit documentation for list You can define your own Assertions Pattern: assertXXX(message, params) #define ACEUNIT_EMBEDDED: message is ignored Instead uses line number as uint16_t © 2007 Christian Hujer
/** My test case. */ A_Test void testFoo1() { #define EXPECTED = 42; uint16_t actual = getAnswer(); assertEquals("Expected answer to be 42.", EXPECTED, actual); } © 2007 Christian Hujer
Test Case Setup / Teardown Prepare data for / cleanup after each test case Like setUp() / tearDown() in JUnit 3.x in JUnit 4.x Multiple A_Before / A_After possible Execution order = order in source code © 2007 Christian Hujer
static Foo_t *foo; A_Before void createFoo() { foo = (Foo_t *) malloc(sizeof(Foo_t)); bzero(foo, sizeof(Foo_t)); } A_After void destroyFoo() { free(foo); } © 2007 Christian Hujer
Prepare data for all tests Fixture Setup / Teardown in JUnit 4.x New in JUnit 4.x / AceUnit Multiple A_BeforeClass / A_AfterClass possible Execution order = order in source code © 2007 Christian Hujer
static Foo_t *foo; A_BeforeClass void allocateFoo() { foo = (Foo_t *) malloc(sizeof(Foo_t)); } A_Before void initFoo() { bzero(foo, sizeof(Foo_t)); } A_AfterClass void freeFoo() { free(foo); } © 2007 Christian Hujer
© 2007 Christian Hujer
A_Test, A_Before etc. Must be on same line as annotated method Should be before other modifiers of method ACEUNIT_STATIC_ANNOTATIONS Define this to #define annotations static Pro: No name clashes between fixtures Con: Problems with some debuggers © 2007 Christian Hujer
aceunit/src/native Add to include search path Add to C search path Before compilation Run generator Include in compilation: AceUnit.c AceUnitData.c Logger of your choice © 2007 Christian Hujer
ACEUNIT_EMBEDDED Use AceUnit in embedded mode less footprint uint16_t instead of char* ACEUNIT_C99 Use AceUnit with C99 instead of C89/C90. ACEUNIT_STATIC_ANNOTATIONS #define A_* static © 2007 Christian Hujer
ACEUNIT_CODE_INCLUDE Define file to include for code (ROM). Use for section information. ACEUNIT_DATA_INCLUDE Define a file to include before variables (RAM). Use for section information © 2007 Christian Hujer28
ACEUNIT_ASSERTION_STYLE Define how to "implement" fail Possible values: ▪ ACEUNIT_ASSERTION_STYLE_RETURN ▪ ACEUNIT_ASSERTION_STYLE_ASSERT ▪ ACEUNIT_ASSERTION_STYLE_ABORT ▪ ACEUNIT_ASSERTION_STYLE_LONGJMP ▪ ACEUNIT_ASSERTION_STYLE_CUSTOM ▪ -> Define ACEUNIT_CUSTOM_ASSERT() © 2007 Christian Hujer29
Small Java Program Creates headers for fixtures "Connects" your fixtures with AceUnit Requires JRE 1.5 or newer © 2007 Christian Hujer30
Arguments: List of Suite names ▪ Packages (directories, will be searched recursively) ▪ Fixtures (omit.c suffix!) Options: --exit Quit Java VM with error code. (Use!) -h, --help: Display help and exit. -f, --force: Overwrite write-protected files © 2007 Christian Hujer31
Example (in a Makefile): prepareAceUnit: java -jar AceUnit.jar --exit src/ Example (Batch): java -jar AceUnit.jar --exit src/ && cc $(find –name *.c) -o runTests &&./runTests © 2007 Christian Hujer32
Questions? © 2007 Christian Hujer33
Footprint, Future Unit Tests in Software Engineering © 2007 Christian Hujer34
In bytes for 32 Bit with ACEUNIT_EMBEDDED Code (constant data, anywhere) 4 per annotated methods 48 per Fixture 2 per test case Plus code (testling and fixture) Data (variable data, RAM) 16 for Runner N*2 for Logging (MiniRamLogger, N = log failures) 1 Jump Buffer (if setjmp()/longjmp() is used) © 2007 Christian Hujer
5 Fixtures with 40 methods, 30 test cases Code: 4 * * * 30 plus code = 460 plus code Data (5 failure entries): * 2 + Jump Buffer = 26 + Jump Buffer © 2007 Christian Hujer
Generator Ant Task for Java Generator (Q1 2008) Alternative Perl Generator (Q1 2008) Alternative C Generator (?) Framework More Assertions (Q1 2008) Internal Refactoring (encapsulation, public API) (?) Pluggable Runners (Q2 2008) Support of Suites (Q2 2008) © 2007 Christian Hujer37
© 2007 Christian Hujer38
Thanks for Listening! Questions? © 2007 Christian Hujer39