Nitish Upreti
@nitish
Why do we need Software
Testing?

CMPSC221
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 :
–
–
–
–
–

CMPSC221

Acceptance Testing
System Testing
Integration Testing
Module Testing
Unit 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
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.
@Test
@Test
public void testMultiply() {{
public void testMultiply()
// MyClass is tested
// MyClass is tested
MyClass tester ==new MyClass();
MyClass tester new MyClass();
// check if multiply(10,5) returns 50
// check if multiply(10,5) returns 50
assertEquals("10 x 5 must be 50", 50, tester.multiply(10, 5));
assertEquals("10 x 5 must be 50", 50, tester.multiply(10, 5));
}}

CMPSC221
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
Live Demo!

CMPSC221
TDD/BDD Anyone?

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
Thank You!

CMPSC221

Software testing

  • 1.
  • 2.
    Why do weneed Software Testing? CMPSC221
  • 3.
  • 4.
    Therac-25 Incident • Radiationtherapy 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
  • 5.
    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
  • 6.
  • 7.
    Keep in mind… “Programtesting 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
  • 8.
    Perceived Benefits • BugDiscovery. • 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
  • 9.
    Some Theory -Bird Eye View • Levels of Software Testing : – – – – – CMPSC221 Acceptance Testing System Testing Integration Testing Module Testing Unit Testing
  • 10.
    Quick Overview… • AcceptanceTesting : 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
  • 11.
    Unit Testing • AUnit 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
  • 12.
    How do IUnit 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
  • 13.
    JUnit • Test frameworkwhich uses annotations to identify methods that specify a test. @Test @Test public void testMultiply() {{ public void testMultiply() // MyClass is tested // MyClass is tested MyClass tester ==new MyClass(); MyClass tester new MyClass(); // check if multiply(10,5) returns 50 // check if multiply(10,5) returns 50 assertEquals("10 x 5 must be 50", 50, tester.multiply(10, 5)); assertEquals("10 x 5 must be 50", 50, tester.multiply(10, 5)); }} CMPSC221
  • 14.
    Annotations @Test public voidmethod() @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
  • 15.
    Test Methods • • • • • • • • • fail(String) assertTrue([message], booleancondition) 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
  • 16.
  • 17.
  • 18.
    What is thisTDD/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
  • 19.
    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
  • 20.

Editor's Notes

  • #14 To write a test with JUnit, you annotate a method with the @org.junit.Test annotation and use a method provided by JUnit to check the expected result of the code execution versus the actual result.
  • #15 @Test public void method() The @Test annotation identifies a method as a test method. @Test (expected = Exception.class) Fails, if the method does not throw the named exception. @Test(timeout=100) Fails, if the method takes longer than 100 milliseconds. @Before public void method() This method is executed before each test. It is used to can prepare the test environment (e.g. read input data, initialize the class). @After public void method() This method is executed after each test. It is used to cleanup the test environment (e.g. delete temporary data, restore defaults). It can also save memory by cleaning up expensive memory structures. @BeforeClass public static void method() This method is executed once, before the start of all tests. It is used to perform time intensive activities, for example to connect to a database. Methods annotated with this annotation need to be defined as static to work with JUnit. @AfterClass public static void method() This method is executed once, after all tests have been finished. It is used to perform clean-up activities, for example to disconnect from a database. Methods annotated with this annotation need to be defined as static to work with JUnit. @Ignore Ignores the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included.
  • #16 fail(String) - Let the method fail. Might be used to check that a certain part of the code is not reached. Or to have a failing test before the test code is implemented. The String parameter is optional. assertTrue([message], boolean condition) - Checks that the boolean condition is true. assertFalse([message], boolean condition) - Checks that the boolean condition is false. assertEquals([String message], expected, actual) - Tests that two values are the same. Note: for arrays the reference is checked not the content of the arrays. assertEquals([String message], expected, actual, tolerance) - Test that float or double values match. The tolerance is the number of decimals which must be the same. assertNull([message], object) - Checks that the object is null. assertNotNull([message], object) - Checks that the object is not null. assertSame([String], expected, actual) - Checks that both variables refer to the same object. assertNotSame([String], expected, actual) - Checks that both variables refer to different objects.