+ All Categories
Home > Documents > Unit Testing 0

Unit Testing 0

Date post: 10-Apr-2018
Category:
Upload: jacob-hamilton
View: 224 times
Download: 0 times
Share this document with a friend

of 22

Transcript
  • 8/8/2019 Unit Testing 0

    1/22

    Software Construction

    Unit Testing

    Christoph Denzler / Martin KroppUniversity of Applied Sciences Northwestern SwitzerlandInstitute for Mobile and Distributed Systems

  • 8/8/2019 Unit Testing 0

    2/22

  • 8/8/2019 Unit Testing 0

    3/22

    Agenda

    Introduction into Testing

    Unit TestingTesting with JUnit

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 3

  • 8/8/2019 Unit Testing 0

    4/22

    The Why, The What, The How

    It has to do with quality!

    Your software has to meet customerre uirements

    functional and non-functional requirements

    How do you ensure that your software doeswhat it should?

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 4

  • 8/8/2019 Unit Testing 0

    5/22

    The Why, The What, The How

    It has to do with safety!

    You often have to change your existing codebecause of new re uirements, im rovements,and bug fixes

    How do you ensure, that your changed softwarestill does what it did before?

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 5

  • 8/8/2019 Unit Testing 0

    6/22

    The Why, The What, The How

    It has to do with sustainability!

    You still have to fix bugs

    How do you ensure, that a fixed bug nevershows up again? (or you notice it immediately,at least)

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 6

  • 8/8/2019 Unit Testing 0

    7/22

    What is Testing?

    Testing is the process of evaluating a particular

    product to determine if it meets the customersrequirements.

    That means1. There must be well specified requirements to

    test against (expected results)2. Testing is comparing actual results to expected

    results

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 7

  • 8/8/2019 Unit Testing 0

    8/22

    What is Testing?

    The deviation of an actual result from an

    expected result, is called a defectTesting can only show the presence of a defectbut not the absence of defects

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 8

  • 8/8/2019 Unit Testing 0

    9/22

    The First Bug

    Grace Hoppers bug (moth stuck in a relay on

    an early machine)

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 9

  • 8/8/2019 Unit Testing 0

    10/22

    Some Famous Software Failures

    1991: The Patriot Missile failure , in Dharan, Saudi Arabia, on February 25, 1991which resulted in 28 deaths, is ultimately attributable to poor handling of roundingerrors.1991: The sinking of the Sleipner A offshore platform in Gandsfjorden nearStavanger, Norway, on August 23, 1991, resulted in a loss of nearly one billiondollars. It was found to be the result of inaccurate finite element analysis.1996: The explosion of the Ariane 5 rocket just after lift-off on its maiden voyageoff French Guiana, on June 4, 1996, was ultimately the consequence of a simpleoverflow.1999: NASAs Mars lander , September 1999, crashed due to a units integrationfaultover $50 million US !2005: Arbeitslosengeld II = ALG-II in Germany:writing 9 digit bank codes left-aligned in 10 digit fields leads to adding a zero at the end and wrong bank codes.2010: Year 2010 bug on German bankcards (Jan 4 2010): It showed up to be asoftware defect due to incorrect year handling.

    More on http://www5.in.tum.de/~huckle/bugse.html

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 10

  • 8/8/2019 Unit Testing 0

    11/22

  • 8/8/2019 Unit Testing 0

    12/22

    The Why, The What, The How

    You can do this manually

    Writing test specifications and protocolsOr (much better) automated

    Let the system do it for you (at least part of it)

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 12

  • 8/8/2019 Unit Testing 0

    13/22

    Testing Dimensions

    Level

    system

    integration

    component

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 13

    Accessibility

    Target

    unit

    white box black box stress

    performance

    reliability

    usability

    robustness

    functionality

  • 8/8/2019 Unit Testing 0

    14/22

    The Testing Process

    Test case

    Test data

    Testresults

    Testreport

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 14

    cases

    data

    tests

    results to test data

  • 8/8/2019 Unit Testing 0

    15/22

    Unit Testing

    Unit Testing is done

    on each modulein isolationto verify the units behavior

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 15

    Unit test willestablish some kind of artificial environmentinvoke routines in the module under test

    check the results against expected values

  • 8/8/2019 Unit Testing 0

    16/22

    Using Test Frameworks

    Provides following capabilities

    test specificationtest implementationstandard way to specify setup and cleanup

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 16

    method for selecting individual tests or all availabletestsmeans of analyzing output for expected (orunexpected) resultsstandardized form of failure reporting

    => Use JUnit for Java Unit Testing

  • 8/8/2019 Unit Testing 0

    17/22

    JUnit 4 Examplepackage unittesting;import java.util.NoSuchElementException;

    public class IntArray {

    private int[] array;

    public IntArray(int[] values) { array = values; }

    public int findLargest() throws NoSuchElementException {

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 17

    int max = Integer.MAX_VALUE;if (array.length == 0) { throw new NoSuchElementException(); }for (int num : array) {

    if (num > max) {max = num;

    }}return max;

    }}

  • 8/8/2019 Unit Testing 0

    18/22

    JUnit4

    Uses annotations, static import

    Setup, Cleanup@Before, @After@BeforeClass, @AfterClass

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 18

    @Test@Test(expected=MovieMgmtException.class)@Ignore

    CheckingAssert (use "static import")

  • 8/8/2019 Unit Testing 0

    19/22

    Using JUnit Asserts

    Use Asserts byusing the prefixed syntaxAssert.assertEquals()

    importing statically the Assert classimport static org.unit.Assert.assertEquals

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 19

    assertEquals()assertEquals([String message], expected, actual)assertEquals([String message], expected, actual,

    tolerance)

    assertNull(), assertNotNull(), assertSame(), assertNotSame(),assertTrue(), assertFalse(), fail()

  • 8/8/2019 Unit Testing 0

    20/22

    Organizing Your Test

    Separate tests from production code

    myProject/trunk/src/prod/src/test // as under prod/src/test/data // for test data

    Put it into the same packages

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 20

    log takes all output

    myProject/trunk/src/prod/ch.fhnw.edu.rental.model/src/test/ch.fhnw.edu.rental.model

  • 8/8/2019 Unit Testing 0

    21/22

    Reflections on Unit Testing

    Coding with ConfidenceWhat is Unit Testing?Why should I bother with Unit Testing?What do I want to accomplish?

    Does it do what I want?Does it do what I want all the time?Can I depend on it?Does it document my intent?

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 21

    ow o o n es ngBad Excuses for not Unit Testing

    It takes too much time to write the testsIt takes too long to run the testsIt's not my job to test my codeI don't really know how the code is supposed to behave so I can't test itBut it compiles

    I'm being paid to write code, not to write testsI feel guilty about putting testers and QA staff out of workMy company won't let me run unit tests on the live system

    from "Pragmatic Unit Testing"

  • 8/8/2019 Unit Testing 0

    22/22

    And Finally

    Some pragmatic tips

    Test Early. Test Often. Test Automatically.

    UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 22

    Coding Ain't Done 'Til All the Tests Run.Tip 63 (The Pragmatic Programmer)


Recommended