2-1 By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development.

Slides:



Advertisements
Similar presentations
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
Advertisements

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.
Chapter 21 Test Driven development. TDD Write the test first Show that test fails Write code to pass the test Run whole suite of tests! This is unit testing.
JUnit. What is unit testing? A unit is the smallest testable part of an application. A unit test automatically verifies the correctness of the unit. There.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
21-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.
22-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.
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.
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.
26-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.
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.
15-Jul-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 (testing whatever occurs.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
Test-Driven Development “Test first, develop later!” –OCUnit.
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:
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Unit Testing.
Principles of Object Oriented Programming Practical session 2 – part A.
Testing in Extreme Programming
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:
Future Media  BBC MMXI TDD at the BBC David Craddock, Jack Palfrey and Tom Canter.
First BlueJ Day Houston, 2006 Unit Testing with BlueJ Bruce Quig Deakin University.
Advanced Programming in Java
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
Testing, The Ugly Stepchild of Software Development Better Living through Software Testing Chris Berry – Feb 21, 2000 Chris Berry
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
Sadegh Aliakbary Sharif University of Technology Spring 2011.
JUnit Don Braffitt Updated: 10-Jun-2011.
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Advanced programming Practices
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
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Computer Science 209 Testing With JUnit.
Unit testing C# classes
Test Driven development
Test Driven development
JUnit 28-Nov-18.
JUnit 28-Nov-18.
Introduction to JUnit CS 4501 / 6501 Software Testing
JUnit 7-Dec-18.
JUnit 11-Jan-19.
More JUnit CS 4501 / 6501 Software Testing
Introduction to JUnit IT323 – Software Engineering II
Class Intro/TDD Intro 8/23/2005
JUnit 18-Apr-19.
JUnit 31-May-19.
Presentation transcript:

2-1 By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development

2-2  What is TDD?  Tests as documentation  Tests as a way to verify your code works Outline

2-3 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

2-4 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

2-5 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

2-6 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

2-7 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 – There are many:  Without a unit-testing framework, TDD is virtually impossible

2-8 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

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

2-10 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

2-11 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); }

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

2-13 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"); }

2-14 Assertions  Useful assertions that go into test methods // Asserts that a condition is true. // If it isn't it throws an AssertionFailedError. public static void assertTrue(boolean condition) // Asserts a condition is true. // If not throw an AssertionFailedError with the message. public static void assertTrue(java.lang.String message, boolean condition) // Asserts that two objects are equal. If they are not an // AssertionFailedError is thrown with the given message. public static void assertEquals(String message, Object expected, Object actual)  All assertions of the org.junit.Assert can be found at

2-15 Project 1: Bowling Score  Develop (design and test) a new type that maintains the score for an automated bowling scoring system  Requirements –Can register pins knocked down –Know current score at all times –Read Project 1 (there is a handout)

2-16 Collaborative Exercise  Get together in a team of 3  Introduce yourselves  The person who has the lowest bowling average will be the scribe  On a piece of paper write down four things you would test, for example  scoreSoFar after one pinsDowned message  scoreSoFar after a strike in frame 1