Computer Science 209 Testing With JUnit.

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.
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.
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
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.
JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.
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.
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.
JUnit Tutorial. Automating Testing Process Testing is boring and time-consuming, it is not a job for “human” Why not automating the testing process? Test.
© S. Demeyer, S. Ducasse, O. Nierstrasz Chapter.1 Unit Testing Explained How to support changes? How to support basic but synchronized documentation?
1 CSC/ECE 517 Fall 2010 Lec. 2 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Debugging 5.Testing.
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 ●
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
Programmer Testing Testing all things Java using JUnit and extensions.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Unit Testing.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Testing in Extreme Programming
1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow
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.
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.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
ESO - Garching 23 June – 02 July, 2003 ACS Course JUnit for Java Unit Testing H. Sommer.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
JUnit Dwight Deugo Nesa Matic
A tool for test-driven development
Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005.
JUnit Don Braffitt Updated: 10-Jun-2011.
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.
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.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
Unit Testing with FlexUnit
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.
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
Test automation / JUnit Building automatically repeatable test suites.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
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.
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
Unit testing Java programs Using JUnit
Chapter 8 – Software Testing
Software Engineering 1, CS 355 Unit Testing with JUnit
JUnit Automated Software Testing Framework
CSCE 315 – Programming Studio, Fall 2017 Tanzir Ahmed
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Test-driven development (TDD)
Testing and debugging A short interlude 2-Dec-18.
Unit testing with JUnit
Testing and debugging A short interlude 17-Apr-19.
Joel Adams and Jeremy Frens Calvin College
JUnit SWE 619 Spring 2008.
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
JUnit Tutorial Hong Qing Yu Nov 2005.
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 Lots of things to test! 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 Write a testing class that extends TestCase import junit.framework.*; public class CardTest extends TestCase{ // Will test the Card 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 CardTest extends TestCase{ public void testgetRank(){ Card c = new Card(2, Suit.spade); assertTrue(c.getRank() == 2); assertTrue(c.getRank() == 3); } A test case is a method that tests the functionality of another method testGetRank is the test case for getRank assertTrue expects true and halts the test if it gets false

Testing equals import junit.framework.*; public void testequals(){ Card c1 = new Card(2, Suit.spade); Card c2 = new Card(3, Suit.heart); assertEquals(c1, c1); assertEquals(c1, new Card(2, Suit.spade)); assertTrue(! c1.equals(c2)); } 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 CardTest extends TestCase{ private Card c1, c2; protected void setUp(){ c1 = new Card(12, Suit.spade); c2 = new Card(12, Suit.heart); } // Tests for getRank and equals can use c1 and c2 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 CardTest extends TestCase{ // Code for fixture and test cases public CardTest(String s){ super(s); } public static Test suite(){ TestSuite suite = new TestSuite(); suite.addTest(new CardTest("testgetRank")); suite.addTest(new CardTest("testequals")); return suite; public static void main(String[] args){ junit.textui.TestRunner.run(suite()); Create an instance of CardTest 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 CardTest extends TestCase{ // Code for fixture and test cases public CardTest(String s){ super(s); } public static Test suite(){ TestSuite suite = new TestSuite(CardTest.class); return suite; public static void main(String[] args){ junit.textui.TestRunner.run(suite()); Create an instance of CardTest 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 For a simple batch-style text interface: >java PatronTest For a GUI that allows you to track and run tests for multiple classes: >java junit.swingui.TestRunner

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