agenda - emory universitycengiz/cs370-pract-softeng... · ± inadequate software testing costs the...

53
Agenda Test-Drive Checkpoint: “OOps, I did it again” due midnight today

Upload: others

Post on 25-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Agenda

Test-Drive Checkpoint: “OOps, I did it again” due midnight today

Mandatory: Set-up meetings with me or James week after breakStand-up for your meeting now!Today:

1 Code design patterns2 Testing intro

Page 2: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Agenda

Test-Drive Checkpoint: “OOps, I did it again” due midnight todayMandatory: Set-up meetings with me or James week after break

Stand-up for your meeting now!Today:

1 Code design patterns2 Testing intro

Page 3: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Agenda

Test-Drive Checkpoint: “OOps, I did it again” due midnight todayMandatory: Set-up meetings with me or James week after breakStand-up for your meeting now!

Today:1 Code design patterns2 Testing intro

Page 4: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Agenda

Test-Drive Checkpoint: “OOps, I did it again” due midnight todayMandatory: Set-up meetings with me or James week after breakStand-up for your meeting now!Today:

1 Code design patterns2 Testing intro

Page 5: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Exit quiz: User interface design, UML, JavaScriptIn UI design, explain concepts of “context sensitivty” and “reducing user’s memory load”.What are advantages and disadvantages of using UML diagrams?How does a JavaScript array differ from a Java array, and what other data structures canit serve as?

Entry survey: Code design patterns and testingWhat is unit versus integration testing?What’s the point of repeating a known pattern than re-inventing the wheel? Argue goodusage scenarios for both.

Page 6: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Design Patterns IEncapsulation, Observer, and Decorator

CS370 Software & Startup Engineering Practicum, Cengiz Günay

(Some slides courtesy of Eugene Agichstein and the Internets. License: CC BY-SA 4.0.)

CS370, Günay (Emory) Design Patterns I Spring 2015 3 / 10

Page 7: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Patterns in Software Engineering

Don’t reinvent the wheel!Re-use existing patterns!

CS370, Günay (Emory) Design Patterns I Spring 2015 4 / 10

Page 8: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Patterns in Software Engineering

Don’t reinvent the wheel!Re-use existing patterns!

CS370, Günay (Emory) Design Patterns I Spring 2015 4 / 10

Page 10: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Example: SimUDuck

Page 11: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this
Page 12: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this
Page 13: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this
Page 14: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this
Page 15: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Recap: Strategy Pattern

Page 16: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Your class hierarchies?

Need some volunteersTell us about your project classes:

1 Uses inheritance for what?2 Would encapsulation be useful?

CS370, Günay (Emory) Design Patterns I Spring 2015 6 / 10

Page 17: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Your class hierarchies?

Need some volunteersTell us about your project classes:

1 Uses inheritance for what?

2 Would encapsulation be useful?

CS370, Günay (Emory) Design Patterns I Spring 2015 6 / 10

Page 18: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Your class hierarchies?

Need some volunteersTell us about your project classes:

1 Uses inheritance for what?2 Would encapsulation be useful?

CS370, Günay (Emory) Design Patterns I Spring 2015 6 / 10

Page 19: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Example of the observer pattern!

Page 20: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Example of the observer pattern!

Page 21: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Observer Pattern

Page 22: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this
Page 23: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Observer Pattern

Page 24: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this
Page 25: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Observer Pattern

Page 26: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Observer Pattern Defined

Page 27: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this
Page 28: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Java Observable

Page 29: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this
Page 30: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Observer pattern in your project?

Can you think of a place that will be useful in your project?

I User notificationI LikesI Button press in games

CS370, Günay (Emory) Design Patterns I Spring 2015 8 / 10

Page 31: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Observer pattern in your project?

Can you think of a place that will be useful in your project?I User notificationI LikesI Button press in games

CS370, Günay (Emory) Design Patterns I Spring 2015 8 / 10

Page 32: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Decorator Pattern

Page 33: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this
Page 34: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this
Page 35: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this
Page 36: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Introduction to Testing

Page 37: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

• "Code without tests is broken by design.“ – J. Kaplan-Moss

• Providing automated tests for your code is a way to repeatedly ensure … that the code you wrote … works as advertised.

• When you first get started, writing tests … sounds like extra work. But simple tests are easy to write and having some tests is better than no tests at all.

2/12/2013 CS 370, Spring 2012 13

“Agile” Dev: Test Early and Often

Page 38: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Testing: The First Bugs

© Ammann & Offutt 14

―It has been just so in all of my

inventions. The first step is an

intuition, and comes with a burst,

then difficulties arise—this thing

gives out and [it is] then that

'Bugs'—as such little faults and

difficulties are called—show

themselves and months of

intense watching, study and

labor are requisite. . .‖ – Thomas

Edison

Hopper’s ―bug‖ (moth stuck in a

relay on an early machine)

Page 39: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Costly Software Failures

© Ammann & Offutt 15

NIST report, ―The Economic Impacts of Inadequate Infrastructure for Software Testing‖ (2002)

– Inadequate software testing costs the US alone between $22 and $59 billion annually

– Better approaches could cut this amount in half

Huge losses due to web application failures– Financial services : $6.5 million per hour (just in USA!)

– Credit card sales applications : $2.4 million per hour (in USA)

In Dec 2006, Amazon.com’s BOGO offer turned into a double discount

2007 : Symantec says that most security vulnerabilities are due to faulty software

Page 40: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Spectacular Software Failures

© Ammann & Offutt 16

Major failures: Ariane 5 explosion, Mars Polar Lander, Intel’s Pentium FDIV bug

Poor testing of safety-critical software can cost lives :

THERAC-25 radiation machine: 3 dead

Mars Polar

Lander crash

site?

THERAC-25 design

Ariane 5:

exception-handling

bug : forced self

destruct on maiden

flight (64-bit to 16-bit

conversion: about

370 million $ lost)

NASA’s Mars lander: September 1999, crashed due to a units integration fault

Toyota brakes : Dozens dead, thousands of crashes

Page 41: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Why write tests?

• Tests increase confidence

• Testable code encourages good design

• Guards code from harm by others

• Instant gratification

• It’s cheaper to find a bug before it’s in Production, than after

Page 42: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Types of Testing

Page 43: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Unit testing

• Unit: smallest testable part of an application

• A function / method or an entire class

• Run code in isolation

• Ensure that the building blocks of your application function in isolation

Page 44: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Integration testing

• Verifies that components of software work together as intended

• Expose defects in the integration between classes

• Don’t interact with external resources

• Use Stubs / Mock objects

• Database, web services, etc.

Page 45: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

System testing

• Actually tests all of the software and external components together

• Ensure that you’ve met the requirements

• Able to interact with external resources

• Database

• Start transaction

• Rollback after each test method

Page 46: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Acceptance testing

• Suite of tests run against completed system

• Typically done by a human being

• Or automated (Selenium, Cucumber, etc.)

• Have requirements been met?

Page 47: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Unit test example

Page 48: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Classic Textbook Example

<?phpnamespace Math;

class Doer{ function add($a, $b) { return $a + $b; }}

Page 49: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Testing it...

<?phprequire_once './bootstrap.php';require_once './sample01.php';

class Sample01_TestCase extends PHPUnit_Framework_TestCase{ private $mathDoer;

public function setUp() { $this->mathDoer = new \Math\Doer(); }

public function testAdd() { $this->assertEquals( 4, $this->mathDoer->add(2, 2)); }}

Page 50: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

And it passes

PHPUnit 3.7.9 by Sebastian Bergmann.

Starting test 'Sample01_TestCase::testAdd'..

Time: 0 seconds, Memory: 6.75Mb

OK (1 test, 1 assertion)

Page 51: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Some other developer thinks 2 + 2 = 5...

<?phpnamespace Math;

class Doer{ function add($a, $b) { // 2 + 2 = 5. I swear. if ($a == 2 && $b == 2) { return 5; }

return $a + $b; }}

Page 52: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

And it fails

PHPUnit 3.7.9 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 6.75Mb

There was 1 failure:

1) Sample02_TestCase::testAddFailed asserting that 5 matches expected 4.

/Users/brian/code/community/talks/emory_2012/code/sample02-test.php:18

FAILURES!Tests: 1, Assertions: 1, Failures: 1.

Page 53: Agenda - Emory Universitycengiz/CS370-pract-softeng... · ± Inadequate software testing costs the US alone between $22 and $59 billion annually ± Better approaches could cut this

Don’t let your spring break!