Upload
nitish-upreti
View
1.540
Download
0
Embed Size (px)
DESCRIPTION
Lecture on Unit Testing for CMPSC 221
Citation preview
Nitish Upreti@nitish
CMPSC221
Therac-25 Incident
• Radiation therapy machine built during the 1980s.
• Six incidents where massive overdose of Radiation was given to the patients.
• Caused lethal Radiation Burns and death.
• University of Washington investigation showed lack of Software Testing as one of the key issues. ( Google the Report)
CMPSC221
Software is Critical
• Validation of software behavior is of utmost importance.
• Shipping a good quality product is necessary.
• Reliability and Stability of the System needs to be at its best.
• Extras: Software system should deliver good performance.
CMPSC221
CMPSC221
Keep in mind…
“Program testing can be a very effective way to show the presence of bugs, but never to show their absence.”
- Edsger W. Dijkstra
Hence Software Testing is a typical “trade off”.CMPSC221
Perceived Benefits
• Bug Discovery.• Bug Prevention : Tests as a safety net.• Flexibility in change / Ease of
Maintenance.• Better Quality / Customer Satisfaction.• Confidence in your codebase.• Tests serving as good Documentation.• Your personal takeaways.
CMPSC221
Some Theory - Bird Eye View
• Levels of Software Testing :– Acceptance Testing– System Testing– Integration Testing– Module Testing – Unit Testing
CMPSC221
Quick Overview…
• Acceptance Testing : Do we meet the final requirements?
• System Testing : Does the system work as a whole?
• Integration Testing : Do different modules work well together?
• Modular Testing : Testing a module ( collection of related Units)
CMPSC221
Unit Testing
• A Unit is a ‘smallest testable part’ of an application.
• Mostly a job of developers.• Show the individual pieces are correct.• Find problems early.• Simplify Integration.• Better design with TDD/BDD (covered
ahead)
CMPSC221
How do I Unit Test ?
• You could do it manually.• Alternatively you could use many
FOSS testing frameworks which will make your lives much easier.
• JUnit is among one of the most powerful framework.
• Fun and easy to work with. Integrates well with Eclipse.
CMPSC221
JUnit
• Test framework which uses annotations to identify methods that specify a test.
CMPSC221
@Testpublic void testMultiply() { // MyClass is tested MyClass tester = new MyClass(); // check if multiply(10,5) returns 50 assertEquals("10 x 5 must be 50", 50, tester.multiply(10, 5)); }
@Testpublic void testMultiply() { // MyClass is tested MyClass tester = new MyClass(); // check if multiply(10,5) returns 50 assertEquals("10 x 5 must be 50", 50, tester.multiply(10, 5)); }
Annotations
@Test public void method()@Test (expected = Exception.class) @Test (timeout=100)@Before public void method()@After public void method()@BeforeClass public static void method()@AfterClass public static void method() @Ignore
CMPSC221
Test Methods
• fail(String)• assertTrue([message], boolean condition)• assertFalse([message], boolean condition).• assertEquals([String message], expected,
actual)• assertEquals([String message], expected,
actual, tolerance)• assertNull([message], object)• assertNotNull([message], object)• assertSame([String], expected, actual) • assertNotSame([String], expected, actual)
CMPSC221
What is this TDD/BDD Buzz?
• TDD : Test Driven Development Practice– Failing Tests that defines a desired
improvement.– Writing minimum code to make it work.– Refactor !
• BDD : Behavior Driven Development– How does the software behave?– Keep into account customer’s vocabulary.– BDD is TDD done right!
CMPSC221
Five Common Excuses
• Is it worth spending all the extra time/energy/money writing tests?
• I am a SDE not an SDET !• Tests in my scenario are hard to write.• I have no idea how to write tests.• I test my code in Production. ;)
CMPSC221