Practical Objects: Test Driven Software Development using JUnit

Similar documents
xtreme Programming (summary of Kent Beck s XP book) Stefan Resmerita, WS2015

Agile Manifesto & XP. Topics. Rapid software development. Agile methods. Chapter ) What is Agile trying to do?

Chapter 14 Software Testing Techniques

Unit Testing with JUnit and CppUnit

The Power of Unit Testing and it s impact on your business. Ashish Kumar Vice President, Engineering

Software Engineering 2 A practical course in software engineering. Ekkart Kindler

EECS 4313 Software Engineering Testing

JUnit Test Patterns in Rational XDE

XP Evolution Rachel Davies

2. Reasons for implementing clos-unit

So, You Want to Test Your Compiler?

Introduction to Extreme Programming. Extreme Programming is... Benefits. References: William Wake, Capital One Steve Metsker, Capital One Kent Beck

Testing Stragegies. Black Box Testing. Test case

Object Oriented Software Design - I

Test automation / JUnit. Building automatically repeatable test suites

Test automation Test automation / JUnit

Analysis of the Test Driven Development by Example

Motivating Example: Two Types of Errors (2) Test-Driven Development (TDD) with JUnit. Motivating Example: Two Types of Errors (1)

Test-Driven Development (TDD) with JUnit

Introduction to Extreme Programming

Automated testing in Agile SW development

Test Driven Development TDD

Credit where Credit is Due. Lecture 29: Test-Driven Development. Test-Driven Development. Goals for this lecture

COMP 354 TDD and Refactoring

Test-Driven Development

JUnit A Cook's Tour. Kent Beck and Erich Gamma. Renaat Verbruggen 1#

CS193D Handout 15 Winter 2005/2006 February 8, 2006 Software Engineering Methodologies and XP. See also: Chapter 6. The Stagewise Model

Tuesday, November 15. Testing

Utilizing Fast Testing to Transform Java Development into an Agile, Quick Release, Low Risk Process

Program Correctness and Efficiency. Chapter 2

Chapter 15. Software Testing The assert Statement

Software Design and Analysis CSCI 2040

Test automation / JUnit. Building automatically repeatable test suites

Java Review via Test Driven Development

COURSE 11 DESIGN PATTERNS

Test First Software Development

Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (testing whatever occurs to you at

Introduction to Automated Unit Testing (xunit) Brian Nielsen Arne Skou

Testing in Agile Software Development

Introduction to JUnit

Overview. State-of-the-Art. Relative cost of error correction. CS 619 Introduction to OO Design and Development. Testing.

JUnit 4 and Java EE 5 Better Testing by Design

8. Quality Assurance

JUnit in EDA Introduction. 2 JUnit 4.3

Software Engineering I (02161)

Introduction to JUnit. Data Structures and Algorithms for Language Processing

Soda Machine Laboratory

Automated Acceptance Testing

Test Driven Development. René Barto SES Agile Development - Test Driven Development

TEST DRIVEN DEVELOPMENT

CSE : Python Programming. Packages (Tutorial, Section 6.4) Announcements. Today. Packages: Concretely. Packages: Overview

TEST-DRIVEN DEVELOPMENT

How Can a Tester Cope With the Fast Paced Iterative/Incremental Process?

Building in Quality: The Beauty of Behavior Driven Development (BDD) Larry Apke - Agile Coach

Basic Keywords Practice Session

Extreme programming XP 6

Software Development Process Models

TOOLS AND TECHNIQUES FOR TEST-DRIVEN LEARNING IN CS1

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

Agile Development

print statements, debugger expressions, test scripts. Writing expressions in a debugger only that t a program works now. An application typically

Lecture 20: SW Testing Presented by: Mohammad El-Ramly, PhD

E xtr B e y CS R m oy 6704, e T a P n a Spring r n o d J g ia n 2002 r g a S m hu m ing

McCa!"s Triangle of Quality

Turning a Marathon Runner into a Sprinter: Adopting Agile Testing Strategies and Practices at Microsoft

Regression testing. Whenever you find a bug. Why is this a good idea?

Introduction to Testing and Maintainable code

MOCKING TO FACILITATE UNIT TESTING. Abstract

XP: Planning, coding and testing. Practice Planning game. Release Planning. User stories. Annika Silvervarg


Tools for Unit Test JUnit

Main concepts to be covered. Testing and Debugging. Code snippet of the day. Results. Testing Debugging Test automation Writing for maintainability

Question 1: What is a code walk-through, and how is it performed?

System Integration and Build Management

OBJECT-ORIENTED DESIGN

Software Engineering I (02161)

Project Automation. If it hurts, automate it! Jan Pool NioCAD University of Stellenbosch 19 March 2008

CSC207 Week 3. Larry Zhang

Software Design Models, Tools & Processes. Lecture 6: Transition Phase Cecilia Mascolo

Lecture 15 Software Testing

Seven Key Factors for Agile Testing Success

Software Testing Infrastructure status

CS 113 MIDTERM EXAM 2 SPRING 2013

DCS/100: Procedural Programming

Simple TDD Case Study. in Java

TESTING AND DEBUGGING

Steps for project success. git status. Milestones. Deliverables. Homework 1 submitted Homework 2 will be posted October 26.

Test Driven Development

Refactoring, 2nd Ed. A love story. Michael Hunger

A few more things about Agile and SE. Could help in interviews, but don t try to bluff your way through

Software Development Methodologies

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

Levels of Testing Testing Methods Test Driven Development JUnit. Testing. ENGI 5895: Software Design. Andrew Vardy

Automated Unit Testing A Practitioner's and Teacher's Perspective

An Introduction to Unit Testing

Extreme Programming practices for your team. Paweł Lipiński

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

Topic 01. Software Engineering, Web Engineering, agile methodologies.

Dilbert Scott Adams. CSc 233 Spring 2012

Software Engineering

Transcription:

1999 McBreen.Consulting Practical Objects Test Driven Software Development using JUnit Pete McBreen, McBreen.Consulting petemcbreen@acm.org Test Driven Software Development??? The Unified Process is Use Case Driven Requirements drive the development process, progress is measured by how many Use Cases are done extreme Programming is Test Driven Testable Requirements drive the process, progress is measured by how many Functional Tests pass JUnit is a freeware Java Testing Framework that allows developers to test their own code Writing the tests first before writing the code ensures that the unit tests are a real test of the implementation Page 2 Page-1

extreme Programming states that Pictures are nice, but tested code is better extreme Programming is a new methodology It is a very high discipline process, but with the absolute minimum of mandated activities or deliverables "Listening, Testing, Coding, Designing. That's all there is to software. Anyone who tells you different is selling something." Kent Beck Extreme Programming is a minimal methodology that works with developer instincts Delivery is a shared responsibility between developers and the user community Design occurs by getting all developers to contribute to the product, rather than a design team and coding team Page 3 Adoption of extreme Programming is just starting, but we can apply the lessons now extreme Programming is a developer scale, lightweight methodology based on four values Ultra high frequency Communication Simplicity of both design and process Feedback driven Activities Courage - embrace change as a way of life, rather than build processes to limit change When we come to realize that change is the only reality, life is much easier - Anon Page 4 Page-2

Applying the lessons of Extreme Programming Steering projects Applying the Quality First strategy Using feedback to improve system quality Big design up front does not work Separating Business and Technical responsibilities Incremental development means incremental requirements capture Make it run, make it right, make it fast Unit Testing and really meaning it Avoiding Integration Hell Refactoring to improve design quality Experimentation requires a high discipline process Use a process coach to tune and tailor your process Page 5 Quality First says do not add any features until you know all existing code works Most projects "add quality" as time progresses Testing "inspects quality" into the software Integration hell is normal It's feature complete, time to get the bugs out XP gets quality to 100% as early as possible The system always runs with zero bugs Developers incrementally capture requirements and add features Quality Time Traditional Projects ship at the end Quality Time XP Projects can ship anytime Page 6 Page-3

Quality First is a nice idea, but how do we manage to achieve it - by getting feedback Feedback is essential for learning, we need to know the outcome to change our actions The greater the delay between action and visible outcome, the smaller are the chances of effective learning In software development, the best feedback occurs when we run a system So it is useful to progress from Requirements, through Design and into Implementation quickly In order to get the necessary speed, do incremental development, developing just a small part each time Just doing the activities faster is not a good idea Forcing the pace just increases errors Page 7 Make it run, make it right, make it fast Developers should make the design executable first by writing the code Running the test cases will then identify any problems with the implementation or requirements Then developers make the design as simple as possible and correct Rewrite for clarity, to reflect what has been learned so far and to meet project standards Only make it fast if performance measurements indicate that it will be too slow for use Measure and profile to find where the system is spending time, and if possible apply a hardware fix Page 8 Page-4

Test everything that could possibly break Relentless Testing means that the Quality of the complete system is always known The benefit of no integration surprises is only available if you have a complete set of tests that you always run no matter how small or insignificant the change Tests fall into one of two categories Unit Tests are written by the Developers, so that they know that every method works as intended Functional tests are owned by the Business, so that they know that every requirement has been satisfied Page 9 Unit Tests are owned by developers For all classes Test a single class and all it's collaborators Everything that could possibly break Run all unit tests before any release (or checkin) Must always score 100%!!! Unit testing requires no manual intervention Don t let the sun set on bad code Work in short episodes, checking in tested code frequently Page 10 Page-5

Functional Tests are owned by customer Test every scenario from every Use Case End to end Input through output Check your export files, database contents, printouts Specify exactly what a requirement means Score increases show progress Catch regressions Page 11 Continuous Integration is a way of avoiding Integration Hell The longer you wait to integrate, the longer it takes, and the more painful it is THEREFORE: don t wait at all! Microsoft integrates daily to weekly extreme Programming projects integrate multiple times PER DAY Take small steps and always ensure that the complete system still passes all of the tests Deleting tests to make your code pass the tests will soon be made a capital offense Page 12 Page-6

To facilitate testing of Classes, a Test Harness is needed for each class that is developed The Unit Test Harness must be capable of fully exercising all of the behaviors of the Class It should create objects, invoke all methods, destroy the objects and validate the performance The Unit Test Harness should also test all invariants and all method pre-conditions and post-conditions All Tests should be run as a Smoke Test following all changes and prior to source code check-in This prevents most errors from entering the master source A Test Harness should require no manual intervention to minimize developer effort Tests that require manual intervention are rarely used Page 13 To introduce the idea of a Test Harness, a very simple Counter Class will serve as an example A partial listing for the Counter Class class Counter { public int currvalue; public int maxvalue; Counter currvalue maxvalue public void increment() { if (currvalue < maxvalue) currvalue = currvalue + 1; public void decrement() { currvalue = currvalue - 1; public void setvalue(int newvalue) { if (newvalue < maxvalue) currvalue = newvalue; increment( ) decrement( ) setvalue( ) Page 14 Page-7

A Test Harness that simply prints the output requires a manual check of the results class TestCounter { public static void main(string argv[]) { Counter mycounter = new Counter(10); mycounter.setvalue(9); System.out.println("Counter "+ mycounter); mycounter.increment(); System.out.println("Counter "+ mycounter); System.exit(0); To avoid manual checking, tests should only produce output in case of error mycounter.setvalue(9); if (mycounter.currvalue!= 9) System.out.println("Error "+ mycounter); Page 15 Functional Test Frameworks are necessary for testing the interactions between objects Functional Test Frameworks should be built to support testing of the complete system Facades simplify this since a Command Line or Batch presentation can be created to drive the application Use Cases are the source for Functional Tests Just because a Class is internally correct, it does not mean that there will be no interaction errors between objects Using test scripts to exercise the application is essential for regression testing Tests can run unattended after every change to the system The presentation logic will still need to be tested, but this will occur after the total application logic has been tested Page 16 Page-8

The JUnit Testing Framework for Java is an example of a Test Harness It provides TestCase and TestSuite classes for managing the execution of Tests public class MoneyTest extends TestCase A TestCase supports the suite() method that returns a set of Tests that can be executed A Test Failure is an anticipated problem that was tested for A Test Error was an unexpected problem that threw an exception in the code being tested JUnit supports both GUI and TextUI execution of the same test cases A live example of providing alternate presentations for the same underlying application logic Page 17 The Graphical User Interface provides simple feedback on the overall Progress Page 18 Page-9

Errors and Failures are highlighted by the coloring in the progress bar Page 19 The details of each Error and Failure are captured by the thrown assertion Page 20 Page-10

Created by Erich Gamma and Kent Beck, JUnit is freely available for download and use Documentation and source code is available at http://members.pingnet.ch/gamma/junit.htm Instructions for use are provided on this webpage titled Test Infected: Programmers Love Writing Tests JUnit was ported to C++ by Michael Feathers, CppUnit is at http://www.armaties.com There are some minor operational differences due to the differences in language capabilities Both implementations provide A README file for installation A cookbook for how to define new test cases Page 21 Using JUnit, an instance of a subclass of TestCase is created for each Unit Test import test.framework.*; public class CounterTest extends TestCase { protected Counter mycounter; public CounterTest(String name) { super(name); protected void setup() { mycounter = new Counter(10); public void testsetvalue() { mycounter.setvalue(9); assertequals(9, mycounter.currvalue); public static Test suite() { TestSuite suite= new TestSuite(); suite.addtest(new CounterTest(" testsetvalue")); return suite; Page 22 Page-11

The JUnit TestCase class allows a set of tests to be run independently The setup() method is invoked prior to running the method named in the TestCase constructor suite.addtest(new CounterTest(" testsetvalues")); If this method is not present, a NoSuchMethodException will be reported as an Error by the Test Harness Run: 1 Failures: 0 Errors: 1 The teardown() method is invoked after running the method to clean up if necessary TestCase can also support a main method public static void main (String[] args) { test.textui.testrunner.run (suite()); Page 23 Designing unit tests When to write a Unit Test is a simple decision Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead. -- Martin Fowler So if a Method could be affected by a coding error, write tests that validate that the Implementation is correct Functional Tests are written for every Use Case as well, but defining these is the responsibility of the Users The Unit Tests are complete when the tests can detect any change in the software If it is possible to change the software without the tests catching the problem then the test suite is incomplete Page 24 Page-12

Designing functional tests from use cases uses the failure conditions from the scenarios For each Failure Condition in a Use Case, Test Plans need test cases to cause that failure And the usual boundary testing applies with values just above and below the threshold to detect problems 4a. Customer has exceeded credit limit Test 1 - Outstanding balance = credit limit Test 2 - Outstanding balance + order value = credit limit Dependencies between the various tests can be identified from the different scenarios Conditions that are only recognized in recoverable scenarios should not affect normal operation e.g. Preferred customer status only affects operation if Customer has exceeded credit limit Page 25 JUnit Demonstration Page 26 Page-13

Closing Thoughts and Question Time Software development is possible without a good testing harness, but it is a stressful practice Using a good testing framework allows for less stress and much more confidence in the code It is much faster to Write the unit tests first, then implement the methods If your implementation environment does not have a port of JUnit, write your own Remember that software development is meant to be fun, if it isn't, the process is wrong Page 27 Links and references The extreme Programming Website http://www.xprogramming.com/ Manifesto for software development http://members.aol.com/acockburn/manifesto.html Software Development as a Cooperative Game http://members.aol.com/humansandt/papers/asgame/asgame.htm Test Infected: Programmers Love Writing Tests http://members.pingnet.ch/gamma/junit.htm Test Driven Software Development Using JUnit http://www.cadvision.com/roshi/talks/testdriven.html Page 28 Page-14