23
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

  • View
    234

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

1

Software Testing and Quality Assurance

Lecture 23 – JUnit Tutorial

Page 2: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

2

Lecture Outline Get familiar with a software testing tool

(JUnit).

Page 3: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

3

Assert Assert within a test:

Call the method being tested and get the actual result.

assert what the correct result should be with one of the provided assert methods.

These steps can be repeated as many times as necessary.

Page 4: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

4

JUnit assert methods static void assertTrue(boolean test)

static void assertTrue(String message, boolean test)

Throws an AssertionFailedError if the test fails (the optional message is included in the Error)

static void assertFalse(boolean test)static void assertFalse(String message, boolean test)

Throws an AssertionFailedError if the test fails.

assertEquals(expected, actual)assertEquals(String message, expected, actual)

For objects, uses your equals method, if you have defined it properly, as public boolean equals(Object o)--otherwise it uses ==

Page 5: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

5

JUnit assert methods (cont...) assertSame(Object expected, Object actual)

assertSame(String message, Object expected, Object actual)

Asserts that two objects refer to the same object (using ==).

assertNotSame(Object expected, Object actual)

assertNotSame(String message, Object expected, Object actual)

Asserts that two objects do not refer to the same object.

Page 6: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

6

JUnit assert methods (cont...) assertNull(Object object)

assertNull(String message, Object object) Asserts that the object is null.

assertNotNull(Object object)assertNotNull(String message, Object object)

Asserts that the object is not null.

fail()fail(String message)

Causes the test to fail and throw an AssertionFailedError Useful as a result of a complex test, when the other assert

methods aren’t quite what you want.

Page 7: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

7

When to use an assert statement Use assertTrue to document a

condition that you “know” to be true.

Use assertFalse; in code that you “know” cannot be reached (such as a default case in a switch statement).

Page 8: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

8

Example: Counter class We will create and test a trivial “counter” class:

The constructor will create a counter and set it to zero.

The increment method will add one to the counter and return the new value.

The decrement method will subtract one from the counter and return the new value.

We write the test methods before we write the code. Don’t be alarmed if, in this simple example, the JUnit

tests are more code than the class itself.

Page 9: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

9

Example: JUnit tests for Counter class (cont...)

public class CounterTest extends junit.framework.TestCase {

Counter counter1;

public CounterTest() { } // default constructor

protected void setUp() { // creates a (simple) test fixturecounter1 = new Counter();

}

protected void tearDown() { } // no resources to release

public void testIncrement() {assertTrue(counter1.increment() == 1);

assertTrue(counter1.increment() == 2);

}

public void testDecrement() {assertTrue(counter1.decrement() == -1);

}

}

Note that each test begins with a brand new counter

This means you don’t have to worry about the order in which the tests are run

Page 10: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

10

Example: The Counter class itself (cont...)

public class Counter { int count = 0;

public int increment() {return ++count;

}

public int decrement() {return --count;

}

public int getCount() {return count;

} }

Is JUnit testing overkill for this little class?

The Extreme Programming view is: If it isn’t tested, assume it doesn’t work

You are not likely to have many classes this trivial in a real program, so writing JUnit tests for those few trivial classes is no big deal

Often even XP programmers don’t bother writing tests for simple getter methods such as getCount()

We only used assertTrue in this example, but there are additional assert methods

Page 11: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

11

Test suites Obviously you have to test your code to

get it working in the first place: You can do ad hoc testing (running

whatever tests occur to you at the moment).

You can build a test suite (a thorough set of tests that can be run at any time).

Page 12: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

12

Test suites: advantages and disadvantages

Disadvantages of a test suite: It’s a lot of extra programming:

This is true, but use of a good test framework can help quite a bit.

You don’t have time to do all that extra work: False--Experiments repeatedly show that test

suites reduce debugging time more than the amount spent building the test suite.

Advantages of a test suite: Reduces total number of bugs in delivered code.

Page 13: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

13

TestRunners: text

Text: Lightweight, quick quiet. Run from command line.

java StringTest

.......

Time: 0.05Tests run: 7, Failures: 0, Errors: 0

Page 14: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

14

TestRunners: swing Run with java junit.swingui.TestRunner.

Page 15: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

15

Example: point classpublic class Point {

private int x, y;

public Point(int x, int y){ this.x = x; this.y = y;}

public void setX(int x) { this.x = x; }

public void setY(int y) { this.y = y; }

public int getX() { return x; }

public int getY() { return y; }}

Page 16: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

16

Example (cont...): point test without using JUnitpublic class PointTestNoJunit {

public static void main(String [] args) {testSetX(new Point(10, 20), 0);testSetX(new Point(10, 20), 1);testSetX(new Point(10, 20), 30);

}private static void testSetX(Point p, int x) {

System.out.print("Before: " + toString(p) + ";");p.setX(x);System.out.println(" After setX(" + x + "): " +

toString(p));}private static String toString(Point p) {

return "<" + p.getX() + ", " + p.getY() + ">";}

}

Page 17: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

17

Example (cont...): point test without using JUnit —output

Before: <10, 20>; After setX(0): <0, 20>

Before: <10, 20>; After setX(1): <1, 20>

Before: <10, 20>; After setX(30): <10, 20>

Before: <10, 20>; After setY(0): <10, 0>

Before: <10, 20>; After setY(1): <10, 1>

Before: <10, 20>; After setY(30): <10, 30>

Page 18: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

18

Example (cont...): point test using JUnitimport junit.framework.*;/** A JUnit test class to test the class Point. */public class PointTest extends TestCase {

/** Tests constructor. */public void testPoint() {

Point p = new Point(10,20);assertEquals(10, p.getX());assertEquals(20, p.getY());

}/** Tests setX */public void testSetX() {

Point p = new Point(10, 20);p.setX(30);assertEquals(30, p.getX());

assertEquals(20, p.getY());}

//other test methods, e.g., for setY(), getX(), and getY().// cont…

Page 19: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

19

Example (cont...): point test suite using JUnit

/** Returns the test suite for this test class. */public static Test suite() {

return new TestSuite(PointTest.class);}

/** Run the tests. */public static void main(String[] args) {

junit.textui.TestRunner.run(suite());// junit.swingui.TestRunner.run(suite());

}}

Page 20: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

20

Example (cont...): point test using JUnit —outputC:/> javac Point.java PointTest.javaC:/> java PointTest

...F.....Time: 0.016There was 1 failure:testSetX1(PointTest)junit.framework.AssertionFailedError: expected:<30>

but was:<1> at PointTest.testSetX1(PointTest.java:58) … at PointTest.main(PointTest.java:17)

FAILURES!!!Tests run: 8, Failures: 1, Errors: 0

Page 21: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

21

Example (cont...): point test using JUnit —Test Fixturepublic class PointTest extends TestCase {

private Point p; // test fixture variablepublic void setUp() { // initializes text fixture variables

p = new Point(10, 10);}

public void tearDown() { }//clean up text fixture variablespublic void testSetX() { // tests SetX

p.setX(20); assertEquals(20, p.getX());

}

public void testSetY() { // tests SetYp.setY(30); assertEquals(30, p.getY());

}

// template and other test methods here…}

Page 22: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

22

JUnit Testing Tips Code a little, test a little, code a little, test a

little . . . Run your tests as often as possible, at least

as often as you run the compiler. Begin by writing tests for the areas of the

code that you’re the most worried about . . .write tests that have the highest possible return on your testing investment.

Page 23: 1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial

23

JUnit Testing tips When you need to add new functionality to

the system, write the tests first. If you find yourself debugging using

System.out.println(), write a test case instead. When a bug is reported, write a test case to

expose the bug. Don’t deliver code that doesn’t pass all the

tests. Separate production and test code:

But typically in the same packages.