Download Unit Testing: Understanding the Concept, Phases, and Benefits and more Study notes Computer Science in PDF only on Docsity! 1 1 Unit Testing 2 Testing in General Purpose – ? Phases – ? 3 What Is Unit Testing? Testing is ? A unit is ? Unit testing is ? 4 Why Unit Testing? 5 How to Do Unit Testing Bottom up construction – Starts with classes that don’t depend on others. – Continue testing building on already tested classes. Benefits – Avoid having to write (test) stubs. – When testing a module, rely only on tested modules. 6 Coupling Coupling: the degree to which some component interacts with other components 2 7 Question How does low coupling help testing? How does high coupling hurt it? 8 Program to Test public final class IMath { /** * Returns an integer approximation * to the square root of x. */ public static int isqrt(int x) { int guess = 1; while (guess * guess < x) { guess++; } return guess; } } 9 Conventional Testing /** A class to test the class IMath. */ public class IMathTestNoJUnit { /** Runs the tests. */ public static void main(String[] args) { printTestResult(0); printTestResult(1); printTestResult(2); printTestResult(3); printTestResult(4); printTestResult(7); printTestResult(9); printTestResult(100); } private static void printTestResult(int arg) { System.out.print(“isqrt(“ + arg + “) ==> “); System.out.println(IMath.isqrt(arg)); } } 10 Conventional Test Output Isqrt(0) ==> 1 Isqrt(1) ==> 1 Isqrt(2) ==> 2 Isqrt(3) ==> 2 Isqrt(4) ==> 2 Isqrt(7) ==> 3 Isqrt(9) ==> 3 Isqrt(100) ==> 10 What does this say about the code? Is it right? What’s the problem with this kind of test output? 11 Solution? Automatic verification by testing program 12 Some Terminology Definition – A test data (or case) for a method M is a pair of (o, args), where • o is not null and M can be sent to o, • args is a tuple of arguments that can be passed to M. – A test data, (o, args), for M succeeds iff o.M(args) behaves as expected. – A test data, (o, args), for M fails iff it does not behave as expected. Question – Why should o not be null? – If M has a bug that is revealed by a test data, does that test data for M succeed or fail?