13
KARLSRUHE INSTITUTE OF TECHNOLOGY (KIT) - GERMANY KSETA Topical Courses - Unit Testing Manuel Giffels, Thomas Hauth, Martin Heck | 15. September 2016 KIT – University of the State of Baden-Wuerttemberg and National Laboratory of the Helmholtz Association www.kit.edu

KSETA Topical Courses - Unit Testing - KIT - ETP Institut ...giffels/KSETA-Workshop_UnitTest.pdfKARLSRUHE INSTITUTE OF TECHNOLOGY (KIT) - GERMANY KSETA Topical Courses - Unit Testing

  • Upload
    hathu

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

KARLSRUHE INSTITUTE OF TECHNOLOGY (KIT) - GERMANY

KSETA Topical Courses - Unit Testing

Manuel Giffels, Thomas Hauth, Martin Heck | 15. September 2016

KIT – University of the State of Baden-Wuerttemberg and National Laboratory of the Helmholtz Association

www.kit.edu

How to be Agile ?

The Agile Development Methodology encourages the developers to be:

flexible - adapt to new requirements

fast - make changes to the implementation on a short timescale

courageous -change key parts of the software frequently

ready - release versions of the software very often

Luckily: The Agile Toolbox provides techniques to support the developers !

Important: Using these techniques is important for a successful Agiledevelopment processOtherwise, Agile might result in

un-controlled

un-coordinated

un-necessarily wasted time

Manuel Giffels, Thomas Hauth, Martin Heck – KSETA Topical Courses - Unit Testing 15. September 2016 2/14

Unit Testing

Manuel Giffels, Thomas Hauth, Martin Heck – KSETA Topical Courses - Unit Testing 15. September 2016 3/14

How do you ensure your program”works” ?

First consideration: what does ”works” mean ?

Completes processing in the required amount of time

Returns the correct result

Behaves appropriately in case of faulty input

Does not crash

Stays inside of the allocated memory and disk space limit

At which stage are these checks necessary ?

During development

Before a release

When changing compile, runtime or external libraries

Question: How do you ensure these points during your developmentprocess ?

Manuel Giffels, Thomas Hauth, Martin Heck – KSETA Topical Courses - Unit Testing 15. September 2016 4/14

Unit Testing

”[Unit test] are the tests that you write to verify the operation of your code. Thesetests aren’t at the level of features. They are at the level of methods andfunctions.” 1

Unit tests are

automated

compact and test the smallest possible unit of functionality (a class, a method)In contrast to: Integration tests to validate the interplay between components

fast: the whole set of units tests should complete in the order of seconds

part of the source base and available to every programmer working on theproject

1Jeff Younker, Foundations of Agile Python Development, Apress, 2008Manuel Giffels, Thomas Hauth, Martin Heck – KSETA Topical Courses - Unit Testing 15. September 2016 5/14

Unit Test Frameworks

Unit test frameworks exist for every language and all offer a very similar set offeatures:

a way to express test cases with a number of tests ...

... and group them together in so called test suites

method/functions/macros to check for correct output of the tested sourcecode

checks for error handling and exceptions

Test Runner: executing all or a sub-set of unit tests and report the result

Available Frameworks:

Python: unittest package (part of python std. library)

C++: googletest, Boost.Test

Java: JUnit

Manuel Giffels, Thomas Hauth, Martin Heck – KSETA Topical Courses - Unit Testing 15. September 2016 6/14

Python unit test example

Our task: implementing a class to compute an average over multiple values:

class Avg:

def __init__(self):

...

def add(self , number ):

# add a number to average

...

def compute(self):

# compute and return the average

...

Question: What would you test for this implementation ?

Manuel Giffels, Thomas Hauth, Martin Heck – KSETA Topical Courses - Unit Testing 15. September 2016 7/14

Python unit test example

What you should always try to test for:

The obvious: averaging n value

The unusual but valid: average of one value

The error case: no numbers added at all

What should/cannot be covered by a unit test:

All possible combinations of input values

Situations which are outside of the region of concern of the code under test(no memory left etc.)

Goal:Try to keep the unit test code as compact and precise as possible

Rule of thumb: About the same amount of unit test code as productioncode

Manuel Giffels, Thomas Hauth, Martin Heck – KSETA Topical Courses - Unit Testing 15. September 2016 8/14

Python unit test exampleOne possible solution for the unit test:

class AvgTest(unittest.TestCase ):

def test_zero_entries(self):

avg = Avg()

with self.assertRaises(NoEntries ):

avg.compute ()

def test_one_entry(self):

avg = Avg()

avg.add (23.0)

self.assertEqual (23.0, avg.compute ())

def test_three_entries(self):

avg = Avg()

avg.add (10.0)

avg.add ( -20.0)

avg.add (1.0)

self.assertAlmostEqual ((10.0+1.0+ -20.0)/3 ,

avg.compute ())

Manuel Giffels, Thomas Hauth, Martin Heck – KSETA Topical Courses - Unit Testing 15. September 2016 9/14

What to do in more complex scenarios ?

”Yeah, but I cannot unit test my code, because I use a [database, network,put your favorite external component here] !

Not true, in these cases unit testing may become more challenging but isstill possible and important !

External components can be hidden behind interface classes

Certain classes might only work in conjunction with others

Some classes may need complex input data to execute their code

The python unittest.mock2 library can help: It can replace any python methodwith test code.

2https://docs.python.org/3.5/library/unittest.mock.htmlManuel Giffels, Thomas Hauth, Martin Heck – KSETA Topical Courses - Unit Testing 15. September 2016 10/14

Test Coverage

The python coverage3 tool can analyze which part of the the code base isactually executed by the unit tests.

To execute and analyze the unit tests:

python3.4 -m coverage run ./drift_tests.py

To create a text report:

python3.4 -m coverage report

To create a html report:

python3.4 -m coverage html

Test Coverage can be a quality criteria of a software project and a test coverageof 100% is desirable.

3https://pypi.python.org/pypi/coverageManuel Giffels, Thomas Hauth, Martin Heck – KSETA Topical Courses - Unit Testing 15. September 2016 11/14

Test Coverage - HTML report

Manuel Giffels, Thomas Hauth, Martin Heck – KSETA Topical Courses - Unit Testing 15. September 2016 12/14

Test Driven Development

Test Driven Development is a programming technique in which the requirementon a piece of code is expressed with a unit test, before the code is written.

Typical workflow:

1) Write a set of unit tests which check for the correct behavior, failure casesand edge cases

2) Implement code for the new class

3) Run unit tests

Goto 2 if unit tests fail

Advantages of Test Driven Development:

Programmer has to think about the interfaces and behavior beforehand

Code is written to be test-able

Writing unit tests is part of regular development workflow

Manuel Giffels, Thomas Hauth, Martin Heck – KSETA Topical Courses - Unit Testing 15. September 2016 13/14