white box testing - free university of bozen-bolzanorusso/ap/whiteboxtestingscaffoldingjunit.pdf ·...

15
24/03/15 1 White box testing Advanced Programming 24/03/15 1 Barbara Russo White-box testing White-box testing is a verification technique software engineers can use to examine if their code works as expected 24/03/15 Barbara Russo 2 Types of WBT Unit testing (we focus on unit testing in the following) Regression testing Integration testing 24/03/15 Barbara Russo 3

Upload: others

Post on 22-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

1  

White box testing

Advanced Programming

24/03/15 1 Barbara Russo

White-box testing

•  White-box testing is a verification technique software engineers can use to examine if their code works as expected

24/03/15 Barbara Russo 2

Types of WBT

•  Unit testing (we focus on unit testing in the following)

•  Regression testing

•  Integration testing

24/03/15 Barbara Russo 3

Page 2: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

2  

WBT with unit testing

•  Each time you write a code module, you should write test cases for it –  A possible exception to this recommendation is the accessor

methods (i.e., getters and setters) of your projects

•  You should concentrate your testing effort on code that could easily be broken –  Generally, accessor methods will be written error-free

24/03/15 Barbara Russo 4

Scaffolding •  Scaffolding is method for unit testing

•  It uses –  Drivers

–  Stubs

–  Harnesses

–  Oracles

24/03/15 Barbara Russo 5

Driver

•  Driver: is a software module used to invoke a module under test and, often, provide test inputs, control and monitor execution, and report test results –  line of code that calls a method and passes that method a

value

24/03/15 Barbara Russo 6

Page 3: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

3  

Example of driver movePlayer(player1, 2);

•  call a method movePlayer and passes player1 and moves it two spaces

•  The drivers would likely to be called from main method

•  A white-box test case would execute this driver line of code and check Player.getPosition() to make sure the player is now on the expected cell on the chess board

24/03/15 Barbara Russo 7

Stub

•  A stub is a computer program statement substituting for the body of a software module that is or will be defined elsewhere or

•  A dummy component or object used to simulate the behaviour of a real component until that component has been developed

24/03/15 Barbara Russo 8

Examples of stub

Simply:

public void movePlayer(Player aPlayer, int diceValue) {

}

or

public void movePlayer(Player aPlayer, int diceValue) {

player=aPlayer;

player.setPosition(1);

} 24/03/15 Barbara Russo 9

Page 4: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

4  

WBT with drivers and stubs

•  Developing stubs allows programmers to call a method in the code being developed, even if the method does not yet have the desired behaviour

•  Stubs can be “filled in” later to form the actual method

24/03/15 Barbara Russo 10

Oracle

•  An oracle is a piece of code that compares the output of a driver with the a predicted output

•  In JUnit these are assert methods:

assertEquals(predicted output, output of a driver)

24/03/15 Barbara Russo 11

Harness

•  It is the environment in which to execute the tests

•  Typically are frameworks

•  Example: JUnit library for eclipse

24/03/15 Barbara Russo 12

Page 5: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

5  

24/03/15 Barbara Russo 13

public void testCenterLine() { Formatter f = new Formatter(); f.setLineWidth(10); assertEquals(" word ", f.center("word")); } public void testOddCenterLine(){ Formatter f = new Formatter(); f.setLineWidth(10); assertEquals( " hello ", f.center("hello")); }

public String center(String line) { return “a”; }

!

What are drivers, stubs, and oracles?

24/03/15 Barbara Russo 14

public void testCenterLine() { Formatter f = new Formatter(); f.setLineWidth(10); assertEquals(" word ", f.center("word")); } public void testOddCenterLine(){ Formatter f = new Formatter(); f.setLineWidth(10); assertEquals( " hello ", f.center("hello")); }

public String center(String line) { return “a”; }

!

What are drivers, stubs, and oracles?

Barbara Russo

TESTING WITH JUNIT

4/2/14 15

Page 6: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

6  

Installing JUnit

•  Download from http://junit.org the jar file

•  Unzip or import the jar file

•  Add to classpath of your project –  Project à Properties à Java Build Path

–  Libraries tab

–  Add library

16

Installing JUnit

Right click on the project folder in package explorer –  BuildPath à add Libraries tab

–  Or

–  BuildPath àin the library tab select add library

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

Page 7: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

7  

TestCase in JUnit

•  Any class that contains test methods should subclass the TestCase class

•  testXXX() methods are defined in this class

•  When you want to check the expected and actual test results, you invoke assert() and pass a boolean expression which will indicate if the test case is passed or not

19

TestCase in JUnit

•  Fixtures:

•  When a test class contains multiple testXXX() methods, you can use the setup() and tearDown() methods to initialize and release any common objects under test

20

Writing a Test Case (1/2) A.  Import the Junit harness:

import junit.framework.*;

B.  Define a subclass of TestCase C.  Override the setUp() method to initialize object(s)

under test (optional) D.  Override the tearDown() method to release any

permanent resources you allocated in setUp() (optional)

21

Page 8: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

8  

Writing a Test Case (2/2)

D.  Define one or more testXxx() methods that exercise the objects under test

E.  Define a suite() method that creates a TestSuite containing all the testXxxx() methods of a TestCase

F.  Define a main() method that runs the TestCase

22

Let’s see the JUnit code

•  Main classes –  junit.framework.TestCase

–  junit.framework.TestSuite

–  junit.textui.TestRunner

–  junit.ui.TestRunner junit.ui.LoadingTestRunner

4/2/14

Let’s see the JUnit code •  Steps:

–  Subclass TestCase –  Write test cases, methods that start with “test” –  setup() - initialize variables –  teardown() - release resources, reset –  use a TestRunner to run tests –  Example in the shell

java junit.ui.LoadingTestRunner TestClass

org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...); or

java org.junit.runner.JUnitCore TestClass1 [...other test classes...]

•  TestRunner expects the name of a TestCase class as argument.

24

Page 9: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

9  

Let’s see the JUnit code

25

TestSuite

•  TestCase instances can be composed into TestSuite hierarchies that automatically invoke all the testXxxx() methods defined in each TestCase instance

•  A TestSuite is a composite of: –  Other tests

–  TestCase instances (using addTest() )

–  Other TestSuite instances (using addTest() )

26

Organizing Tests

• Create test cases in the same or a lateral package as the code under test –  Has all the application level classes and test cases for those

components

• For each Java package, define a TestSuite class that contains all the test for verifying code in the package

27

Page 10: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

10  

Example the class import java.util.*; public class BrowsableList { private Vector items; public BrowsableList() { items = new Vector(); } public void add(Browsable element) { items.add(element); }

28

Example the class public Browsable first() throws NoSuchElementException{

return (Browsable) items.firstElement();

}

public Browsable removeFirst() throws NoSuchElementException {

try {

return (Browsable) items.remove(0);

} catch (ArrayIndexOutOfBoundsException e) {

throw new NoSuchElementException("no elem!");

}

}

public boolean isEmpty() {return items.isEmpty();}

}

29

Example the test class import junit.framework.*;

import junit.textui.*; public class BrowsableListTest extends TestCase {

public static void main(String[] args) {

// run the test from the main of JUnitCore in the package runner

org.junit.runner.JUnitCore.main(BrowsableList.class.getName());

}

private BrowsableList items;

private TestItem alpha;

private TestItem beta;

public void setUp() {

items = new BrowsableList();

alpha = new TestItem("alpha");

beta = new TestItem("beta");

} 30

Page 11: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

11  

Example the test class public void tearDown() { }

public BrowsableListTest() { super(""); }

public BrowsableListTest(String name) {

super(name);

}

public void testIsEmpty() {

assert(items.isEmpty());

}

public void testAddOneElement() {

items.add(alpha);

Browsable first = items.first();

TestItem expected = new TestItem("alpha");

assertEquals(first, expected);

assert(!items.isEmpty());

}

31

Example the test class public void testAddTwoElements() {

items.add(alpha);

items.add(beta);

Browsable first = items.removeFirst();

TestItem expectedAlpha=new TestItem("alpha");

Browsable second = items.removeFirst();

TestItem expectedBeta = new TestItem("beta");

assertEquals(first, expectedAlpha);

assertEquals(second, expectedBeta);

assert(items.isEmpty());

}

public static Test suite() {

return new TestSuite(BrowsableListTest.class);

}

}

32

Simple Test Suite package junit.samples; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /* Some simple tests. */ public class SimpleTest extends TestCase { protected int fValue1; protected int fValue2; @Override protected void setUp() { fValue1= 2; fValue2= 3; } public static Test suite() {

/* the dynamic way */ return new TestSuite(SimpleTest.class); }

Page 12: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

12  

Simple Test Suite

24/03/15 Barbara Russo 34

public void testAdd() { double result= fValue1 + fValue2; // forced failure result == 5 assertTrue(result == 6); } public void testDivideByZero() { int zero= 0; int result= 8/zero; result++; } public void testEquals() { assertEquals(12, 12); assertEquals(12L, 12L); assertEquals(new Long(12), new Long(12)); assertEquals("Size", 12, 13); assertEquals("Capacity", 12.0, 11.99, 0.0); } public static void main (String[] args) { junit.textui.TestRunner.run(suite()); } }

Setting up Composite Test Suites package junit.samples; import junit.framework.Test; import junit.framework.TestSuite; /* TestSuite that runs all the sample tests */ public class AllTests { public static void main (String[] args) { junit.textui.TestRunner.run(suite()); } public static Test suite( ) { TestSuite suite= new TestSuite("All JUnit Tests"); suite.addTest(SimpleTest.suite()); suite.addTest(new TestSuite(junit.samples.money.MoneyTest.class)); suite.addTest(junit.tests.AllTests.suite()); return suite; } }

Runnig a test with annotation

•  When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into Junit

24/03/15 Barbara Russo 36

Page 13: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

13  

The test suite class

24/03/15 Barbara Russo 37

Public class AllTest{ import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses(ATest.class, BTest.class, CTest.class) public class ABCSuite { } }

Create the test source folder

24/03/15 Barbara Russo 38

The JUnit Build Path

24/03/15 Barbara Russo 39

Page 14: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

14  

Selecting the class and the method

24/03/15 Barbara Russo 40

Creating the test suite

24/03/15 Barbara Russo 41

Run the test

24/03/15 Barbara Russo 42

Page 15: White box testing - Free University of Bozen-Bolzanorusso/AP/WhiteBoxTestingScaffoldingJUnit.pdf · Let’s see the JUnit code • Main classes – junit.framework.TestCase – junit.framework.TestSuite

24/03/15  

15  

Test Output

24/03/15 Barbara Russo 43

JUnit Testing tips •  If you find yourself debugging using

System.out.println(), write a test case instead –if possible ☺

• When a bug is reported, write a test case to expose the bug

• Don’t deliver code that doesn’t pass all the tests • Write test cases to ensure method, statement, condition

coverage –if possible

44