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

An overview of unit testing in java using junit and emma for code coverage, as well as unit testing in c using check. How to write test cases, organize them into suites, and run the tests using these frameworks. It also introduces splint for static checking in c. Junit is a testing harness for java that enables quick and simple test creation, execution, and evaluation. Test cases can be scattered in the code or organized into separate suites. Emma is a free code coverage tool for java that can be used to instrument code or run tests with the emma runner. Code coverage is an indication of how much of the code is being tested, but having good coverage does not necessarily mean having good tests. Check is a unit testing framework for c that follows the same idea as junit. The document also includes examples of test cases and test suites, as well as instructions for running junit, emma, and check.

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-las-1
koofers-user-las-1 🇺🇸

10 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Unit Testing Testing!
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

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