CS 3773 Software Engineering Basic Concepts of Software Testing Unit Testing.

Slides:



Advertisements
Similar presentations
Unit Testing Australian Development Centre Brisbane, Australia.
Advertisements

1 Unit Testing with JUnit CS 3331 Fall 2009 Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report, 3(7):37-50, Available.
Computer Science 209 Testing With JUnit. Why Test? I don ’ t have time, I ’ ve got a deadline to meet The more pressure I feel, the fewer tests I will.
J-Unit Framework.
CS4723 Lecture 3 Unit Testing. 2 Unit testing  Testing of an basic module of the software  A function, a class, a component  Typical problems revealed.
Approach of Unit testing with the help of JUnit Satish Mishra
Software Engineering Module 1 -Components Teaching unit 3 – Advanced development Ernesto Damiani University of Bozen- Bolzano Lesson 4 – Software Testing.
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
SE 450 Software Processes & Product Metrics Reliability: An Introduction.
JUnit, Revisited 17-Apr-17.
Program Testing Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
Software Testing. “Software and Cathedrals are much the same: First we build them, then we pray!!!” -Sam Redwine, Jr.
Copyright by Scott GrissomCh 1 Software Development Slide 1 Software Development The process of developing large software projects Different Approaches.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
CS4723 Software Validation and Quality Assurance Lecture 02 Overview of Software Testing.
Terms: Test (Case) vs. Test Suite
CS5103 Software Engineering Lecture 13 Software Licenses Software Testing.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
Software Testing. Definition To test a program is to try to make it fail.
© 2012 IBM Corporation Rational Insight | Back to Basis Series Chao Zhang Unit Testing.
CS 501: Software Engineering Fall 1999 Lecture 16 Verification and Validation.
1 Debugging and Testing Overview Defensive Programming The goal is to prevent failures Debugging The goal is to find cause of failures and fix it Testing.
Automated GUI testing How to test an interactive application automatically?
 CS 5380 Software Engineering Chapter 8 Testing.
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
1 CS 501 Spring 2002 CS 501: Software Engineering Lecture 23 Reliability III.
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
16 October Reminder Types of Testing: Purpose  Functional testing  Usability testing  Conformance testing  Performance testing  Acceptance.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
LCG-SPI: SW-Testing LCG AppArea internal review (20/10/03)
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
A tool for test-driven development
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Software Development Problem Analysis and Specification Design Implementation (Coding) Testing, Execution and Debugging Maintenance.
JUnit Don Braffitt Updated: 10-Jun-2011.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 11, Testing.
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
Software Engineering1  Verification: The software should conform to its specification  Validation: The software should do what the user really requires.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Unit 17: SDLC. Systems Development Life Cycle Five Major Phases Plus Documentation throughout Plus Evaluation…
1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that.
Testing Overview Software Reliability Techniques Testing Concepts CEN 4010 Class 24 – 11/17.
Test automation / JUnit Building automatically repeatable test suites.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Thanks to Atif Memon from UMD for disaster examples
Unit Testing with JUnit
Introduction to JUnit CS 4501 / 6501 Software Testing
Hao Zhong Shanghai Jiao Tong University
CS5123 Software Validation and Quality Assurance
Computer Science 209 Testing With JUnit.
Mid-term Exam Account for 20% of the grade 100 points in total
Unit testing C# classes
History, Characteristics and Frameworks
Thanks to Atif Memon from UMD for disaster examples
Introduction to Testing, SUnit and Error Handling
Testing and Test-Driven Development CSC 4700 Software Engineering
Introduction to JUnit CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
Introduction to JUnit IT323 – Software Engineering II
Test Case Test case Describes an input Description and an expected output Description. Test case ID Section 1: Before execution Section 2: After execution.
Thanks to Atif Memon from UMD for disaster examples
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Presentation transcript:

CS 3773 Software Engineering Basic Concepts of Software Testing Unit Testing

2 Why testing? Examples  Mars Climate Orbiter ($165M, 1998)  Sent to Mars to relay signal from the Mars Lander  Smashed to the planet: failing to convert between English measure to metric values  Shooting down of Airbus A300 (290 death, 1988)  US CG-49 shoot down a Airbus A300  Misleading output of the tracking software  THERAC-25 Radiation Therapy (1985)  2 cancer patients at East Texas Cancer center received fatal overdoses  Miss-handling of race condition of the software in the equipment

3 Why testing? Numbers  On average, 1-5 bugs per KLOC (thousand lines of code)  In mature software  More than 10 bugs in prototypes  Windows2000  35MLOC  63K known bugs at the time of release  2 bugs per KLOC  $59.5B loss due to bugs in US 2002 (estimation by NIST)  It is not feasible to remove all bugs  But try to reduce critical bugs

4 Answer is testing, why?  “50% of my employees are testers, and the rest spends 50% of their time testing” ---- Bill Gates, 1995  More reliable than inspection, relatively cheap  Actually in the old days, when testing is expensive, inspection was the major answer  You get what you pay (linear rewards)  Compared to other 3 approaches  Inspection, static checking, formal proof

5 Testing: Concepts  Test case  Test oracle  Test suite  Test script  Test driver  Test result  Test coverage

6 Testing: Concepts  Test case  An execution of the software with a given list of input values  Include:  Input values, sometimes fed in different steps  Expected outputs  Test oracle  The expected outputs of software by feeding in a list of input values  A part of test cases  Hardest problem in auto-testing: test oracle problem

7 Testing: Concepts  Test suite  A collection of test cases  Usually these test cases share similar pre-requisites and configuration  Usually can be run together in sequence  Different test suites for different purposes  Smoke test, Certain platforms, Certain feature, performance, …  Test Script  A script to run a sequence of test cases or a test suite automatically

8 Testing: Concepts  Test Driver  A software framework that can load a collection of test cases or a test suite  It can usually handle the configuration and comparison between expected outputs and actual outputs  Test Coverage  A measurement to evaluate how well the testing is done  The measure can be based on multiple elements  Code  Input combinations  Specifications

9 Granularity of Testing: V-model

10 Granularity of testing  Unit Testing  Test of a single module  Integration Testing  Test the interaction between modules  System Testing  Test the system as a whole, by developers on test cases  Acceptance Testing  Validate the system against user requirements, by customers with no formal test cases  Regression Testing  Test a new version with old test cases

11 Unit testing  Testing of an basic module of the software  A function, a class, a component  Typical problems revealed  Local data structures  Algorithms  Boundary conditions  Error handling

12 An example: w riting test cases without a test framework  Class Definition  Consider a class that handles and manages a Fibonacci array  Initialized the class  Fibonacci f = new Fibonacci ();  Extend to a certain length  f. extend (length)  Provide the number at certain index  f. get(index)  Get Length  f.getLength()  Provide a range of numbers  f. getRange(start, end)

13 An example: writing automatic test cases without a test framework public static void main(String args[]){ Fibonacci f = new Fibonacci(); if(f.getLenght != 0){ System.err.print(“wrong results …”); } f.extend(50); if(f.getLenght != 50){ System.err.print(“wrong results …”); } int num = f.get(50); if(num != ){ System.err.print(“wrong results …”); } int[] range = f.getRange(40, 50) if(!ValueEquals(range, …)){ System.err.print(“wrong results …”); } … } Not Enough! What happened if an exception is thrown

14 An example: writing automatic test cases without a test framework public static void main(String args[]){ Fibonacci f = new Fibonacci(); if(f.getLenght != 0){ System.err.print(“wrong results …”); } f.extend(50); if(f.getLenght != 50){ System.err.print(“wrong results …”); } int num = f.get(50); if(num != ){ System.err.print(“wrong results …”); } int[] range = f.getRange(40, 50) if(!ValueEquals(range, …)){ System.err.print(“wrong results …”); } … } Potential test interference!

15 Common Operations  Comparison between actual results and expected results  Report error messages  Catch Exceptions  Re-initialize the class to be tested

16 Unit test framework  xUnit  Created by Kent Beck in 1989  This is the same guy we mentioned in XP, design patterns  The first one was sUnit (for smalltalk)  Junit  The most popular xUnit framework  There are about 70 xUnit frameworks for corresponding languages  Never in the annals of software engineering was so much owed by so many to so few lines of code Martin Fowler

17 State machine of JUnit Initialization Of Test Class setUp try{ testMethod } catch {…} tearDown Why? Try and Catch?

18 Writing a Test Case public class FibonacciTest extends TestCase { protected Fibonacci fTest; protected static int groudtruth[] = {0, 1,..., }; public void setUp(){ fTest = new Fibonacci(); public testInitialize(){ assertEquals(“Length of fTest after initialization”, 0, fTest.getLength()); public testExtend(){ fTest.extend(50); assertEquals(“…”, 50, fTest.getLength()); public testGet(){ fTest.extend(50); assertEquals(“…”, groudtruth[49], fTest.get(50)); } … }

19 Benefits of using test framework  Clear structure of testing  Each test method for one feature  Common initialization to setup  Assertion methods  You can always define new assertion methods  Reduced Interference  Try catch to run all test methods  Always do re-initialization in setup

20 Demo  Writing a JUnit testcase for a class in your project  Run it as a JUnit test case

21 Tips of writing test cases  Bottom Up  If you know that obj.m1 -> obj.m2, test obj.m2 first  Why?  You may not understand the error in m1 if you don’t know whether m2 is working or not  Externalize data  Externalize expected results to files and load them at the test case constructor  Why?  Easier to maintain if you want to reuse them in several places

22 Tips of writing test cases  Use development Database  Do not use real database for testing  Why? Obvious  Do enough logging in the code during testing  Why?  Help debugging  Testing for exceptions  How? public testException(){ try{ do(invalid); fail(); }catch(xxxException e){} }

23 Assertions public void testCapacity() { // a test method …. assertTrue(fFull.size() == 100+size); //assertion } If assertion fails: Assertion failed: myTest.java:150 (expected true but was false) Not so good! Try: assertEquals(100+size, fFull.size()); //expected value first Assertion failed: myTest.java:150 (expected 102 but was 103) Better! Try: assertEquals(“list length”, 100+size, fFull.size()); Assertion failed: myTest.java:150 (list length expected 102 but was 103)

24 Assertions  Extend TestCase and write your own assertions  AssertStringContains, AssertArrayEquals, …  Use fail(String) to fail a test with certain message  Feel free to change the fixture  Each test will re-setup public void testRemoveAll() { fFull.removeAllElements(); fEmpty.removeAllElements(); assertTrue(fFull.isEmpty()); assertTrue(fEmpty.isEmpty()); }

Tear down  Consider the following test code void setUp() { File f = open(“foo”); File b = open(“bar”); } void testAAA() { use f and b } void testBBB(){ use f and b } Problems? void setUp() { File f = open(“foo”); File b = open(“bar”); } void testAAA() { try { use f and b } finally { clean&close f, b } void testBBB() { try { use f and b } finally { clean&close f, b } Better?

26 Tear down  Consider the following test code void setUp() { File f = open(“foo”); File b = open(“bar”); } void testAAA() { use f and b } void testBBB(){ use f and b } void tearDown{ clean&close f, b } Problems? void setUp() { File f = open(“foo”); File b = open(“bar”); } … void tearDown{ try{ clean&close f }catch{ … } the same for b }

27 Tear down  Be careful about tear down  If tear down is not complete, a test failure may affect the following test cases  Recover the changes done to global data that are not well handled by the setup  Database, files, network, global variables  Clean resources  Caution of exceptions in tear down itself