Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.

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

Practice Session 5 Java: Packages Collection Classes Iterators Generics Design by Contract Test Driven Development JUnit.
J-Unit Framework.
Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
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.
Ch. 2 Exploring core JUnit. This chapter covers ■ Using the core JUnit classes ■ Understanding JUnit mechanisms ■ Understanding the JUnit lifecycle.
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.
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.
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.
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.
JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.
Programmer Testing Testing all things Java using JUnit and extensions.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Recitation 7 James Wei Professor Peck 2/28/2014. Covered in this Recitation LinkedList practice with JUnit testing Submit through ambient.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
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.
Intoduction to Unit Testing Using JUnit to structure Unit Testing SE-2030 Dr. Rob Hasker 1 Based on material by Dr. Mark L. Hornick.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
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.
Recursion. Midterm Exam Wednesday,November 4, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices Simple problem solving.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
1.  Writing snippets of code that try to use methods (functions) from your program.  Each snippet should test one (and only one) function......by calling.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
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 Dwight Deugo Nesa Matic
A tool for test-driven development
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. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
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,
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 with FlexUnit
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
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,
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
JUnit Testing Why we do this and how we can get better.
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.
Automated Testing with PHPUnit. How do you know your code works?
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Software Construction Lab 10 Unit Testing with JUnit
Unit Testing with JUnit
Computer Science 209 Testing With JUnit.
JUnit Automated Software Testing Framework
CMPE212 – Stuff… Assn 2 due this Friday. Winter 2018
Credit to Eclipse Documentation
Introduction to JUnit CS 4501 / 6501 Software Testing
Introduction to JUnit IT323 – Software Engineering II
CSE 143 Lecture 5 More ArrayIntList:
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.
Presentation transcript:

Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at The Ohio State University. The slides have been modified a bit for use at Clemson by Professors Murali Sitaraman and Jason Hallstrom.

Computer Science and Engineering The Ohio State University To Ponder  Why are exams a good thing?

Computer Science and Engineering The Ohio State University Testing  Testing helps increase our confidence in our code “If it isn’t tested, assume it doesn’t work”  Testing is a comparison: Expected behavior of the component  See Javadoc description and/or formal specification Actual behavior of the component  Run the code  Three parts: Implementation, specification, test cases  Some believe in test-driven development Write tests first! Then write code so that all tests compile Then refine code so that all tests pass Repeat: write more tests, refine code so they pass

Computer Science and Engineering The Ohio State University Writing Good Tests  Goal: to expose problems! Assume role of an adversary Failure == success  Test boundary conditions e.g. 0, Integer.MAX_VALUE, empty array  Test different categories of input e.g. positive, negative, and zero  Test different categories of behavior e.g. each menu option, each error message  Test “unexpected” input e.g. null pointer, last name includes a space  Test representative “normal” input e.g. random, reasonable values

Computer Science and Engineering The Ohio State University Primitive Testing: println  Console IO to observe actual behavior  Compare IO with expected output  Advantages: Testing code is simple, easy, intuitive  Problems: Exhaustive testing means lots of output Comparison is tiresome and error-prone Difficult to automate

Computer Science and Engineering The Ohio State University Serious Testing: JUnit  A “framework” for testing Java code Frameworks are libraries with gaps Programmer writes classes following particular conventions to fill in gaps Result is the complete product  Current version of JUnit: 4 (4.4) Big changes from JUnit 3.8 Most information available online is about 3.8, the version used in class

Computer Science and Engineering The Ohio State University Example: TestStackImpl1 import junit.framework.*; public class TestStackImpl1 extends TestCase { private Stack s; //coding to the interface public void testPush() { s = new StackImpl1(); s.push(“A”); assertEquals(“ ”, s.toString()); } public void testClear() { s = new StackImpl1(); s.push(“A”); s.push(“B”); s.clear(); assertEquals(“<>”, s.toString()); }

Computer Science and Engineering The Ohio State University Vocabulary  Test case Exercises a single unit of code / behavior / functionality Test cases should be small (i.e. test one thing) Test cases should be independent In JUnit: A public method beginning with “test” in a class derived from TestCase  Test fixture Exercises a single class A collection of test cases In JUnit: A class derived from TestCase with multiple methods beginning with “test”  Test suite Exercises all the classes in a program A collection of test fixtures In JUnit: Developed using the TestSuite class (not discussed in class)

Computer Science and Engineering The Ohio State University Execution Model: Multiple Instances testPush() testClear() TestSI1() p testPush() testClear() TestSI1() p TestStackImpl1

Computer Science and Engineering The Ohio State University Execution Model: Implications  Separate instances of test class created One instance / test method  Do not use test cases with side effects Passing or failing one test case should not affect the others  Fixture: common set-up to all test cases Fields for instances of class being tested Factor initialization code into its own method Override setUp() from TestCase

Computer Science and Engineering The Ohio State University Good Practice: setUp()  Initialize a fixture with a setup method rather than the constructor  Reason: If the code being tested throws an exception during the setup, the output is much more meaningful

Computer Science and Engineering The Ohio State University Example: TestStackImpl1 import junit.framework.*; public class TestStackImpl1 extends TestCase { private Stack s; //coding to the interface public void setUp() { s = new StackImpl1(); s.push(“A”); } public void testPush() { assertEquals(“ ”, s.toString()); } public void testClear() { s.push(“B”); s.clear(); assertEquals(“<>”, s.toString()); }

Computer Science and Engineering The Ohio State University Execution Model testPush() testClear() TestSI1() p testPush() testClear() TestSI1() p TestStackImpl1 2 2 setUp() 1 1

Computer Science and Engineering The Ohio State University Assertions  Different kinds of tests assertEquals (message, expected, actual); assertTrue (message, condition); assertFalse (message, condition); assertNull (message, object); assertNotNull (message, object);  Note: message argument not discussed in class --- but very useful!

Computer Science and Engineering The Ohio State University Good Practice: assertEquals  Prefer assertEquals to assertTrue assertEquals is overloaded  Expected and actual can be primitives or references Failed test case produces useful output junit.framework.AssertionFailedError: Age at birth expected: but was: Compare with assertTrue junit.framework.AssertionFailedError: Age at birth  Use 3-argument version (not discussed in class) 1 st argument: String to display on failure assertEquals(String msg, int expected, int actual)

Computer Science and Engineering The Ohio State University Good Practice: Comparing Floats  Never compare floating point numbers directly for equality assertEquals(“Low-density experiment”, 1.456, calculated); Numeric instabilities make exact equality problematic  Better approach: Equality with tolerance assertEquals(“Low-density experiment”, 1.456, calculated, 0.001);

Computer Science and Engineering The Ohio State University Eclipse Demo  New > JUnit Test Case  First screen of wizard: Checkbox “New JUnit 3 Test” Enter name of test class (e.g. TestThing) Enter name of “class under test” (e.g. Thing) If warning “JUnit 3 not on build path” appears, click link to add it to build path  Second screen of wizard: Select methods to test Generates one test case / selected method But you will need many more than that  To run, Run As… > JUnit Test Case

Computer Science and Engineering The Ohio State University Specification vs Implementation  Tests can be written for either Specification tests test only behavior promised in Javadoc (or specification) of interface Implementation tests test all behavior documented in Javadoc (or specification) of class  Examples: Interface does not guarantee order of elements in a returned array, but implementation always has them in sorted order  Specification tests work for all (correct) classes implementing the given interface

Computer Science and Engineering The Ohio State University Good Practice: Organization  Keep test classes in the same project as the code They are part of the build Helps to keep tests current  Name test classes consistently e.g. TestWritingStick tests WritingStick  Group tests in same package, but different source folder as the code e.g. project X9, package cu.cs:  Code: X9/src/cu/cs/WritingStick.java  Tests: X9/test/cu/cs/TestWritingStick.java Tests can see public and package-visible stuff

Computer Science and Engineering The Ohio State University Supplemental Reading  JUnit web site See “Getting Started”  JUnit FAQ  JUnit cookbook ok.htm  IBM developerWorks “An Early Look at JUnit 4” ibm.com/developerworks/java/library/j- junit4.html Assumes JUnit 3.8 background

Computer Science and Engineering The Ohio State University Summary  Nature of testing Specification, implementation, test cases  JUnit overview Test case: method beginning with “test” Test fixture: class collecting common tests Test suite: set of fixtures Assertions  Execution model Multiple instantiation of test class Independence of test cases