Docsity

Unit Testing: Java, jUnit, Emma, C, Check and Splint - Prof. Spiros Mancoridis, Study notes of Engineering

Partial preview of the text

Download Unit Testing: Java, jUnit, Emma, C, Check and Splint - Prof. Spiros Mancoridis and more Study notes Engineering in PDF only on Docsity!

Unit Testing Testing

Plan

  • Java
    • jUnit (test cases)
    • Emma (coverage)
  • C
    • Check (test cases)
    • Splint (static checking)

jUnit: Anatomy of a test case

@Test public void testAdd(){

int i = SimpleProg.add_nums(5,4);

assertEquals(9,i);

jUnit: Anatomy of a test case

@Test public void testAdd(){

int i = SimpleProg.add_nums(5,4);

assertEquals(9,i);

Running jUnit

  • Compiling with jUnit
    • javac -cp junit-4.5.jar:. *.java
  • Running with jUnit
    • java -cp junit-4.5.jar:. org.junit.runner.JUnitCore testSimpleProg

Emma: Code Coverage

  • Emma is a free code coverage tool for Java
  • Code can be instrumented using Emma or

executed by the emma runner

  • http://emma.sourceforge.net/

What is Coverage?

  • An indication of how much

we are testing

  • Having good code coverage

does not mean we have good

tests

Running Emma

java -cp emma.jar:junit-4.5.jar:. emmarun -sp.
-ix +SimpleProg -r html -cp junit-4.5.jar:.
org.junit.runner.JUnitCore testSimpleProg

Unit Testing C: Check

  • Check is a unit testing framework, similar to

jUnit

  • Idea is the same:
    • Make test cases
    • Compile them in
    • User a test runner to execute them
  • http://check.sourceforge.net/

Anatomy of a Check Unit Test

START_TEST (test_add)

int i = addNums(5,4);

fail_unless(i==9, "i not set correctly");

END_TEST

Running Check

  • Compiling with Check
    • gcc -o test testHello.c hello.c \
check-0.9.5/src/*.o -Icheck-0.9.5/src
  • Run the application as normal
    • test
Running suite(s): Simple Check
100%: Checks: 1, Failures: 0, Errors: 0

Output:

Static Checking with Splint

  • Splint checks individual source files before

compilation

  • Can find a large variety of bugs and point

developers to problem areas

  • Has problems with finding false positives and

thus making complex code to work with Splint

can be difficult

  • Users can annotate their code to help Splint

to make certain assumptions

  • http://www.splint.org/

Sample Splint Output 1

splint hello2.c Splint 3.1.1 --- 07 Dec 2007 hello2.c: (in function main) hello2.c:8:11:Variable c used before definition An rvalue is used that may not be initialized to a value on some execution path. (Use -usedef to inhibit warning) hello2.c:11:16: Possibly null storage tmp passed as non-null param: reflect (tmp) A possibly null pointer is passed as a parameter corresponding to a formal parameter with no /@null@/ annotation. If NULL may be used for this parameter, add a /@null@/ annotation to the function parameter declaration. (Use -nullpass to inhibit warning) hello2.c:10:17: Storage tmp may become null hello2.c:11:16: Passed storage tmp not completely defined (tmp is undefined): reflect (tmp) Storage derivable from a parameter, return value or global is not defined. Use /@out@*/ to denote passed or returned storage which need not be defined. (Use -compdef to inhibit warning) hello2.c:10:41: Storage *tmp allocated ............

Sample Splint Output 2

hello2.c:15:12: Fresh storage tmp not released before return A memory leak has been detected. Storage allocated locally is not released before the last reference to it is lost. (Use -mustfreefresh to inhibit warning) hello2.c:10:41: Fresh storage tmp created hello2.c:1:6: Function exported but not used outside hello2: reflect A declaration is exported, but not used outside this module. Declaration can use static qualifier. (Use -exportlocal to inhibit warning) hello2.c:4:1: Definition of reflect Finished checking --- 5 code warnings zsh: exit 1 splint hello2.c