christian hujer what is aceunit? how does aceunit work? how do i use aceunit? 2007-12-161© 2007...

39
AceUnit Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-16 1 © 2007 Christian Hujer

Upload: ana-breech

Post on 14-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 1

AceUnit

Christian Hujer

What is AceUnit?How does AceUnit work?How do I use AceUnit?

2007-12-16

Page 2: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 2

About AceUnitWhat is AceUnit? Why Unit Test?Requirements, Relationship to other Frameworks

2007-12-16

Page 3: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 3

Unit Testing

Find bugs early Prevent regression Speedup development Embrace change / Refactoring Grey Box Testing

2007-12-16

Page 4: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 4

Requirements

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-12-16

Page 5: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 5

History / Relationship

AceUnit

EmbUnit• C• Embedded

JUnit 4.x• (Java)• Convenient

CUnit• C• Desktop

2007-12-16

Page 6: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 6

What is AceUnit?

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-12-16

Page 7: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 7

End of About AceUnit

Further Information AceUnit:

http://aceunit.sourcefourge.net/ CUnit:

http://cunit.sourceforge.net/ EmbUnit:

http://embunit.sourceforge.net/ JUnit:

http://junit.sourceforge.net/

Questions?2007-12-16

Page 8: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 8

AceUnit ArchitectureAceUnit ComponentsAceUnit Framework

2007-12-16

Page 9: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 9

AceUnit Components

AceUnit Generator

•Parses C Sources•Performs Reflection•Evaluates Annotations•Creates Fixtures and Suites (planned)

AceUnit Framework

•Provides Assertions•Collects Errors•Runs Fixtures and Suites (planned)•Connects to Loggers

2007-12-16

Page 10: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 10

AceUnit Framework

2007-12-16

SuiteFixtur

e

Runner

Logger

Assert

Tests

Data

Page 11: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 11

End of AceUnit Architecture

Further Information AceUnit: http://aceunit.sourceforge.net/

Questions?

2007-12-16

Page 12: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 12

How to use AceUnitWriting TestsUsing AssertionsIntegrating AceUnit in your buildConfiguring AceUnit

2007-12-16

Page 13: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 13

Writing Tests

Annotations Fixtures Assertions Suites Logging

2007-12-16

Page 14: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 14

Annotations / Metadata

Programming Languages: Java, C# Metadata about members and other

accessible elements Examples (Java):

@PrimaryKey private String key; @NotNull private String name; void setColor(@Nullable final Color

color);

2007-12-16

Page 15: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 15

Annotations in AceUnit

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 fixture2007-12-16

Page 16: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 16

Fixtures

Collection of test cases with same preconditions

Test source = test class = fixture

2007-12-16

Page 17: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 17

Fixture Example

/** @file 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-12-16

Page 18: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 18

Using Assertions

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-12-16

Page 19: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 19

Assertions Example

/** My test case. */A_Test void testFoo1() { #define EXPECTED = 42; uint16_t actual = getAnswer(); assertEquals("Expected answer to

be 42.", EXPECTED, actual);}

2007-12-16

Page 20: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 20

A_Before / A_After

Test Case Setup / Teardown Prepare data for / cleanup after each

test case Like setUp() / tearDown() in JUnit 3.x Like @Before / @After in JUnit 4.x Multiple A_Before / A_After possible Execution order = order in source

code

2007-12-16

Page 21: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 21

A_Before / A_After Example

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-12-16

Page 22: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 22

A_BeforeClass / A_AfterClass

Prepare data for all tests Fixture Setup / Teardown Like @BeforeClass / @AfterClass in

JUnit 4.x New in JUnit 4.x / AceUnit Multiple A_BeforeClass / A_AfterClass

possible Execution order = order in source

code2007-12-16

Page 23: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 23

A_BeforeClass / A_AfterClass Example

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-12-16

Page 24: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 24

Fixtures (Roundup)

Fixture Setup

• A_BeforeClass

• A_AfterClass

Test Case Setup

• A_Before

• A_After

Test Cases

• A_Test

2007-12-16

Page 25: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 25

Further Notes on Annotations

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-12-16

Page 26: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 26

Integrating AceUnit in your build

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-12-16

Page 27: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 27

Configuring AceUnit (1/3) 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-12-16

Page 28: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 28

Configuring AceUnit (2/3)

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-12-16

Page 29: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 29

Configuring AceUnit (3/3)

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-12-16

Page 30: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 30

The Generator

Small Java Program Creates headers for fixtures "Connects" your fixtures with

AceUnit Requires JRE 1.5 or newer

2007-12-16

Page 31: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 31

Invoking the Generator

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-12-16

Page 32: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 32

Invoking the Generator

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-12-16

Page 33: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 33

End of How to use AceUnit

Questions?

2007-12-16

Page 34: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 34

Final ThoughtsFootprint, FutureUnit Tests in Software Engineering

2007-12-16

Page 35: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 35

AceUnit Footprint

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-12-16

Page 36: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 36

AceUnit Footprint Example

5 Fixtures with 40 methods, 30 test cases

Code: 4 * 40 + 48 * 5 + 2 * 30 plus code = 460 plus code

Data (5 failure entries): 16 + 5 * 2 + Jump Buffer = 26 + Jump Buffer

2007-12-16

Page 37: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 37

The Future of AceUnit

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-12-16

Page 38: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 38

Develop for Testability!

Axioms:

• Software is not by default Unit testable• Testability is an Architecture and Design issue

2007-12-16

Page 39: Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? 2007-12-161© 2007 Christian Hujer

© 2007 Christian Hujer 39

The End

Thanks for Listening!

Questions?

2007-12-16