agenda - emory universitycengiz/cs370-pract-softeng... · ± inadequate software testing costs the...
TRANSCRIPT
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
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
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
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
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.
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
Patterns in Software Engineering
Don’t reinvent the wheel!Re-use existing patterns!
CS370, Günay (Emory) Design Patterns I Spring 2015 4 / 10
Patterns in Software Engineering
Don’t reinvent the wheel!Re-use existing patterns!
CS370, Günay (Emory) Design Patterns I Spring 2015 4 / 10
Click on me!
Example: SimUDuck
Recap: Strategy Pattern
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
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
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
Example of the observer pattern!
Example of the observer pattern!
Observer Pattern
Observer Pattern
Observer Pattern
Observer Pattern Defined
Java Observable
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
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
Decorator Pattern
Introduction to Testing
• "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
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)
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
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
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
Types of Testing
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
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.
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
Acceptance testing
• Suite of tests run against completed system
• Typically done by a human being
• Or automated (Selenium, Cucumber, etc.)
• Have requirements been met?
Unit test example
Classic Textbook Example
<?phpnamespace Math;
class Doer{ function add($a, $b) { return $a + $b; }}
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)); }}
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)
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; }}
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.
Don’t let your spring break!