CS115 Class 15: Testing

39
1 CS115 Class 15: Testing Due today User Manual Software Inspections – Review: Testing Next time – Review No Silver Bullet

description

CS115 Class 15: Testing. Due today User Manual Software Inspections Review: Testing Next time Review No Silver Bullet. French Guyana, June 4, 1996. $800 million software failure. Mars Climate Orbiter. - PowerPoint PPT Presentation

Transcript of CS115 Class 15: Testing

Page 1: CS115 Class 15: Testing

1

CS115 Class 15: Testing

• Due today– User Manual– Software Inspections– Review: Testing

• Next time– Review No Silver Bullet

Page 2: CS115 Class 15: Testing

2

French Guyana, June 4, 1996$800 million software failure

Page 3: CS115 Class 15: Testing

3

Mars Climate Orbiter

– The 125 million dollar Mars Climate Orbiter is assumed lost by officials at NASA. The failure responsible for loss of the orbiter is attributed to a failure of NASA’s system engineer process. The process did not specify the system of measurement to be used on the project. As a result, one of the development teams used Imperial measurement while the other used the metric system of measurement. When parameters from one module were passed to another during orbit navigation correct, no conversion was performed, resulting in the loss of the craft.

Page 4: CS115 Class 15: Testing

4

Page 5: CS115 Class 15: Testing

5

More BSOD Embarrassments

Page 6: CS115 Class 15: Testing

6

Page 7: CS115 Class 15: Testing

7

Economic Impact

• NIST study– On CNN.com - April 27, 2003

http://www.nist.gov/director/prog-ofc/report02-3.pdf

Page 8: CS115 Class 15: Testing

8

Basic Definitions

• Error-mistake or deviation from correct value

• Failure-unacceptable system behavior

• Defect/fault-flaw in any part of system that may cause a failure

Page 9: CS115 Class 15: Testing

9

Testing Objective

• Find defects/faults– Find them effectively

• as many as possible

– Find them efficiently • as many as possible given testing time

Page 10: CS115 Class 15: Testing

10

Testing scope

• Unit • Integration• System• Acceptance• Alpha/Beta

Page 11: CS115 Class 15: Testing

11

Test approach

• White box• Black box

Page 12: CS115 Class 15: Testing

12

Software Development Today

Programmer

Tester

Decision Maker

Why do we have this structure?

Page 13: CS115 Class 15: Testing

13

Typical Scenario (1)

Programmer

Tester

Decision Maker

“I’m done.

“It doesn’t #$%&

compile!”

“OK, calm down. We’ll

slip the schedule. Try

again.”

Page 14: CS115 Class 15: Testing

14

Typical Scenario (2)

Programmer

Tester

Decision Maker

“I’m done.

“It doesn’t install!”

“Now remember, we’re all in this together. Try

again.”

Page 15: CS115 Class 15: Testing

15

Typical Scenario (3)

Programmer

Tester

Decision Maker

“I’m done.

“It does the wrong

thing in half the tests.”

“Let’s have a meeting to

straighten out the spec.”

“No, half of your tests

are wrong!”

Page 16: CS115 Class 15: Testing

16

Typical Scenario (4)

Programmer

Tester

Decision Maker

“I’m done.

“It still fails some tests we agreed

on.”

“Try again, but please hurry up!”

Page 17: CS115 Class 15: Testing

17

Typical Scenario (5)

Programmer

Tester

Decision Maker

“I’m done.

“Yes, it’s done!”

“Oops, the world has changed. Here’s the new

spec.”

Page 18: CS115 Class 15: Testing

18

Software Development Today

Programmer

Tester

Decision Maker

Why do we have this structure?

Page 19: CS115 Class 15: Testing

19

Standard Testing Questions

• How shall we generate/select test cases?

• Did this test execution succeed or fail?

• How do we know when to stop testing?

Page 20: CS115 Class 15: Testing

20

Summary

• Testing is hard– If done manually, also very expensive and boring

• Use inspections!– Will save more time in testing and debugging

• A number of techniques can make testing effective– Randomized testing– Exhaustive testing on small examples– Regression testing with nightly build

Page 21: CS115 Class 15: Testing

21

Back to Design

• Testing has a profound impact on design– Because some designs are easier to test

• Design software so it can be tested!

Page 22: CS115 Class 15: Testing

22

Principles of Testability

• Avoid unpredictable results– No unnecessary non-deterministic behavior

• Design in self-checking– At appropriate places have system check its own work

• Asserts

– May require adding some redundancy to the code

• Have a test interface• Minimize interactions between features

– Number of interactions can easily grow huge– Rich breeding ground for bugs

Page 23: CS115 Class 15: Testing

23

JUnit

Test automation

Page 24: CS115 Class 15: Testing

24

Benefits of JUnit

• Free - http://www.junit.org• Automatic check of expected vs. actual• Simple to use – quick to write and run• Tests are written in Java

Page 25: CS115 Class 15: Testing

25

Design of JUnit

• Built around Command pattern – each method under test is represented by a

separate test method

Page 26: CS115 Class 15: Testing

26

JUnit Sample Unit Test write tests for all methods in a

class

import junit.framework.TestCase

public class ShoppingCartTest extends TestCase { protected void setUp() {…} protected void tearDown() {…} public void testEmpty() {…} public void testAddItem() {…}}

Page 27: CS115 Class 15: Testing

27

How to create a test suite

import junit.framework.TestSuite; public class EcommerceTestSuite {

public static Test suite() { TestSuite suite = new TestSuite(); suite.addTestSuite(ShoppingCartTest.class);suite.addTest(CreditCardTestSuite.suite());

} /** * Runs the test suite using the textual runner. */ public static void main(String[] args) {

junit.textui.TestRunner.run(suite());}

}

Page 28: CS115 Class 15: Testing

28

A GUI for running the test suite

java junit.swingui.TestRunner EcommerceTestSuite

Page 29: CS115 Class 15: Testing

29

Initialization called before every test case method

protected void setUp() {cart = new ShoppingCart(); book1 = new Product("Pragmatic Unit Testing",

29.95); cart.addItem(book1);

}

Page 30: CS115 Class 15: Testing

30

Clean up called after every test case method

protected void tearDown() { // set to null any references to big objects

Page 31: CS115 Class 15: Testing

31

Tests the empty method

public void testEmpty() { cart.empty(); assertEquals(0, cart.getItemCount());

}

Page 32: CS115 Class 15: Testing

32

Tests addItem method

public void testAddItem() { Product book2 = new Product("Pragmatic Project

Automation", 29.95); cart.addItem(book2); double expectedBalance = book1.getPrice() +

book2.getPrice();assertEquals(expectedBalance, cart.getBalance()); assertEquals(2, cart.getItemCount());

}

Page 33: CS115 Class 15: Testing

33

Output of JUnit

>java junit.textui.TestRunner TestServer1) testAddItem AssertionFailedError: expected:<59.90> but was:<59.00 >

Page 34: CS115 Class 15: Testing

34

JUnit assertXXX

• assertEquals(x,y) // .equals, or values• assertFalse(boolean expr)• assertNotNull(obj ref)• assertNotSame(x, y) // using ==• assertNull()• assertSame()• assertTrue()• fail()

public void testPassNullsToConstructor(){ try{ Server s = new Server(null, null); fail(“Expected IllegalArgumentException”); } catch (IllegalArgumentException expected){}}

Page 35: CS115 Class 15: Testing

35

JUnit Sequence

• New test case for every test<foo> method• For each test<foo> method

– create new object• no sharing of instance variables between tests

– setUp()– test<foo>– tearDown()

Page 36: CS115 Class 15: Testing

36

JUnit Practices

• One function check per test method– usually one assert per test

• sometimes more

– failure aborts entire method

• Many test<foo> per TestCase• Can group multiple TestCase into a TestSuite

Page 37: CS115 Class 15: Testing

37

JUnit Recommendations

• Add JUnit tests in your code– run regularly (eg before SVN commit)

• Benefits:– catch errors early

• easier to debug, easier to fix

– reduce cost of integration testing– reduce risk

• of large, untested code base• of late slips in schedule (no time to recover)• increase confidence, reduce stress

Page 38: CS115 Class 15: Testing

38

More details ...

• JUnit home: www.junit.org

• Tutorial http://clarkware.com/articles/JUnitPrimer.html

• Also (thanks Stan)– http://www.soe.ucsc.edu/classes/cmps115/Spring06/SECURE/9904c.htm

Page 39: CS115 Class 15: Testing

39

“Best Practices” Session

• By now, you have now developed some expertise in your particular specialization– (tester, coder, documenter, facilitator)

• Group by specialization to discuss – what knowledge you’ve gained– what works, what doesn’t– tips to help other teams

• Short (5min) presentation using doc camera