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.

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

J-Unit Framework.
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.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
1-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.
Approach of Unit testing with the help of JUnit Satish Mishra
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.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
JUnit Syed Nabeel. Motivation Unit Testing Responsibility of  developer Rarely done properly Developers Excuse: “I am too much in a hurry”
JUnit, Revisited 17-Apr-17.
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.
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.
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.
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.
Testowanie kodu Bartosz Baliś, Na podstawie prezentacji Satisha Mishra Iana Sommerville Erica Braude.
CS 635 Advanced Object-Oriented Design & Programming Spring Semester, 2006 Doc 2 Terms & Testing Jan 24, 2006 Copyright ©, All rights reserved SDSU.
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.
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.
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.
03 Using Eclipse. 2 IDE Overview An IDE is an Interactive Development Environment Different IDEs meet different needs BlueJ and DrJava are designed as.
Unit testing Java programs1 Unit testing Java programs Using JUnit 4 “If it isn't tested, it doesn’t work”
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
INF 111 / CSE 121 Discussion Session Week 2 - Fall 2007 Instructor: Michele Rousseau TA: Rosalva Gallardo.
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.
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.
JUnit & Eclipse1 DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY Feb 2, 2009 revision 1.2 – Feb 2, 2009 by Emil Vassev & Joey.
Test automation / JUnit Building automatically repeatable test suites.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
CSC 395 – Software Engineering Lecture 10: Execution-based Testing –or– We can make it better than it was. Better...faster...agiler.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
JUnit Dwight Deugo Nesa Matic
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.
JUnit Dwight Deugo Nesa Matic
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
A tool for test-driven development
Simple Java Unit Testing with JUnit 4 and Netbeans 6.1 Kiki Ahmadi JUG-Bonek.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Don Braffitt Updated: 10-Jun-2011.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
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,
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.
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. 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.
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.
Java IDE Dwight Deugo Nesa Matic
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 Java programs Using JUnit
Software Engineering 1, CS 355 Unit Testing with JUnit
Overview of Eclipse Lectures
Introduction to JUnit IT323 – Software Engineering II
Developing Java Applications with NetBeans
Developing Java Applications with NetBeans
Joel Adams and Jeremy Frens Calvin College
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:

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 the code for a class is written, but before other classes are written.  After code for all classes is written, but before the classes are integrated.  After the classes are integrated.  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After the code for a class is written, but before other classes are written.  After code for all classes is written, but before the classes are integrated.  After the classes are integrated.

Testing at the Boundaries  What does this mean?  Example (from the online notes):  For a method that removes an element from a list:  Test on a list with 1 element.  Test on a list that does not contain the element to be removed.  Test on a list where the element is at the beginning/end of the list.  Test on a list where the element appears multiple times.  Test on an empty list.  What does this mean?  Example (from the online notes):  For a method that removes an element from a list:  Test on a list with 1 element.  Test on a list that does not contain the element to be removed.  Test on a list where the element is at the beginning/end of the list.  Test on a list where the element appears multiple times.  Test on an empty list.

Testing a Square-Root Routine  From the text: What kinds of tests were proposed?  Test numbers greater than 1.  Test numbers less than 1.  Test negative numbers.  Test randomly generated numbers.  Apply the inverse of the square-root operation.  From the text: What kinds of tests were proposed?  Test numbers greater than 1.  Test numbers less than 1.  Test negative numbers.  Test randomly generated numbers.  Apply the inverse of the square-root operation.

Testing a Stack  Suppose we have a MyStack class implemented in an integer array.  MyStack(int n);  void push(int i);  int pop();  int top();  What test cases should we use? (Discuss in groups.)  Suppose we have a MyStack class implemented in an integer array.  MyStack(int n);  void push(int i);  int pop();  int top();  What test cases should we use? (Discuss in groups.)

What is JUnit?  Regression testing framework  Written by Erich Gamma and Kent Beck  Used for unit testing in Java  Open Source  Released under IBM's CPL  Regression testing framework  Written by Erich Gamma and Kent Beck  Used for unit testing in Java  Open Source  Released under IBM's CPL

Testing » Where Does JUnit Come From?  JUnit’s web site:  Eclipse includes JUnit  Eclipse provides new GUI to run JUnit test cases and suites  JDT tools include a plug-in that integrates JUnit into the Java IDE  Allows you to define regression tests for your code and run them from the Java IDE.  JUnit’s web site:  Eclipse includes JUnit  Eclipse provides new GUI to run JUnit test cases and suites  JDT tools include a plug-in that integrates JUnit into the Java IDE  Allows you to define regression tests for your code and run them from the Java IDE.

Testing » JUnit Test Cases  JUnit Test Cases  Test case  Runs multiple tests  Implemented as subclass of TestCase  Define instance variables that store the state of the tests in the class  Initialize TestCase by overriding setUp method  Cleanup after test case is done by overriding tearDown method  The Test framework will invoke the setUp and tearDown methods.  JUnit Test Cases  Test case  Runs multiple tests  Implemented as subclass of TestCase  Define instance variables that store the state of the tests in the class  Initialize TestCase by overriding setUp method  Cleanup after test case is done by overriding tearDown method  The Test framework will invoke the setUp and tearDown methods.

Testing » Creating JUnit Test Cases in Eclipse  Create a new package to contain your test case classes.  Add the JUnit JAR file to the project’s buildpath.  Create a new package to contain your test case classes.  Add the JUnit JAR file to the project’s buildpath.

Testing » Creating JUnit Test Cases in Eclipse  Select your testing package  From the context menu select New » JUnit Test Case. This opens the New JUnit Test Case Wizard.  Fill in the name of your test case in the Name field.  Select the method stubs that you want Eclipse to generate  This will create the corresponding class in your testing package  Select your testing package  From the context menu select New » JUnit Test Case. This opens the New JUnit Test Case Wizard.  Fill in the name of your test case in the Name field.  Select the method stubs that you want Eclipse to generate  This will create the corresponding class in your testing package

Testing » JUnit TestCase Template public class NewTestCase extends TestCase { public static void main(String[] args) { } public NewTestCase(String arg0) { super(arg0); } protected void setUp() throws Exception { super.setUp(); } protected void tearDown() throws Exception { super.tearDown(); } public class NewTestCase extends TestCase { public static void main(String[] args) { } public NewTestCase(String arg0) { super(arg0); } protected void setUp() throws Exception { super.setUp(); } protected void tearDown() throws Exception { super.tearDown(); }

Testing » Adding Tests to Test Cases  Any method in a TestCase class is considered a test if it begins with the word “test”.  You can write many tests (have many test methods)  Each test method should use a variety of assert … methods to perform tests on the state of its class.  Assert methods are inherited  Any method in a TestCase class is considered a test if it begins with the word “test”.  You can write many tests (have many test methods)  Each test method should use a variety of assert … methods to perform tests on the state of its class.  Assert methods are inherited

Testing » JUnit Assert Methods  Assert methods include:  assertEquals(x,y)  assertFalse(boolean)  assertTrue(boolean)  assertNull(object)  assertNotNull(object)  assertSame(firstObject, secondObject)  assertNotSame(firstObject, secondObject)  Assert methods include:  assertEquals(x,y)  assertFalse(boolean)  assertTrue(boolean)  assertNull(object)  assertNotNull(object)  assertSame(firstObject, secondObject)  assertNotSame(firstObject, secondObject)

Testing » Adding Two Tests to JUnit Test Case public class NewTestCase extends TestCase { public static void main(String[] args) { } public NewTestCase(String arg0) { super(arg0); } protected void setUp() throws Exception { super.setUp(); } protected void tearDown() throws Exception { super.tearDown(); } public void testCompareSucceed() { assertEquals(0, 0); // this assertion will succeed } public void testCompareFail() { assertEquals(0, 1); // this assertion will fail } public class NewTestCase extends TestCase { public static void main(String[] args) { } public NewTestCase(String arg0) { super(arg0); } protected void setUp() throws Exception { super.setUp(); } protected void tearDown() throws Exception { super.tearDown(); } public void testCompareSucceed() { assertEquals(0, 0); // this assertion will succeed } public void testCompareFail() { assertEquals(0, 1); // this assertion will fail }

Testing » Running JUnit Test Case  Select TestCase class  From the Run menu select Run » Run As » JUnit Test  This will run the tests in your TestCase class along with the setUp and tearDown methods  You will then get a report in the JUnit window  Select TestCase class  From the Run menu select Run » Run As » JUnit Test  This will run the tests in your TestCase class along with the setUp and tearDown methods  You will then get a report in the JUnit window

Testing » JUnit Window  Red indicates a test has failed  You can see which test failed  You can see the call trace leading to the failure  If you wish to see the tests in TestCase click on the Hierarchy tab  Red indicates a test has failed  You can see which test failed  You can see the call trace leading to the failure  If you wish to see the tests in TestCase click on the Hierarchy tab

Testing » JUnit Window  You can see how many tests ran  How many failures occurred  You can see the details of the failure  Errors occur when exceptions are thrown (e.g., when assertions fail)  You can see how many tests ran  How many failures occurred  You can see the details of the failure  Errors occur when exceptions are thrown (e.g., when assertions fail)