By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)

Slides:



Advertisements
Similar presentations
Unit Testing. Topics Motivation JUnit framework Basic JUnit tests – static methods Ensure exceptions – instance methods.
Advertisements

CIT 590 Unit testing.
A Brief Introduction to Test- Driven Development Shawn M. Jones.
JUnit intro Kalvis Apsitis. What are “Programmer Tests”? Programmer Testing is the testing performed by a developer with the goal of verifying the correct.
Ch. 2 Exploring core JUnit. This chapter covers ■ Using the core JUnit classes ■ Understanding JUnit mechanisms ■ Understanding the JUnit lifecycle.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Asserting Java ©Rick Mercer Chapter 3 Objects and JUnit.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
JUnit, Revisited 17-Apr-17.
JUnit. Why is testing good? Due to psychological factors, programmers are bad testers. A computer can test much faster than a human Philosophy: “If it.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
TDD Test-Driven Development. JUnit 4.0 To use annotations need to import org.junit.Test To use assertion need to import org.junit.Assert.* No need to.
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
Test-Driven Development “Test first, develop later!” –OCUnit.
CIT 590 Unit testing. Agenda Debugging attempt 2 (because I am stubborn) What is unit testing Why? Unit testing framework in Python.
CS/ENGRD 2110 FALL 2014 Lecture 3: Fields, getters and setters, constructors, testing 1.
Test-Driven Development With Visual Studio 2005 Erno de Weerd Info Support.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
CS/ENGRD 2110 SPRING 2015 Lecture 3: Fields, getters and setters, constructors, testing 1.
Principles of Object Oriented Programming Practical session 2 – part A.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Designing For Testability. Incorporate design features that facilitate testing Include features to: –Support test automation at all levels (unit, integration,
Testing in Extreme Programming
EXtreme Programming: Test-First Copyright Rick Mugridge Test-First Programming Rick Mugridge Department of Computer Science University of Auckland.
Introduction to Testing 1. Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
CSE 219 Computer Science III Testing. Testing vs. Debugging Testing: Create and use scenarios which reveal incorrect behaviors –Design of test cases:
© ALEXANDRE CUVA  VERSION 2.00 Test Driven Design.
October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved.
Testing in NetBeans. SWC Testing The ideal test: When the test is passed, the product is ready for delivery! Ideal – but (almost) impossible –Number of.
Working with arrays (we will use an array of double as example)
Advanced Programming in Java
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
1-1 1) Software Development 2) with TDD at the end By Rick Mercer with help from these sources: Rational Unified Process Computing Fundamentals with C++,
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
JUnit Adam Heath. What is JUnit?  JUnit is a unit testing framework for the Java programming language  It allows developers to swiftly and easily test.
A tool for test-driven development
Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005.
Testing, The Ugly Stepchild of Software Development Better Living through Software Testing Chris Berry – Feb 21, 2000 Chris Berry
Sadegh Aliakbary Sharif University of Technology Spring 2011.
JUnit Don Braffitt Updated: 10-Jun-2011.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
2-1 By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Lecture 5: Test-Driven Development Basics
Unit Testing.
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
Unit Testing with JUnit
Dept of Computer Science University of Maryland College Park
Introduction to JUnit CS 4501 / 6501 Software Testing
Computer Science 209 Testing With JUnit.
Unit testing C# classes
JUnit 28-Nov-18.
Credit to Eclipse Documentation
Introduction to JUnit CS 4501 / 6501 Software Testing
Chapter 6 Class Definitions and Member Functions
More JUnit CS 4501 / 6501 Software Testing
Introduction to JUnit IT323 – Software Engineering II
Class Intro/TDD Intro 8/23/2005
Principles of Object Oriented Programming
Presentation transcript:

By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)

 What is TDD?  Using Tests as documentation and explanation  Assertions verify your code works -Even when you change other parts of the system Outline

Test Driven Development (TDD) "TDD is emerging as one of the most successful developer productivity enhancing techniques to be recently discovered. The three-step: write test, write code, refactor – is a dance many of us are enjoying" –Eric Vautier, David Vydra "TDD is the Gem of Extreme Programming" –Kent Beck

TDD: What is it?  TDD turns traditional development around –Write test code before the methods –Do so in very small steps –Refuse to add any code until a test exists for it Have a failing test (red bar) before writing the code to make the test pass  You can run tests anytime –And you should do so quite often

Simple, yet hard to do  When you first try TDD, it requires great discipline because it is easy to “slip” and write methods without first writing a new test  Pair programming helps you to stay on track –Williams and Kessler

Comments, Documentation?  Most programmers don’t read documentation –instead they prefer to work with the code  Programmers often look for sample code that sends messages to instances of a class –Well-written unit tests can provide such a working specification of your code more docs are necessary –Unit tests effectively become a significant portion of your technical documentation

Testing Framework  An underlying assumption of TDD is that you have a unit-testing framework  Good software developers use a testing framework such as JUnit –Many exist  Without a unit-testing framework, TDD is virtually impossible

TDD Helps Design  TDD makes you think about what you want or need before you write the code –pick class and method names –decide the needed arguments –choose return types –write the class, constructors, instance variables, and methods to make what you want work

Benefits of TDD  Benefits include –Rapid feedback –Interface before algorithm –Know when you’re finished –Change the code with confidence

Principles of TDD  You maintain an exhaustive suite of Programmer Tests  No code goes into production unless it has associated tests  You write the tests first  Tests determine what code you need to write if you are programming by intention –Then you are designing as you write tests

A unit test with 2 test methods import static org.junit.Assert.*; import org.junit.Test; public class BankAccountTest public void testDeposit() { BankAccount anAcct = new BankAccount("Zac", ); assertEquals( , anAcct.getBalance(), 1e-12); anAcct.deposit(0.52); assertEquals( , anAcct.getBalance(), 1e-12); public void testWithdraw() { BankAccount anAcct = new BankAccount("Zac", ); assertEquals( , anAcct.getBalance(), 1e-12); anAcct.withdraw(10.52); assertEquals(990.00, anAcct.getBalance(), 1e-12); }

Annotations  Annotations provide data about a program that is not part of the program itself. Uses include - Information for the compiler  Annotations can be used by the compiler to detect errors or suppress - Compiler-time and deployment-time processing  Software tools can process annotation information to generate code, XML files - Runtime processing  examined at

JUnit Annotations  Useful annotations used by JUnit marks a test method mark methods that execute before and after every test method in the unit marks a method that executes exactly once before any other annotated method in the unit test

What is the output? import org.junit.*; public class ShowFlowOfControl public static void beforeAllOthers() { System.out.println("First one time"); public void beforeEachTest() { System.out.println("Before"); public void afterEachTest() { System.out.println("After"); public void test1() { System.out.println("one"); public void test2() { System.out.println("two"); }

Assertions  Useful assertions that go methods // Asserts that a condition is true public static void assertTrue(boolean condition) public static void assertEquals(int expected, int actual) public static void assertEquals (double expected, double actual, double err) // Asserts that two Object objects are equal public static void assertEquals(Object expected, Object actual)  All assertions of org.junit.Assert can be found at

So why test code?  So we develop quality software that works  It can get you jobs  It's what good software developers do  It is very important to test your code  You can develop code in half the time  Gives you the power and confidence to refactor  To save lives and help avoid financial losses  Rick says you have to

JUnit Never in the field of software development was so much owed by so many to so few lines of code —Martin Fowler