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.

Slides:



Advertisements
Similar presentations
Design By Contract Using JMSAssert.
Advertisements

JUnit Tutorial Hong Qing Yu Nov JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices.
J-Unit Framework.
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.
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, Revisited 17-Apr-17.
Fall 2007CS 2251 Programming Tools Eclipse JUnit Testing make and ant.
JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.
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.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
Eclipse IDE. 2 IDE Overview An IDE is an Interactive Development Environment Different IDEs meet different needs BlueJ and DrJava are designed as teaching.
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 & 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:
INF 111 / CSE 121 Discussion Session Week 2 - Fall 2007 Instructor: Michele Rousseau TA: Rosalva Gallardo.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
Testing with Android Part I of II. Android Testing Framework Based on JUnit The Android JUnit extensions provide component-specific test case classes.
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.
ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.
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.
JUnit & Eclipse1 DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY Feb 2, 2009 revision 1.2 – Feb 2, 2009 by Emil Vassev & Joey.
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.
Welcome To Eclipse. Basic Concepts Workspace – directory where your projects are stored –Projects grouping of related works –Resources generic term to.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
JUnit Dwight Deugo Nesa Matic
Installing Repast in the Eclipse IDE Charlie Gieseler 6/28/04.
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
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
JUnit SWE 619 Summer July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable.
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,
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.
Time to apply stuff… Faculty of Mathematics and Physics Charles University in Prague 5 th October 2015 Workshop 1 – Java Wrestling.
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,
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Test automation / JUnit Building automatically repeatable test suites.
Java IDE Dwight Deugo Nesa Matic
Introduction to Unit Testing and JUnit David Rabinowitz.
Software Construction Lab 10 Unit Testing with JUnit
ATS Application Programming: Java Programming
Software Engineering 1, CS 355 Unit Testing with JUnit
Overview of Eclipse Lectures
Test-first development
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:

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 written? When  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.  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be written? When  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.

3 Testing at the Boundaries  What does this mean?  Example (from Dr. Jo Perry) 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 Dr. Jo Perry) 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

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

5 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();  Suppose we have a MyStack class implemented in an integer array.  MyStack(int n);  void push(int i);  int pop();  int top();

6 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();  Suppose we have a MyStack class implemented in an integer array.  MyStack(int n);  void push(int i);  int pop();  int top();

7 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();  Suppose we have a MyStack class implemented in an integer array.  MyStack(int n);  void push(int i);  int pop();  int top(); 17 9

8 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? What test cases  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? What test cases 17

9 What is JUnit?  An open-source regression-testing framework for Java  JUnit’s web site:  Eclipse includes JUnit  However, you need to set it up inside each project.  It allows you to define regression tests for your code and run them from the Java IDE.  An open-source regression-testing framework for Java  JUnit’s web site:  Eclipse includes JUnit  However, you need to set it up inside each project.  It allows you to define regression tests for your code and run them from the Java IDE.

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

11 Testing » Setting up JUnit  JUnit is in Eclipse by default, but needs to be applied to a project by performing a few actions.  First, right-click on the name of the package.  JUnit is in Eclipse by default, but needs to be applied to a project by performing a few actions.  First, right-click on the name of the package.

12  From the popup menu, select “Build Path”; then “Configure Build Path” Testing » Setting up JUnit, cont.

13  Then select “Add Library” and then “JUnit”. Testing » Setting up JUnit, cont.

14  Then, when asked which version, just click on “Finish”. Testing » Setting up JUnit, cont.

15 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.

16 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

17 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(); }

18 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.  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.

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

20 Testing » Example of a Test Case package stackTests; import junit.framework.TestCase; import stack.MyStack; public class Tests extends TestCase { protected void setUp() throws Exception { super.setUp(); } protected void tearDown() throws Exception { super.tearDown(); } public void testPush() { MyStack = new MyStack(5); m.push(27); assertEquals(m.pop(), 27); } package stackTests; import junit.framework.TestCase; import stack.MyStack; public class Tests extends TestCase { protected void setUp() throws Exception { super.setUp(); } protected void tearDown() throws Exception { super.tearDown(); } public void testPush() { MyStack = new MyStack(5); m.push(27); assertEquals(m.pop(), 27); }

21 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

22 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  Red indicates a test has failed  You can see which test failed  You can see the call trace leading to the failure

23 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)

24 Testing » Exercise  Go through the Stack class and add additional unit tests.  The stack class is at  /csc216/lec/001/exercises/stack.java /csc216/lec/001/exercises/stack.java  Submit one of your test cases here. Submit one  Go through the Stack class and add additional unit tests.  The stack class is at  /csc216/lec/001/exercises/stack.java /csc216/lec/001/exercises/stack.java  Submit one of your test cases here. Submit one

25 Important terms  Black-box testing  White-box testing  Regression testing  Test coverage  Black-box testing  White-box testing  Regression testing  Test coverage