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.

Slides:



Advertisements
Similar presentations
JUnit Tutorial Hong Qing Yu Nov JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices.
Advertisements

Unit Testing Australian Development Centre Brisbane, Australia.
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.
1 Unit Testing with JUnit CS 3331 Fall 2009 Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report, 3(7):37-50, Available.
J-Unit Framework.
Unit and Functional Testing with JUnit and Related Tools Greg Barnes University of Washington
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Approach of Unit testing with the help of JUnit Satish Mishra
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
Introduction to Eclipse, Unit Testing and JUnit David Rabinowitz.
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.
JUnit, Revisited 17-Apr-17.
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.
Program Testing Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
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.
© S. Demeyer, S. Ducasse, O. Nierstrasz Chapter.1 Unit Testing Explained How to support changes? How to support basic but synchronized documentation?
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
Unit Testing Discussion C. Unit Test ● public Method is smallest unit of code ● Input/output transformation ● Test if the method does what it claims ●
CODING Research Data Management. Research Data Management Coding When writing software or analytical code it is important that others and your future.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Testing in Extreme Programming
1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow
CSE 219 Computer Science III Testing. Testing vs. Debugging Testing: Create and use scenarios which reveal incorrect behaviors –Design of test cases:
Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
Test automation / JUnit Building automatically repeatable test suites.
Junit At the forefront of Test Driven Development.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
First BlueJ Day Houston, 2006 Unit Testing with BlueJ Bruce Quig Deakin University.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
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.
CSC Programming I Lecture 6 September 4, 2002.
Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Don Braffitt Updated: 10-Jun-2011.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
JUnit A Unit Testing Framework for Java. The Objective Introduce JUnit as a tool for Unit Testing Provide information on how to: Install it Build a test.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
JUnit, Bugzilla James Atlas July 24, 2008 *part of today’s slides courtesy of Dwight Deugo and Nesa Matic under the EPL.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
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.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Testing JUnit Testing. Testing Testing can mean many different things It certainly includes running a completed program with various inputs It also includes.
Test automation / JUnit Building automatically repeatable test suites.
Debugging and Testing Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Introduction to Unit Testing and JUnit David Rabinowitz.
Testing and Debugging UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Unit Testing.
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Dept of Computer Science University of Maryland College Park
Computer Science 209 Testing With JUnit.
Test-first development
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Test-driven development (TDD)
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Presentation transcript:

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 write Fewer tests lead to less accurate and less stable code Less accurate and less stable code puts more pressure on me...

Integrate Testing and Coding Code a little, test a little, code a little, test a little... Write test units before coding the pieces to be tested Get immediate feedback and fix bugs as you code

Tester Programs Test individual classes where possible, then interactions Bottom-up approach A main method displays expected results before actual results of running methods

Testing Freecell public class FreeCellTester{ public static void main(String[] args){ FreeCellModel = new FreeCellModel(); System.out.println(model); } // Now what?? } Lots of things to test! Smaller pieces (deck, cards, particular types of cells) should be tested first

Problems With Tester Programs Must visually inspect a long output stream to check results (tedious and error-prone) Must create code to produce visually useful output Tests of methods are not modularized: One long sequence that is hard to maintain

JUnit Provides “ outside influence ” in the form of a framework for unit testing Highly modular, can create a suite of test cases for each class and add to them incrementally Assertion-based: A failed test halts with an error message, so verification is automated

The Basic Framework import junit.framework.*; public class PatronTest extends TestCase{ // Will test the Patron class using JUnit } Write a testing class that extends TestCase TestCase is defined in the junit.framework package You should write a similar class for each of the classes that are unit-tested in your system

Add a Test Case import junit.framework.*; public class PatronTest extends TestCase{ public void testIncBooksOut(){ Patron p1 = new Patron("Lambert", "Java"); for (int i = 1; i <= Patron.MAX_BOOKS; i++){ p1.incBooksOut(); assertTrue(i == p1.getBooksOut()); } A test case is a method that tests the functionality of another method testIncBooksOut is the test case for incBooksOut assertTrue expects true and halts the test if it gets false

Testing equals import junit.framework.*; public void testEquals(){ Patron p1 = new Patron("Lambert", "Java"); Patron p2 = new Patron("Levy", "Scheme"); assertEquals(p1, p1); assertEquals(p1, new Patron("Lambert", "Java")); assertTrue(! p1.equals(p2)); } assertEquals runs the equals method with its two parameters Be sure to test all of the possible options of equality

Establish a Fixture import junit.framework.*; public class PatronTest extends TestCase{ private Patron p1, p2; protected void setUp(){ p1 = new Patron("Lambert", "Java"); p2 = new Patron("Levy", "Scheme"); } // Tests for incBooksOut and equals can use p1 and p2 } A fixture captures a common context for several test cases This context consists of a set of objects that can be initialized once in a setup method and made visible to all test cases Some fixtures can consist of many objects, so this really saves time

Create a Suite of Test Cases import junit.framework.*; public class PatronTest extends TestCase{ // Code for fixture and test cases public PatronTest(String s){ super(s); } public static Test suite(){ TestSuite suite = new TestSuite(); suite.addTest(new PatronTest("testIncBooksOut")); suite.addTest(new PatronTest("testEquals")); return suite; } public static void main(String[] args){ junit.textui.TestRunner.run(suite()); } Create an instance of PatronTest for each test case method and pass the name of the method to the constructor Add each case to a TestSuite object Run the suite in the main method Be sure that the names of the test case methods are spelled correctly!

Create a Suite of Test Cases import junit.framework.*; public class PatronTest extends TestCase{ // Code for fixture and test cases public PatronTest(String s){ super(s); } public static Test suite(){ TestSuite suite = new TestSuite(PatronTest.class); return suite; } public static void main(String[] args){ junit.textui.TestRunner.run(suite()); } Create an instance of PatronTest for each test case method and pass the name of the method to the constructor Add each case to a TestSuite object Run the suite in the main method Alternatively, pass the class of the test cases to another Suite constructor

Running a Test >java PatronTest >java junit.swingui.TestRunner For a simple batch-style text interface: For a GUI that allows you to track and run tests for multiple classes:

Guidelines for Testing Write tests as you develop, when you imagine how the code will run Factor the test code to reflect the factoring of your system Keep the old tests running, and always run all of your tests

Guidelines for Testing Avoid writing print statements; write tests instead Focus on tests that are useful –When you add functionality to the system –During debugging, write a test that will succeed if the code works, then debug with it

Guidelines for Testing Run every test at least once a day Set them up so that you can ’ t go home unless they ’ re all 100% You ’ ll be more aggressive about refactoring because you won ’ t fear breaking the code