Test-first development

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Test-First Programming. The tests should drive you to write the code, the reason you write code is to get a test to succeed, and you should only write.
Computer Science 209 Testing With JUnit. Why Test? I don ’ t have time, I ’ ve got a deadline to meet The more pressure I feel, the fewer tests I will.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
A Brief Introduction to Test- Driven Development Shawn M. Jones.
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
T ESTING WITH J UNIT IN E CLIPSE Farzana Rahman. I NTRODUCTION The class that you will want to test is created first so that Eclipse will be able to find.
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.
Presented by IBM developer Works ibm.com/developerworks/ 2006 January – April © 2006 IBM Corporation. Making the most of The Java Development Tools project.
Unit testing Java programs1 Unit testing Java programs Using JUnit 4 “If it isn't tested, it doesn’t work”
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Refactoring Lecture 5 CIS 6101 Software Processes and Metrics.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Google Maps Android API v2 吳俊興 國立高雄大學 資訊工程學系 CSF645 – Mobile Computing 行動計算
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
Testing in Extreme Programming
EXtreme Programming: Test-First Copyright Rick Mugridge Test-First Programming Rick Mugridge Department of Computer Science University of Auckland.
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
Test automation / JUnit Building automatically repeatable test suites.
Refactoring An Automated Tool for the Tiger Language Leslie A Hensley
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
A tool for test-driven development
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit A framework which provides hooks for easy testing of your Java code, as it's built Note: The examples from these slides can be found in ~kschmidt/public_html/CS265/Labs/Java/Junit.
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,
Eclipse Project. Installing Visit to download a copy for your home computerhttp:// –Get Release version 3.0 (or.
1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Unit testing Java programs1 Unit testing Java programs Using JUnit 4 “If it isn't tested, it doesn’t work”
Time to apply stuff… Faculty of Mathematics and Physics Charles University in Prague 5 th October 2015 Workshop 1 – Java Wrestling.
1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that.
Software Design– Unit Testing SIMPLE PRIMER ON Junit Junit is a free simple library that is added to Eclipse to all automated unit tests. The first step,
Test automation / JUnit Building automatically repeatable test suites.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Software Development.
Introducing IBM Rational Software Architect
Unit Testing.
Module Road Map Refactoring Why Refactoring? Examples
Unit testing Java programs Using JUnit
Eclipse Navigation & Usage.
Executing Runtime Checks (For Comp401 and Comp410)
Computer Science 209 Testing With JUnit.
An Automated Testing Framework
Software Engineering 1, CS 355 Unit Testing with JUnit
Unit testing C# classes
Some Tips for Using Eclipse
Executing Runtime Checks (For Comp401 and Comp410)
Overview of Eclipse Lectures
How to Run a Java Program
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Introduction to Testing, SUnit and Error Handling
Test-driven development (TDD)
Testing and Test-Driven Development CSC 4700 Software Engineering
JavaTeaching and Importing a github repository
eXtreme Programming (XP) and eXtreme Modeling (XM)
Coding Concepts (Basics)
Test-Driven Development
Chapter 3 – Agile Software Development
How to Run a Java Program
Turning in Assignments
Introduction to JUnit IT323 – Software Engineering II
Refactoring.
CS 240 – Advanced Programming Concepts
Test-Driven Development
Working with Libraries
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Classes and Objects Object Creation
Refactoring.
The beginning of media computation Followed by a demo
Presentation transcript:

Test-first development Writing tests before code clarifies the requirements to be implemented. Tests are written as programs rather than data so that they can be executed automatically. The test includes a check that it has executed correctly. All previous and new tests are automatically run when new functionality is added. Thus checking that the new functionality has not introduced errors.

Guideline for Testing first The name of the test should describe the requirement of the code There should be at least one test for each requirement of the code. Each possible path through of the code is a different requirement Only write the simplest possible code to get the test to pass, if you know this code to be incomplete, write another test that demonstrates what else the code needs to do A test should be similar to sample code, in that it should be clear to someone unfamiliar with the code as to how the code is intended to be used

Guideline for Testing first If a test seems too large, see if you can break it down into smaller tests If you seem to be writing a lot of code for one little test, see if there are other related tests you could write first, that would not require as much code Test the goal of the code, not the implementation One test/code/simplify cycle at a time. Do not write a bunch of tests, and try to get them working all at once Keep writing tests that could show if your code is broken, until you run out of things that could possibly break

Guideline for Testing first When choosing an implementation, be sure to choose the simplest implementation that could possibly work If you are unsure about a piece of code, add a test you think might break it A test is one specific case, for which there is a known answer If all of the tests succeed, but the program doesn't work, add a test

JUnit with Eclipse Before writing any code fragment, an automated test must be written to check the functioning of this code. Since the code does not exist yet, it initially fails the test. After the test begins to pass, duplicate code must be removed.

JUnit with Eclipse Run Eclipse IDE. We will create a new workplace project so click File -> New -> Project, then choose Java and click Next. Type in a project name -- for example, ProjectWithJUnit. Click Finish. The new project will be generated in your IDE. Let's configure our Eclipse IDE, so it will add the JUnit library to the build path. Click on Project -> Properties, select Java Build Path, Libraries, click Add External JARs and browse to directory where your JUnit is stored. Pick junit.jar and click Open. You will see that JUnit will appear on your screen in the list of libraries. By clicking Okay you will force Eclipse to rebuild all build paths.

JUnit with Eclipse To create such a test, right-click on the ProjectWithJUnit title, select New -> Other, expand the "Java" selection, and choose JUnit. On the right column of the dialog, choose Test Case, then click Next.

JUnit with Eclipse import junit.framework.TestCase; public class TestThatWeGetHelloWorldPrompt extends TestCase { public TestThatWeGetHelloWorldPrompt( String name) { super(name); } public void testSay() { HelloWorld hi = new HelloWorld(); assertEquals("Hello World!", hi.say()); public static void main(String[] args) { junit.textui.TestRunner.run( TestThatWeGetHelloWorldPrompt.class);

JUnit with Eclipse public class HelloWorld { public String say() { return("Hello World!"); } } How to write a test case? Set up preconditions Exercise functionality being tested Check postconditions

JUnit with Eclipse Failure test successful test

JUnit with Eclipse The framework uses reflection to find and collect all of the test methods whose signature match Public void testWhatever () We cannot control the order in which the test methods will be run

JUnit with Eclipse

Refactoring Refactoring Refactoring is changing the internal structure of the code without changing functionality Examples: Remove duplicate code Leverage existing code Remove unused code Refactoring mercilessly requires good unit tests and functional tests that can easily be executed Refactoring: If you are adding functionality – you are not refactoring! Although you may need to refactor before adding funtionality. Metaphor example: Pub/Sub system has Publishers Subscribers Topics Messages Does not need to be overly abstracted.

JUnit with Eclipse

Our Assignment Platform: Eclipse Language: Java Screen recording: Microsoft Producer Techniques you will use Test-driving think-aloud

References Kent Beck: „Extreme Programming Explained – Embrace Change“ Martin Fowler: „Refactoring“ XP Exchange www.xpexchange.net Don Wells www.extremeprogramming.org Ron Jeffries www.xprogramming.com There are more in the XP Series by Addison-Wesley; this is just for a start. See also any of the web-sites mentioned.