Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing...

74
Object-Oriented Design and Programming (Java)

Transcript of Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing...

Page 1: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

Object-Oriented Design and Programming (Java)

Page 2: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

2

Topics Covered Today

• Overview of Software Testing• Software Testing Methods• Software Test Types• 2.1 Implementing Classes

– 2.1.4 Unit Testing

Page 3: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

3

Overview of Software Testing

• Purpose of Software Testing • Role of Testers • Truths and Realities

Page 4: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

4

Software Testing

• Any activity designed to evaluate an attribute or capability of a program to determine that it meets required standards.

Page 5: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

5

Purpose of Software Testing

• Measure Quality– Validation 确认 : Prove it works (Do the right thing)– Verification 证明 : Do it right

• Provide information– To development and management– For risk evaluation

• Watching the process not just the product

Page 6: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

6

Software Quality

• Doing the right things right at the right time

• Conformance to applicable standards– Customer focus

• Features vs. Flaws

– Engineering focus• Maintainability, sustainability 可维护 , testability, etc.

• Quality is defined as conformance一致 to requirements, not as “goodness” or “elegance”

• Quality also means “meet customer needs”

MC SYSTEM
可持续、可维护、可测试
Page 7: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

7

Role of Testers

• Find Bugs • Break Software • Keep Developers Honest • Defect Prevention • Quality Measurement • Drive quality upstream • Customer focus

Goal: ensure the quality

Page 8: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

8

Truths and Realities

• The earlier a bug is fixed, the less costly it is.– Before code review– By code review– By testing– By Beta testers– After product is released

• Prevention vs. Detection– “The mere act of designing a test is a powerful bug

preventer”

Page 9: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

9

Truths and Realities

• Thorough Testing vs. Risk-based testing– Thorough Testing: testing for everything– Risk-based Testing

• Identify and analyze risks to enable informed and calculated decisions

• Look for the best use of test resource• Risks can be categorized by severity and likelihood.

MC SYSTEM
严重性
Page 10: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

10

Software Testing Methods

• Black-box Testing • White-box Testing • Risk-based Testing

Page 11: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

11

Black-box Testing

• Doesn’t require explicit knowledge of the internal structure of the system

• Primarily focuses on functional requirements– User-perspective

– Validates the spec

Input Output

Page 12: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

12

White-box testing

• Requires knowledge of the internal structure

• Primarily focuses on code coverage– e.g. write tests to “cover” internal paths– Good for testing individual functions– Developer’s tests

Input Output

Page 13: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

13

Risk-based Testing

• Used to determine:– What to test– Test priority– Test coverage

Page 14: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

14

Risk-based Testing

Page 15: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

15

Risk-based Testing

• Two basic factors for determining risk:– Impact to the customer– Probability of customer experiencing issues

• Other factors affecting risk assessment:– History, complexity, usability, dependencies, new

features or technologies, modifications, etc.

• Value of Risk Assessment– Prioritize work– Scope testing effort

MC SYSTEM
优先级
MC SYSTEM
影响范围
Page 16: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

16

Software Test Types

• 基于是否需要执行被测试软件– Dynamic Test 动态测试、 Static Test 静态测试;

• 基于是否关注软件结构与算法– Black Box Test 、 White Box Test

• 基于测试的不同阶段– Unit testing 单元测试、 Integration testing 集成测试、

System testing 系统测试、 Acceptance testing 验收测试;• 其它

– Smoke Test 冒烟测试、 Regression Test 回归测试、 Functional Test 功能测试、 Stress Test 负载测试 ( 压力测试 ) 、 Performance Test 性能测试、 Accessibility Test 易用性测试、 Install/Uninstall Test 安装与反安装测试、 Security Test 安全性测试、 Application Compatibility Test 兼容性测试、 Alpha 测试、 Beta 测试等

Page 17: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

17

Test Stages

System specification

( 规格定义 )

Design ( 设计 )

Code ( 编码 )

System testing

( 系统测试 )

Integration testing ( 集成测试 )

Unit testing ( 单元测试 )

Requirement specification

( 用户需求 )

Acceptance testing

( 验收测试 )

Page 18: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

18

BVT(Build Verification Test)

• An automated test suite run on each new build to validate the integrity of the build and basic functionality before the new build is released for general testing. – Also known as build acceptance test (构建验证测试

) .

• Advantages– Save testing time– Validate basic functionality

• Disadvantages– Minimum level of test coverage

Page 19: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

19

Application Compatibility Test

• Test 3rd-party application behavior and integration with new software

• Advantages– Exercise application interaction– Customer focused

• Disadvantages– Testers not usually experts in all application software– Impossible to test all applications or interactions

Page 20: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

20

Scenario Tests

• Tests that simulate customers’ real usage

• Advantages– Customer focused

• Disadvantages– Difficult to simulate

Page 21: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

21

Software Testing

• Important and expensive activity– Not unusual to spend 30-40% of total project effort on

testing– For critical systems (e.g. flight control): cost can be

several times the cost of all other activities combined

Page 22: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

22

Test Planning

• Why Test Plan

• What’s in a Test Plan

• How to write a test plan

Page 23: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

23

Benefits of Writing Test Plan

• Improves test coverage and efficiency

• Improves communication among peers, teams and with management– Feedback

• Helps manage testing

• Improves individual accountability

MC SYSTEM
同事
MC SYSTEM
责任
Page 24: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

24

What’s in a Test Plan

• Who will be doing the work

• What will be tested

• What will NOT be tested

• Test environments (hardware/software)

• Test strategy

• Test dependency

• Test groups/categories

• Risks/Open Issues

Page 25: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

25

Test Development

• Characteristics of Good Tests– Reasonable probability of catching an error

– Not redundant

– Be focused

• Development Techniques– Boundary Value Analysis

– Error Guessing/Exploratory Testing

• Test Case Documentation

MC SYSTEM
必要的
MC SYSTEM
多余的,Not redundant必须的
MC SYSTEM
探索、搜索
Page 26: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

26

Test Case

• A test case(测试用例 ) is a document that describes an input, action, or event and an expected response, to determine if a feature of an application is working correctly

• Test case: specifies– Inputs + pre-test state of the software– Expected results (outputs and state)

Page 27: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

27

Bug Report – Bad Example

Title: AV occurs.

Description:

An AV occurs when running test 2641.

Repro Steps:

1) Run test 2641

Page 28: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

28

Mistakes to Avoid

• Incomplete repro steps

• Lack of details and investigation

• Missing environment/configuration info

• Missing expected result

• Duplicate bugs

Page 29: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

29

Good Testers

• Good knowledge of computer science• Knowledge of one or more programming languages• Debugging skills• Attention to details• Ability to prioritize work• Team player• Good communication skills

MC SYSTEM
有重要观、大局观能力
MC SYSTEM
很好的项目参与
Page 30: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

30

Topics Covered Today

• Overview of Software Testing• Software Testing Methods• Software Test Types• 2.1 Implementing Classes

– 2.1.4 Unit Testing

Page 31: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

31

Unit Testing

• scope = individual component

• Focus: component correctness

• Responsibility of the developer

• White-box and black-box techniques

Page 32: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

32

Example: Hello World Program

• Use main method to test class “HelloWorld”

• Testing plan:– Instance of HelloWorld.java should not be null– Output should be “Hello World”

Page 33: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

33

HelloWorld.java code

public class HelloWorld {

public String sayHello() {

System.out.println("Hello World");

return “Hello World”;

}

public static void main( String[] args ) {

HelloWorld world = new HelloWorld();

world.sayHello();

}

}

Page 34: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

34

Create a New Class for Testingpublic class HelloWorld {

String sayHello() {

return "Hello World!";

} }

public class TestHelloWorld {

public static void main(String[] args) {

HelloWorld world = new HelloWorld();

String result = world.sayHello();

if (result.equals("Hello World!") == false) {

System.out.println("Bad result: " + result);

} }

}

Page 35: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

35

Example 2

• Another example to create a new class for testing (see 2.1.4 Unit Testing).– BankAccount.java– TestBankAccount.java

Page 36: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

36

Class BankAccountpublic class BankAccount { private double balance; public BankAccount() { balance = 0.0; } public double getBalance() { return balance; } public boolean deposit(double amount) { if (amount > 0) { balance += amount; return true; } else { return false; } } public boolean withdraw(double amount) { if (amount > 0 && balance >= amount) { balance -= amount; return true; } else { return false; } } }

Page 37: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

37

Class TestBankAccountimport java.io.*; public class TestBankAccount { private static PrintWriter stdOut = new PrintWriter(System.out, true); private static PrintWriter stdErr = new PrintWriter(System.err, true);

public static void assertTrue(String message, boolean condition) { if (! condition) { stdErr.print("** Test failure "); stdErr.println(message); } } public static void main(String[] args) { boolean result; // Testing constructor and accessor BankAccount accountOne = new BankAccount(); assertTrue("1: testing method getBalance()", accountOne.getBalance() == 0);

Page 38: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

38

Class TestBankAccount //Testing method deposit BankAccount accountTwo = new BankAccount(); result = accountTwo.deposit(100); assertTrue("2: testing method deposit", result); assertTrue("3: testing method deposit", accountTwo.getBalance() == 100);

result = accountTwo.deposit(50); assertTrue("4: testing method deposit", result); assertTrue("5: testing method deposit", accountTwo.getBalance() == 150);

result = accountTwo.deposit(0); assertTrue("6: testing method deposit", ! result); assertTrue("7: testing method deposit", accountTwo.getBalance() == 150);

result = accountTwo.deposit(-25); assertTrue("8: testing method deposit", !result); assertTrue("9: testing method deposit", accountTwo.getBalance() == 150);

Page 39: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

39

Class TestBankAccount //Testing method withdraw BankAccount accountThree = new BankAccount(); accountThree.deposit(100);

result = accountThree.withdraw(60); assertTrue("10: testing method withdraw", result); assertTrue("11: testing method withdraw", accountThree.getBalance() == 40); result = accountThree.withdraw(50); assertTrue("12: testing method withdraw", ! result); assertTrue("13: testing method withdraw", accountThree.getBalance() == 40); result = accountThree.withdraw(0); assertTrue("14: testing method withdraw", ! result); assertTrue("15: testing method withdraw", accountThree.getBalance() == 40); result = accountThree.withdraw(-10); assertTrue("16: testing method withdraw", ! result); assertTrue("17: testing method withdraw", accountThree.getBalance() == 40); stdOut.println("done"); } }

Page 40: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

40

A (slightly) better TestHelloWorld program

public class TestHelloWorld {

private int nbErrors = 0;

public void testSayHello() {

HelloWorld world = new HelloWorld();

String result = world.sayHello();

if (result.equals("Hello World!") == false) {

throw new RuntimeException("Bad result: " + result);

}

}

Page 41: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

41

A (slightly) better TestHelloWorld program

public static void main(String[] args) {

TestHelloWorld test = new TestHelloWorld();

try { test.testSayHello(); }

catch (Throwable e) {

test.nbErrors++;

e.printStackTrace(); }

if (test.nbErrors > 0) {

throw new RuntimeException

("There were " + test.nbErrors + " error(s)");

}

} }

Page 42: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

42

Advantage

• Move the test into its own method. – It’s now easier to focus on what the test does. – You can also add more methods with more unit tests

later, without making the main block harder to maintain.

• Change the main block to print a stack trace when an error occurs and then, if there are any errors, to throw a summary exception at the end.

Page 43: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

43

JUnit

• JUnit is a framework for performing unit testing on programs– A unit test is a test of a single class

• A test case( 测试用例 ) is a single test of a single method• A test suit( 测试系列 ) is a collection of test cases

• Available as a stand-alone application and build into Eclipse

• Framework executes the test cases and records the results– Displays results in a GUI

Page 44: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

44

Why JUnit

• Allow you to write code faster while increasing quality• Elegantly simple • Check their own results and provide immediate feedback • Tests is inexpensive • Increase the stability of software • Developer tests • Written in Java • Free • Gives proper understanding of unit testing

MC SYSTEM
简单好看
Page 45: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

45

New->Other->Java->JUnit->JUnit Test Case

Page 46: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

46

Create New JUnit Test Case

Page 47: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

47

TestHelloWorld program written with JUnit 3.x

import junit.framework.TestCase;

public class TestHelloWorld extends TestCase {

public void testSayHello() {

HelloWorld world = new HelloWorld();

String result = world.sayHello();

assertEquals("Hello World!", result);

}

}

Page 48: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

48

import junit.framework.TestCase;

public class TestHelloWorld extends TestCase {

public void testSayHello() {

HelloWorld world = new HelloWorld();

String result = world.sayHello();

assertEquals("Hello World!", result);

}

}1. You start by extending the test class from the standard Junit junit.framework.TestCase. This base class includes the framework code that JUnit needs to automatically run the tests.

TestHelloWorld program written with JUnit 3.x

Page 49: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

49

TestHelloWorld program written with JUnit 3.x

import junit.framework.TestCase;

public class TestHelloWorld extends TestCase {

public void testSayHello() {

HelloWorld world = new HelloWorld();

String result = world.sayHello();

assertEquals("Hello World!", result);

}

} 2. You make sure that the method name follows the pattern testXXX(). Following the testXXX naming convention is not strictly required, but it is strongly encouraged as a best practice.

Page 50: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

50

TestHelloWorld program written with JUnit 3.x

import junit.framework.TestCase;

public class TestHelloWorld extends TestCase {

public void testSayHello() {

HelloWorld world = new HelloWorld();

String result = world.sayHello();

assertEquals("Hello World!", result);

}

}3. You start the test by creating an instance of the HelloWorld class (the “object under test”),

Page 51: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

51

TestHelloWorld program written with JUnit 3.x

import junit.framework.TestCase;

public class TestHelloWorld extends TestCase {

public void testSayHello() {

HelloWorld world = new HelloWorld();

String result = world.sayHello();

assertEquals("Hello World!", result);

}

} 4. You execute the test by calling the method to test, no parameter is passing in this case.

Page 52: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

52

TestHelloWorld program written with JUnit 3.x

import junit.framework.TestCase;

public class TestHelloWorld extends TestCase {

public void testSayHello() {

HelloWorld world = new HelloWorld();

String result = world.sayHello();

assertEquals("Hello World", result);

}

}5. To check the result of the test, you call an assertEquals method, which inherited from the base TestCase.

static void assertEquals([String message],expected,actual)

Page 53: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

53

JUnit for Eclipse

Page 54: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

54

Successful Test

Page 55: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

55

Failed Test

Hello World. 多了一个句点符号

Page 56: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

56

JUnit Methods

• assertEquals(x, y) – Test passes if x and y are equal– x and y can be primitives or any type with an appropriate equals

method– Three argument versions exist for floating point numbers

• assertFalse(b) – Test passes if boolean value b is false• assertTrue(b) – Test passes if boolean value b is true• assertNull(o) – Test passes if object o is null• assertNotNull(o) – Test passes if object o is not null• assertSame(ox, oy) – Test passes if ox and oy refer to the

same object • assertNotSame(ox, oy) – Test passes if ox and oy do not

refer to the same object

Page 57: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

57

JUnit Test Runner Sequence

• Test runner( 测试运行程序 ) is given a list of test classes• For each test class

• Test runner produces a report

Create an instance of the test class

For each test*() method

Run setUp() method // code run before every test

Run test method steps and checks

If a check fails, an assertion is thrown and the testmethod fails

Run tearDown() method

Page 58: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

58

Things to Notice

• The setUp() method ensures you entered the test method with a virgin set of objects.– Officially called “test fixtures( 测试固件 )”.

– It is just a some code you want run before every test

• The tearDown() method frees memory, prevents results of one test from affecting the next.– It will be run after every test case

• Only the first failure in a test method is reported– Don’t do too much in a single test

Page 59: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

59

Implementing setUp() method

• Override setUp() to initialize the variables, and objects

• Since setUp() is your code, you can modify it any way you like (such as creating new objects in it)

• Reduces the duplication of code

Page 60: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

60

Implementing the tearDown() method

• In most cases, the tearDown() method doesn’t need to do anything– The next time you run setUp(), your objects will be

replaced, and the old objects will be available for garbage collection

– Like the finally clause in a try-catch-finally statement, tearDown() is where you would release system resources (such as streams)

Page 61: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

61

the Structure of a Test Method

• A test method doesn’t return a result

• If the tests run correctly, a test method does nothing

• If a test fails, it throws an AssertionFailedError

• The JUnit framework catches the error and deals with it; you don’t have to do anything

Page 62: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

62

Test suites

• In practice, you want to run a group of related tests .To do so, running test suites.import junit.framework.Test;

import junit.framework.TestSuite;

public class AllTests {

public static Test suite() {

TestSuite suite = new TestSuite("Test for default package");

suite.addTestSuite(TestStringUtil.class);

suite.addTestSuite(TestHelloWorld.class)

return suite;

} }

Page 63: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

63

Creating a test class in JUnit

• Define a subclass of TestCase • Override the setUp() method to initialize object(s) under

test. • Override the tearDown() method to release object(s) under

test. • Define one or more public testXXX() methods that exercise

the object(s) under test and assert expected results. • Define a static suite() factory method that creates a

TestSuite containing all the testXXX() methods of the TestCase.

• Optionally define a main() method that runs the TestCase in batch mode.

Page 64: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

64

Start to Use it

• Download the latest version of JUnit from http://download.sourceforge.net/junit/

• Installation – unzip the junit.zip file – add junit.jar to the CLASSPATH.

Page 65: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

65

the Framework of JUnit

Page 66: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

66

the Framework of JUnit

Package junit.framework

Page 67: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

67

the Framework of JUnit

Package junit.runner

Page 68: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

68

the Framework of JUnit

A set of assert methods. Messages are only displayed when an assert fails.

Page 69: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

69

the Framework of JUnit

A TestResult collects any errors or failures that occur during a test.

A TestFailure collects a failed test together with the caught exception.

Page 70: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

70

the Framework of JUnit

A Test can be run and passed a TestResult.

Page 71: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

71

the Framework of JUnit

A TestListener is apprised of events that occur during a test, including when the test begins and ends, along with any errors or failures.

Page 72: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

72

the Framework of JUnit

A TestCase defines an environment (or fixture) that can be used to run multiple tests.

Page 73: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

73

the Framework of JUnit

A TestSuite runs a collection of test cases, which may include other test suites. It is a composite of Tests.

Page 74: Object-Oriented Design and Programming (Java). 2 Topics Covered Today Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing.

74

the Framework of JUnit

A test runner is a user interface for launching tests.

BaseTestRunner is the superclass for all test runners.