20
Nitish Upreti @nitish

Software testing

Embed Size (px)

DESCRIPTION

Lecture on Unit Testing for CMPSC 221

Citation preview

Page 1: Software testing

Nitish Upreti@nitish

Page 2: Software testing

Why do we need Software Testing?

CMPSC221

Page 3: Software testing

CMPSC221

Page 4: Software testing

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

Page 5: Software testing

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

Page 6: Software testing

CMPSC221

Page 7: Software testing

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

Page 8: Software testing

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

Page 9: Software testing

Some Theory - Bird Eye View

• Levels of Software Testing :– Acceptance Testing– System Testing– Integration Testing– Module Testing – Unit Testing

CMPSC221

Page 10: Software testing

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

Page 11: Software testing

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

Page 12: Software testing

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

Page 13: Software testing

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)); }

Page 14: Software testing

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

Page 15: Software testing

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

Page 16: Software testing

Live Demo!

CMPSC221

Page 17: Software testing

TDD/BDD Anyone?

CMPSC221

Page 18: Software testing

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

Page 19: Software testing

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

Page 20: Software testing

Thank You!

CMPSC221