Testing, Debugging, and Verification



Similar documents
Approach of Unit testing with the help of JUnit

JUnit. Introduction to Unit Testing in Java

Introduction to Automated Testing

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Unit Testing. and. JUnit

Unit Testing & JUnit

Tools for Integration Testing

Chapter 8 Software Testing

Unit Testing JUnit and Clover

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Java Unit testing with JUnit 4.x in Eclipse

JUnit Automated Software Testing Framework. Jeff Offutt. SWE 437 George Mason University Thanks in part to Aynur Abdurazik. What is JUnit?

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

CS 451 Software Engineering Winter 2009

Going Interactive: Combining Ad-Hoc and Regression Testing

Testing Tools Content (Manual with Selenium) Levels of Testing

Basic Testing Concepts and Terminology

Unit Testing with zunit

The V-model. Validation and Verification. Inspections [24.3] Testing overview [8, 15.2] - system testing. How much V&V is enough?

National University of Ireland, Maynooth MAYNOOTH, CO. KILDARE, IRELAND. Testing Guidelines for Student Projects

APPROACHES TO SOFTWARE TESTING PROGRAM VERIFICATION AND VALIDATION

VHDL Test Bench Tutorial

Tutorial IV: Unit Test

Outline. 1 Denitions. 2 Principles. 4 Implementation and Evaluation. 5 Debugging. 6 References

Agile.NET Development Test-driven Development using NUnit

Using JUnit in SAP NetWeaver Developer Studio

Automation using Selenium

TESTING FRAMEWORKS. Gayatri Ghanakota

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Testing and Inspecting to Ensure High Quality

Java Access to Oracle CRM On Demand. By: Joerg Wallmueller Melbourne, Australia

Improved Software Testing Using McCabe IQ Coverage Analysis

How To Test In Bluej

Software Engineering I: Software Technology WS 2008/09. Integration Testing and System Testing

Software testing. Objectives

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

LAB 6: Code Generation with Visual Paradigm for UML and JDBC Integration

To reduce or not to reduce, that is the question

UML-based Test Generation and Execution

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

TESTING WITH JUNIT. Lab 3 : Testing

AP Computer Science Java Subset


Chapter 11: Integrationand System Testing

Software Construction

Unit-testing with JML

Announcement. SOFT1902 Software Development Tools. Today s Lecture. Version Control. Multiple iterations. What is Version Control

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

A Practical Guide to implementing Agile QA process on Scrum Projects

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Module 10. Coding and Testing. Version 2 CSE IIT, Kharagpur

Unit Testing and JUnit

Installing Java (Windows) and Writing your First Program

Bringing Value to the Organization with Performance Testing

Object-Oriented Software Engineering THE TOOLS OF THE TRADE CHAPTER 5. Stephen R. Schach 5.1 Stepwise Refinement.

The goal with this tutorial is to show how to implement and use the Selenium testing framework.

Integrated Error-Detection Techniques: Find More Bugs in Java Applications

Rake Task Management Essentials

Software Testing, Mythology & Methodologies

Metrics in Software Test Planning and Test Design Processes

Software Engineering. Top-Down Design. Bottom-Up Design. Software Process. Top-Down vs. Bottom-Up 13/02/2012

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Benefits of Test Automation for Agile Testing

Writing Self-testing Java Classes with SelfTest

Basic Unix/Linux 1. Software Testing Interview Prep

Reduce Medical Device Compliance Costs with Best Practices.

Introduction to Eclipse

STUDY AND ANALYSIS OF AUTOMATION TESTING TECHNIQUES

Effective unit testing with JUnit

Introduction to Programming System Design. CSCI 455x (4 Units)

Testing. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies. CHAPTER AUTHORS Michael Atmadja Zhang Shuai Richard

Slides prepared by : Farzana Rahman TESTING WITH JUNIT IN ECLIPSE

Testing Methodology Assignment 1 Unit testing using JUnit

Tutorial 7 Unit Test and Web service deployment

Java course - IAG0040. Unit testing & Agile Software Development

Experimental Comparison of Concolic and Random Testing for Java Card Applets

Appium mobile test automation

Table of Contents. LESSON: The JUnit Test Tool...1. Subjects...2. Testing What JUnit Provides...4. JUnit Concepts...5

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

the first thing that comes to mind when you think about unit testing? If you re a Java developer, it s probably JUnit, since the

White Papers: Unit Testing. Unit Testing

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Chapter 13: Program Development and Programming Languages

Levels of Software Testing. Functional Testing

Chapter 5 Names, Bindings, Type Checking, and Scopes

Software Engineering. How does software fail? Terminology CS / COE 1530

+ Introduction to JUnit. IT323 Software Engineering II By: Mashael Al-Duwais

The Integration of Secure Programming Education into the IDE

Software Life Cycle. Management of what to do in what order

The «include» and «extend» Relationships in Use Case Models

An Overview of Java. overview-1

RUP. Development Process. Iterative Process (spiral) Waterfall Development Process. Agile Development Process. Well-known development processes

Transcription:

Testing, Debugging, and Verification Testing, Part II Moa Johansson 10 November 2014 TDV: Testing /GU 141110 1 / 42

Admin Make sure you are registered for the course. Otherwise your marks cannot be recorded. Even if you are repeating the course, only taking exercises or exam. If in doubt, contact the student center to double check. PhD students excluded (but drop me an email so I know who you are) If anyone have a clashing exam: contact student center/exam administration. Please sign up to the News Google-group. Use news group to look for labpartners. TDV: Testing /GU 141110 2 / 42

Exercise sessions First exercise session this Wednesday please bring laptops install relevant tools before topic: testing install JUnit beforehand (version JUnit4 upwards) TDV: Testing /GU 141110 3 / 42

Overview of this Lecture Testing levels and the V-model Focus on Unit Testing Terminology: Test case, test set, test suit, oracle Introduction to JUnit: a framework for rapid unit testing Extreme Testing using JUnit TDV: Testing /GU 141110 4 / 42

Recall: The V-Model Requirements Analysis Customer needs User's/client's needs met? Acceptance Testing Architectural Design Choose components, connections Assembled system meets spec? System Testing Subsystem Design Structure, behaviour of subsystem Does components work together? Integration Testing Code! Implementation Test individual methods, classes Unit Testing (many variants!) TDV: Testing /GU 141110 5 / 42

Examples: System, Unit and Integration Testing Pentium Bug (-94) Wrong result on floating point divisions. Omission of 5 entries in 1066 entry table. Rarely happened (on system level). Easy catch in unit test. Arianne 5 Rocket (-96) Exploded 5 secs. after takeoff. Used guidance system from Arianne 4. Flight trajectory was different. Lacked system testing. TDV: Testing /GU 141110 6 / 42

Examples: System, Unit and Integration Testing Mars Lander (-99) Crashed on landing. Mismatch in units: imperial vs. metric. Lacking in integration testing. TDV: Testing /GU 141110 7 / 42

Discussion: Testing Levels of a System for Printing Paychecks Name: Person nr: Hours: Print Paycheck Staff Database Given name, looks up address, position. Payroll System calculates: salaries, taxes, bonus Print Paycheck Formats and prints paycheck Think of examples of: System Tests Integration Tests Unit Tests TDV: Testing /GU 141110 8 / 42

Some examples of Tests System Test Enter data in GUI, does it print the correct paycheck, formatted as expected? Integration Tests, e.g. Payroll asks database for staff data, are values what s expected? Maybe there are special characters (unexpected!). Are paychecks formatted correctly for different kinds of printers? Unit Tests, e.g. Does payroll system compute correct tax-rate, bonus etc? Does the Print Paycheck button react when clicked?... TDV: Testing /GU 141110 9 / 42

Regression Testing Orthogonal to the above testing levels: Regression Testing Testing that is done after changes in the software. Purpose: gain confidence that the change(s) did not cause (new) failures. Standard part of the maintenance phase of software development. E.g. Suppose Payroll subsystem is updated. Need to re-run tests (which ones?). TDV: Testing /GU 141110 10 / 42

Unit Testing Rest of testing part of the course: focusing largely on unit testing recall: unit testing = procedure testing = (in oo) method testing major issues in unit testing: 1. unit test cases ( test cases in short) 2. order in which to test and integrate units start with 1. TDV: Testing /GU 141110 11 / 42

Test Cases The science of testing is largely the science of test cases. TDV: Testing /GU 141110 12 / 42

What does a test case consists of? (to be refined later) Test case Initialisation (of class instance and input arguments) Call to the method under test. Decision (oracle) whether the test succeeds or fails two first parts seem enough for a test case, but test oracle is vital for automated evaluation of test TDV: Testing /GU 141110 13 / 42

Success vs. Failure of Tests What does it mean for a test to succed?... or fail? TDV: Testing /GU 141110 14 / 42

Test Cases, more precise Params, Input State Result, Final State I: (P, S in ) Method (R, S out ) m Oracle O Oracle O Yes! No! More formally... A test case is a tuple m, I, O of method m, input I, and oracle O, where m is the method under test I is a tuple P, S in of call parameters P and initial state S in O(R, S out ) {pass, fail is a function on return value R and final state S out, telling whether they comply with correct behaviour TDV: Testing /GU 141110 15 / 42

Test Set A test set TS m for a (Java) method m consists of n test cases: TS m = { m, I 1, O 1,..., m, I n, O n In general, O i is specific for each test case! TDV: Testing /GU 141110 16 / 42

Test Suite A test suite for methods m 1,..., m k is a union of corresponding test sets: TS m 1... TS mk TDV: Testing /GU 141110 17 / 42

Automated and Repeatable Testing Basic idea: write code that performs the tests. By using a tool you can automatically run a large collection of tests The testing code can be integrated into the actual code, thus stored in an organised way side-effect: documentation After debugging, the tests are rerun to check if failure is gone Whenever code is extended, all old test cases can be rerun to check that nothing is broken (regression testing) TDV: Testing /GU 141110 18 / 42

Automated and Repeatable Testing (cont d) We will use JUnit for writing and running the test cases. JUnit: small tool offering some functionality repeatedly needed when writing test cases a way to annotate methods as being test cases a way to run and evaluate test cases automatically in a batch TDV: Testing /GU 141110 19 / 42

JUnit Java testing framework to write and run automated tests JUnit features include: Assertions for testing expected results Annotations to designate test cases Sharing of common test data Graphical and textual test runners JUnit is widely used in industry JUnit used from command line or within an IDE (e.g., Eclipse) (Demo) TDV: Testing /GU 141110 20 / 42

Basic JUnit usage public c l a s s Ex1 { public s t a t i c int find_min ( int [] a) { int x, i; x = a [0]; for (i = 1; i < a. length ;i ++) { i f (a[i] < x) x = a[i]; return x;... TDV: Testing /GU 141110 21 / 42

Basic JUnit usage continued from prev page... public s t a t i c int [] insert ( int [] x, int n) { int [] y = new int [x. length + 1]; int i; for (i = 0; i < x. length ; i ++) { i f (n < x[i]) break; y[i] = x[i]; y[i] = n; for (; i < x. length ; i ++) { y[i +1] = x[i]; return y; TDV: Testing /GU 141110 22 / 42

Basic JUnit usage JUnit can test for expected return values. public c l a s s Ex1Test { @Test public void test_find_min_1 () { int [] a = {5, 1, 7; int res = Ex1. find_min (a); asserttrue( res == 1); @Test public void test_insert_1 () { int [] x = {2, 7; int n = 6; int [] res = Ex1. insert (x, n); int [] expected = {2, 6, 7; asserttrue( Arrays. equals ( expected, res )); TDV: Testing /GU 141110 23 / 42

Testing for Exceptions JUnit can test for expected exceptions @Test (expected= IndexOutOfBoundsException. c l a s s ) public void outofbounds () { new ArrayList < Object >(). get (1) ; expected declares that outofbounds() should throw an IndexOutOfBoundsException. If it does not throw any exception, or throw a different exception the test fails. TDV: Testing /GU 141110 24 / 42

Compile and Run JUnit test class Alt. JUnit plugin in Eclipse IDE: Right click on your project (or choose when creating project): Build Path Add Libraries... Choose JUnit 4. Run the tester class as usual in Eclipse. Compile test class: javac -cp.:pathtojunitjarfile Ex1Test.java To run all methods annotated with @Test in a class: java -cp.:pathtojunitjarfile org.junit.runner.junitcore Ex1Test (under Windows: ; instead of : ) TDV: Testing /GU 141110 25 / 42

Reflection: Extreme Testing JUnit designed for Extreme Testing paradigm Extreme Testing part of Extreme Programming (but not depending on that) TDV: Testing /GU 141110 26 / 42

Reflection: Extreme Testing (cont d) A few words Extreme Programming (no introduction here, but see [Myers], Chapter 8) Extreme Programming (XP) invented by Beck (co-author of JUnit) Most popular agile development process Must create tests first, then create code basis Must run unit tests for every incremental code change Motivation: oo programming allows rapid development still, quality is not guaranteed aim of XP: create quality programs in short time frames XP relies heavily on unit and acceptance testing TDV: Testing /GU 141110 27 / 42

Extreme Unit Testing modules (classes) must have unit tests before coding begins benefits: You gain confidence that code will meet specification. You better understand specification and requirements. You express end result before you start coding. You may implement simple designs and optimise later while reducing the risk of breaking the specification. TDV: Testing /GU 141110 28 / 42

Extreme Testing Example: Class Money c l a s s Money { private int amount ; private Currency currency ; public Money ( int amount, Currency currency ) { t h i s. amount = amount ; t h i s. currency = currency ; public Money add( Money m) { // NO IMPLEMENTATION YET, WRITE TEST FIRST c l a s s Currency { private String name ; public Currency ( String name ) { t h i s. name = name ; Demo in Eclipse. TDV: Testing /GU 141110 29 / 42

Write a Test Case for add() import org. junit.*; import s t a t i c org. junit. Assert.*; public c l a s s MoneyTest { @Test public void simpleadd() { Currency sek = new Currency (" SEK "); Money m1 = new Money (120, sek ); Money m2 = new Money (160, sek ); Money result = m1.add (m2); Money expected = new Money (280, sek ); asserttrue ( expected. equals ( result )); @Test is an annotation, turning simpleadd into a test case TDV: Testing /GU 141110 30 / 42

Example: Class Money Now, implement the method under test, and make sure it fails c l a s s Money { private int amount ; private Currency currency ;... public Money add( Money m) { return null ; TDV: Testing /GU 141110 31 / 42

Compile and Run JUnit test class JUnit reports failure Produce first real implementation TDV: Testing /GU 141110 32 / 42

Example: Class Money First real attempt to implement the method under test c l a s s Money { private int amount ; private Currency currency ; public Money ( int amount, Currency currency ) { t h i s. amount = amount ; t h i s. currency = currency ; public Money add( Money m) { return new Money ( amount +m. amount, currency ); TDV: Testing /GU 141110 33 / 42

Compile and Run JUnit test class JUnit will still report failure Fix possible defects, until test passes. Can you spot it? What if we have different currencies? TDV: Testing /GU 141110 34 / 42

Extend Functionality Extend Money with Euro-exchange-rate first in test cases public c l a s s MoneyTest { @Test public void simpleadd() { Currency sek = new Currency (" SEK ",9.01); Money m1 = new Money (120, sek );... @Test public void adddifferentcurr() { Currency sek = new Currency (" SEK ",9.01); Money m1 = new Money (120, sek ); Currency nok = new Currency (" NOK ",7.70); Money m2 = new Money (160, nok ); Money result = m1.add (m2); Money expected = new Money (307, sek); asserttrue ( expected. equals ( result )); Change, and test implementation TDV: Testing /GU 141110 35 / 42

Common Parts into Test Fixture public c l a s s MoneyTest { private Currency sek; private Money m1; @Before public void setup() { sek = new Currency (" SEK ",9.01) ; m1 = new Money (120, sek ); @Test public void simpleadd () { Money m2 = new Money (140, sek );... @Test public void adddifferentcurr () { Currency nok = new Currency (" NOK ",7.70) ; Money m2 = new Money (160, nok );... TDV: Testing /GU 141110 36 / 42

Integrating Test Units Testing a unit may require: Stubs to replace called procedures Simulate behaviour of component not yet developed. E.g. test code that calls a method not yet implemented. Drivers to replace calling procedures Simulate environment from where procedure is called. E.g. test harness. TDV: Testing /GU 141110 37 / 42

Incremental Testing: Top-Down and Bottom-Up Explore incremental test strategies, following call hierarchy: Top-Down Testing Test main procedure, then go down the call hierarchy requires stubs, but no drivers Bottom-Up Testing Test leaves in call hierarchy, and move up to the root. Procedure is not tested until all children have been tested. requires drivers, but no stubs TDV: Testing /GU 141110 38 / 42

Discussion: Top-down vs Bottom-up Testing Name: Person nr: Hours: Print Paycheck How would you go about testing the Paycheck system Staff Database Given name, looks up address, position. Payroll System calculates: salaries, taxes, bonus Print Paycheck Formats and prints paycheck Bottom-up? Which drivers do you need? Top-down? Which stubs do you need? TDV: Testing /GU 141110 39 / 42

Discussion: Top-down vs Bottom-up Testing Staff Database Given name, looks up address, position. Name: Person nr: Hours: Print Paycheck Payroll System calculates: salaries, taxes, bonus Print Paycheck Formats and prints paycheck Bottom-up? start from e.g. Print Paycheck, Staff Database Which drivers do you need? Driver replacing the caller, here Payroll System, Interface. Top-down? start from e.g. Interface, then Payroll System Which stubs do you need? Stubs replacing called procedures, i.e. Payroll System, Staff Database, Print Paycheck. TDV: Testing /GU 141110 40 / 42

Top-Down Testing: Pros and Cons Advantages of Top-Down Testing Advantageous if major flaws occur toward top level. Early skeletal program allows demonstrations and boosts morale. Disadvantages of Top-Down Testing Stubs must be produced (often more complicated than anticipated). Judgement of test results more difficult. Tempting to defer completion of testing of certain modules. TDV: Testing /GU 141110 41 / 42

Bottom-Up Testing: Pros and Cons Advantages of Bottom-Up Testing Advantageous if major flaws occur toward bottom level. Judgement of test results is easier. Disadvantages of Bottom-Up Testing Driver units must be produced. The program as an entity does not exist until the last unit is added. TDV: Testing /GU 141110 42 / 42