object oriented design and programming java n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Object-Oriented Design and Programming (Java) PowerPoint Presentation
Download Presentation
Object-Oriented Design and Programming (Java)

Loading in 2 Seconds...

play fullscreen
1 / 74

Object-Oriented Design and Programming (Java) - PowerPoint PPT Presentation


  • 131 Views
  • Uploaded on

Object-Oriented Design and Programming (Java). Topics Covered Today. Overview of Software Testing Software Testing Methods Software Test Types 2.1 Implementing Classes 2.1.4 Unit Testing. Overview of Software Testing. Purpose of Software Testing Role of Testers Truths and Realities.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

Object-Oriented Design and Programming (Java)


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
topics covered today
Topics Covered Today
  • Overview of Software Testing
  • Software Testing Methods
  • Software Test Types
  • 2.1 Implementing Classes
    • 2.1.4 Unit Testing
overview of software testing
Overview of Software Testing
  • Purpose of Software Testing
  • Role of Testers
  • Truths and Realities
software testing
Software Testing
  • Any activity designed to evaluate an attribute or capability of a program to determine that it meets required standards.
purpose of software testing
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
software quality
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”
role of testers
Role of Testers
  • Find Bugs
  • Break Software
  • Keep Developers Honest
  • Defect Prevention
  • Quality Measurement
  • Drive quality upstream
  • Customer focus

Goal: ensure the quality

truths and realities
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”
truths and realities1
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.
software testing methods
Software Testing Methods
  • Black-box Testing
  • White-box Testing
  • Risk-based Testing
black box testing

Input

Output

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
white box testing

Input

Output

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
risk based testing
Risk-based Testing
  • Used to determine:
    • What to test
    • Test priority
    • Test coverage
risk based testing2
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
software test types
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测试等
test stages
Test Stages

Requirement specification

(用户需求)

Acceptance testing

(验收测试)

System testing

(系统测试)

System specification

(规格定义)

Design (设计)

Integration testing (集成测试)

Code (编码)

Unit testing (单元测试)

bvt build verification test
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
application compatibility test
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
scenario tests
Scenario Tests
  • Tests that simulate customers’ real usage
  • Advantages
    • Customer focused
  • Disadvantages
    • Difficult to simulate
software testing1
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
test planning
Test Planning
  • Why Test Plan
  • What’s in a Test Plan
  • How to write a test plan
benefits of writing test plan
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
what s in a test plan
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
test development
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
test case
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)
bug report bad example
Bug Report – Bad Example

Title: AV occurs.

Description:

An AV occurs when running test 2641.

Repro Steps:

1) Run test 2641

mistakes to avoid
Mistakes to Avoid
  • Incomplete repro steps
  • Lack of details and investigation
  • Missing environment/configuration info
  • Missing expected result
  • Duplicate bugs
good testers
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
topics covered today1
Topics Covered Today
  • Overview of Software Testing
  • Software Testing Methods
  • Software Test Types
  • 2.1 Implementing Classes
    • 2.1.4 Unit Testing
unit testing
Unit Testing
  • scope = individual component
  • Focus: component correctness
  • Responsibility of the developer
  • White-box and black-box techniques
example hello world program
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”
helloworld java code
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();

}

}

create a new class for testing
Create a New Class for Testing

public 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);

} }

}

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

public 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; }

} }

class testbankaccount
Class TestBankAccount

import 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);

class testbankaccount1
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);

class testbankaccount2
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");

} }

a slightly better testhelloworld program
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);
  • }
  • }
a slightly better testhelloworld program1
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)");

}

} }

advantage
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.
junit
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
why junit
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
testhelloworld program written with junit 3 x
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);

}

}

testhelloworld program written with junit 3 x1
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);

}

}

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 x2
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.

testhelloworld program written with junit 3 x3
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”),

testhelloworld program written with junit 3 x4
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.

testhelloworld program written with junit 3 x5
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)

failed test
Failed Test

Hello World. 多了一个句点符号

junit methods
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
junit test runner sequence
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

things to notice
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
implementing setup method
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
implementing the teardown method
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)
the structure of a test method
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
test suites
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;

} }

creating a test class in junit
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.
start to use it
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.
the framework of junit1
the Framework of JUnit

Package junit.framework

the framework of junit2
the Framework of JUnit

Package junit.runner

the framework of junit3
the Framework of JUnit

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

the framework of junit4
the Framework of JUnit

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

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

the framework of junit5
the Framework of JUnit

A Test can be run and passed a TestResult.

the framework of junit6
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.

the framework of junit7
the Framework of JUnit

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

the framework of junit8
the Framework of JUnit

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

the framework of junit9
the Framework of JUnit

A test runner is a user interface for launching tests.

BaseTestRunner is the superclass for all test runners.