References: Teach Yourself Object-Oriented Programming in 21 Days by A.Sintes, 1 Testing Stragegies Test case a set of inputs and expected outputs looks at specific piece of functionality to determine if system behaves as expected failed test = system bug every test case must pass before continuing use black box and white box testing 2 Black Box Testing Tests that system functions as expected When testing individual classes, b.b. testing is based on functional requirements of the class When testing whole system, b.b. testing is based on use cases e.g. if a method adds two numbers, test verifies if output is/is not the correct sum 3 1
White Box Testing Tests are based on the method s implementation 100% code coverage is aimed for Tests every branch of the code e.g. if method divides two numbers, with an error branch for divide by 0, a test case must be devised that executes the error condition. 4 5 Unit Testing examines only one feature at a time examines a single class in isolation sends a message to an object and verifies that the expected result is returned each class should have a corresponding unit test should be done throughout development can be white box and black box 6 2
Integration Testing tests that objects interact properly can be based on white box and black box testing needed for each important interaction in the system 7 System Testing verifies the whole system performs as described by use cases should also test things that are not described by use cases to verify that unexpected conditions are handled gracefully includes stress and performance tests -- done in environment that matches production environment 8 System Testing Related to acceptance testing (tests run by the customer on the delivered software) One test can touch many different objects and subsystems All tests must pass before leaving an iteration 9 3
Regression Testing Examines changes to the system that have already been tested and validated If a change is made, the code and any code that was dependent on it is retested Reruns the unit, integration and system tests 10 11 Unit Testing Testing should be done continuously during development Should write a unit test for each class Unit test examines the lowest-level building block of an OO program: an object Can have any number of test cases Each test case checks one feature of the object 12 4
Example Unit Test Public class SavingsAccountTest { public static void main ( String[] args ) { SavingsAccountTest sat = new SavingsAccountTest(); sat.test_applyinginterest(); One test case public void test_applyinginterest() { SavingsAccount acct = new SavingsAccount( 10000.00,.05 ); acct.addinterest(); print_getbalanceresult( acct.getbalance(), 10500.00, 1 ); private void print_getbalanceresult ( double actual, double expected, int test ) { if (actual == expected ) { //passed System.out.println( PASS: test# +test+ interest applied ok ); else { //failed System.out.println( FAIL: test# +test+ interest applied wrong ); System.out.println( value returned: + actual ); System.out.println( expected value: + expected ); 13 Why Do Unit Testing? Unit tests avoid manual validations that are usually error prone and always time consuming help detect errors at lowest level of development help you know when you are done writing a class -- i.e., when all unit tests pass help you design classes when you write the test case before you write the class help you to refactor code (instant feedback on changes 14 Frameworks Framework: a reusable domain model contains all classes common to a domain of problems and serves as basis for a specific application in the domain you extend the classes in the framework and provide your own problem-specific classes to create an application Easier to use a testing framework 15 5
Testing Framework Provides a skeleton you can reuse for writing and executing unit tests Contains base classes for writing unit tests, built-in support for test automation and utilities for interpreting and reporting output Junit is a well-known testing framework for testing Java classes 16 References: Teach Yourself Object-Oriented Programming in 21 Days by A.Sintes, Junit.org Some slides by Quddus Chong 17 Testing with JUnit The JUnit tool is available from www.junit.org The XP programming philosophy is: program incrementally, test-first, and integrate continuously. Tests are generally written method-bymethod. A test should be created whenever the programmer perceives a risk in the code. 18 6
Testing with JUnit Key benefits of automated testing: You can run the tests over and over again Have a framework that facilitates testing Better confidence in quality of code By testing first, the design becomes more focused on implementing behavior (less wasteful code) 19 How to do it (The Test/Code cycle) Write one test Compile the test it should fail to compile, because you haven t implemented the code that the test calls. Implement just enough to compile. Run the test and see it fail. Implement just enough to make the test pass. Run the test and see it pass. Refactor for clarity and remove duplication. Repeat from top. 20 JUnit Has classes for writing unit tests, for validating output and for running test cases in a GUI or command-line environment junit.framework.testcase is base class for defining unit tests To write unit tests, write a class that inherits from TestCase, overrides some methods, and provides its own test case methods 21 7
What s inside a TestCase? TestCase TestCase constructor() IndividualTest() IndividualTest() IndividualTest() TestSuite import static org.junit.assert.*; import org.junit.before; import org.junit.test; public class TutorialTest { @Before protected void setup() throws Exception { super.setup(); @Test public void testsayhello() { HelloWorld world = new HelloWorld(); asserttrue("world is created", world!=null ); assertequals("hello World", world.sayhello() ); 22 JUnit Testing Unit test = test of a single class Design test cases during implementation Run tests after every implementation change When you find a bug, add a test case that catches it 23 JUnit 4.0 Test class name = tested class name + Test Test methods start with test import junit.framework.*; @Test public class DayTest extends TestCase { public void testadd() {... public void testdaysbetween() {...... 24 8
JUnit 4.0 Each test case ends with assertion Test framework catches assertion failures @Test public void testadd() { Day d1 = new Day(1970, 1, 1); int n = 1000; Day d2 = d1.adddays(n); assert d2.daysfrom(d1) == n; 25 JUnit The name of any test case method must begin with test The method must be preceded by @Test A test suite is a mechanism for running multiple test cases 26 Installing/Using JUnit Download.zip file (latest version is 3.7) and unzip. Make sure that junit.jar is on your classpath. JUnit is built in to Eclipse To run JUnit from Eclipse: Right click on source file Choose New JUnit Test Case If junit.jar is not on build path, add it If you choose setup, then you can declare objects once; otherwise, you must declare an object locally to test 27 9
Test Suite @Test public static Test suite () { TestSuite suite= new TestSuite(); suite.addtest(new TutorialTest ("testsayhello")); return suite; 28 10