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.

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

Java Testing Tools. junit is a testing harness for unit testing. emma is a code coverage tool. The tools can be used in concert to provide statement and.
Practice Session 5 Java: Packages Collection Classes Iterators Generics Design by Contract Test Driven Development JUnit.
J-Unit Framework.
An Introduction to JUnit Greg Jackson June Software Quality Assurance & Testing 2 Contact Info Northrop Grumman Corp
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.
Unit and Functional Testing with JUnit and Related Tools Greg Barnes University of Washington
Ch. 2 Exploring core JUnit. This chapter covers ■ Using the core JUnit classes ■ Understanding JUnit mechanisms ■ Understanding the JUnit lifecycle.
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
Fall 2007CS 2251 Programming Tools Eclipse JUnit Testing make and ant.
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.
Fall 2007CS 2251 Programming Tools Eclipse JUnit Testing make and ant.
3. A Testing Framework. © O. Nierstrasz P2 — A Testing Framework 3.2 A Testing Framework Overview  What is a framework?  JUnit — a simple testing framework.
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.
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.
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.
A Unit Testing Framework: JUnit COMP 302 Software Engineering Koc University, Istanbul.
JUnitTest Infected: Programmers Love Writing Tests A little test, a little code, a little test, a little code…
Programmer Testing Testing all things Java using JUnit and extensions.
The Design of JUnit Yonglei Tao. Test-First Development  An essential element in eXtreme Programming (XP)  Test is written before the code  As an executable.
Learning JUnit for Unit Testing JUnit Tutorial Dr. Robert L. Probert S.I.T.E., University of Ottawa Sept
© 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.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow
Automated GUI testing How to test an interactive application automatically?
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.
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.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
A tool for test-driven development
Testing and Build JUnit and Ant. JUnit Testing is not closely integrated with development. This prevents you from measuring the progress of development-
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
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.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
OOSC – Lab 1. Welcome Object Oriented programming is first and foremost a method for software construction, whose goal is to build high-quality software.
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.
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.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
JUnit Testing Why we do this and how we can get better.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Test automation / JUnit Building automatically repeatable test suites.
CSE 143 Lecture 14: testing.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
JUnit testing framework
Software Construction Lab 10 Unit Testing with JUnit
Computer Science 209 Testing With JUnit.
Test-first development
Test-driven development (TDD)
Credit to Eclipse Documentation
Introduction to JUnit CS 4501 / 6501 Software Testing
Unit Testing with JUnit
CSE 403 JUnit Reading: These lecture slides are copyright (C) Marty Stepp, They may not be rehosted, sold, or modified without expressed permission.
CSE 143 Lecture 5 More ArrayIntList:
Eclipse JUnit Testing make and ant
Learning JUnit for Unit Testing
Chapter 20: Software Testing - Using JUnit, and Cobertura
JUnit Tutorial Hong Qing Yu Nov 2005.
Presentation transcript:

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 This assumes that you've read a bit about testing; I'm not going to lecture here The heart of this is a test case, a class which contains tests A test is a method that test methods that you've written The framework is found in junit.jar On the CS machines, currently, is located at /usr/share/java/junit.jar

The TestCase class Each test case is a class you provide that extends (inherits from) TestCase Contains: member data special admin-type methods:  setUp() – run before each test  tearDown() – run after each test  suite() – (static), a method that, basically, lists the tests to be added to the framework the tests  methods that take no arguments  often start with or contain the string "Test" (for older versions of the framework)

Example – Money class The files for these examples can be found in: ~kschmidt/public_html/CS265/Labs/Java/Junit MoneyTest written to test class Money

Example – extending TestCase, c'tor import junit.framework.*; public class MoneyTest extends TestCase { public MoneyTest( String name ) { super( name ); }... }

Example - setup Add some Money objects to use in our tests: public class MoneyTest extends TestCase { public MoneyTest( String name ) { super( name ); } private Money m12CHF; protected void setUp() { m12CHF= new Money( 12, "CHF" ); m14CHF= new Money( 14, "CHF" ); }... }

Adding Tests When you implement a method, the test for that method should actually be written first Add a corresponding "test" method to your TestCase class Takes no arguments returns void use various Assert methods to access hooks into the framework (pass/fail, logging)

JUnit: Testcase & Assert To use classes from the JUnit framework (TestCase, Assert, etc): import junit.framework.*

Assert static methods Static methods of the class Assert All are overloaded to take an optional message (a String ) as the first argument assertTrue( boolean condition ) assertFalse( boolean condition ) assertEquals( expected, actual )  overloaded to take any primitives, or anything derived from Object  Note, if subtypes of Object, need to override equals()

Assert methods (cont) assertNull( Object ) assertNotNull( Object ) assertSame( Object expected, Object actual)  Checks to see that expected and actual refer to the same object (so, of course, are equal) assertNotSame( Object expected, Object actual) fail()  Just dumps the testing, w/the optional msg

Example – override equals() We need to provide Money.equals() : public boolean equals( Object anObject ){ if( anObject instanceof Money ) { Money aMoney= (Money)anObject; return aMoney.currency().equals( currency() ) && amount() == aMoney.amount(); } return false; }

Test equals Add to MoneyTest: public void testEquals() { Money expected = new Money( 12, "CHF" ); Assert.assertEquals( expected, m12CHF ); Assert.assertEquals( m12CHF, m12CHF ); Assert.assertNotSame( expected, m12CHF ); Assert.assertFalse( m12CHF.equals( m14CHF )); Assert.assertFalse( expected.equals( m14CHF )); }

Test Money.add() Add to MoneyTest: public void testAdd() { Money expected= new Money( 26, "CHF" ); Money result= m12CHF.add( m14CHF ); Assert.assertEquals( expected, result ); expected = new Money( 1, "CHF" ); result = m12CHF.add( md13CHF ); Assert.assertEquals( expected, result ); result = md13CHF.add( m12CHF ); Assert.assertEquals( expected, result ); }

Identifying tests – suite() You've written some tests They need to be identified to the testing framework Override the suite() static method Create a TestSuite object Use its addTest() method to add tests you want to run

Example – suite() // adding each test // you can just add the tests you want public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest( new MoneyTest( "testEquals" )); suite.addTest( new MoneyTest( "testAdd" )); return suite; }

Adding all tests You can write a suite() method that uses reflection to add all of the testXXX() methods in the TestCase class to the Suite: public static Test suite() { return new TestSuite( MoneyTest.class ); }

Testing your test case As a sanity check, after writing a test or two, you might want to make a little main method for the TestCase class: public static void main( String args[] ){ junit.textui.TestRunner.run( suite() ); } This will set up and run the tests Make sure you didn't make any errors in the testing framework (Remember to add junit.jar to $CLASSPATH to run this from the command line)

Hints (from Prof. Noll at Santa Clara Univ.) Tests should be silent. Do not under any circumstances use System.out.println in any test method. Rather, use assertions. Before you add a method to your production class, think about the pre-conditions and post-conditions for the method: what needs to be in place before the method can be called, and what is supposed to have happened after the method returns? Then, capture the pre-/post-conditions as initialization code and assertions in a unit test method: initialize the pre- conditions, call the method, assert the post-conditions. This accomplishes two things: first, it ensures that you understand what the method is supposed to do before you write it. Second, it provides a test for the method that is available as soon as the method is ready to test.

*See u/~jnoll/174/projects/ junit.html Hints (cont.) (from Prof. Noll at Santa Clara Univ.) When you are tempted to put System.out.println in your production code, instead write a test method. This will help to clarify your design, and increase the coverage of your unit tests. It also prevents ``scroll blindness'', as the tests say nothing until a failure is detected. Don't put System.out.println in your production code (see also above). If you want to do this to observe the behavior of your program, write a unit test to assert its behavior instead. If you need to print to stdout as part of the program's functionality, pass a PrintWriter or output stream to those methods that do printing. Then, you can easily create unit tests for those methods.

Example – build.xml

Ant hook for Junit You can have Ant run these tests, as you write and compile each method Add a target to build.xml depends on the class and your TestCase being compiled Might modify the classpath to include junit.jar Have a jar action that describes the tests you want to run