A Unit Testing Framework: JUnit www.junit.org COMP 302 Software Engineering Koc University, Istanbul.

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.
1 Unit Testing with JUnit CS 3331 Fall 2009 Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report, 3(7):37-50, Available.
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.
An Introduction to JUnit Greg Jackson June Software Quality Assurance & Testing 2 Contact Info Northrop Grumman Corp
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
3. A Testing Framework. © O. Nierstrasz P2 — A Testing Framework 3.2 A Testing Framework Overview  What is a framework?  What is an Annotation?  JUnit.
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.
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.
20-Jun-15 More About JUnit. Test suites A test suite is a group of JUnit tests You can create a test suite in Eclipse as follows: File  New  Other...
3. A Testing Framework. © O. Nierstrasz P2 — A Testing Framework 3.2 A Testing Framework Sources  JUnit documentation (from
JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.
Exemplo de desenvolvimento com testes Prof. Dr. Alfredo Goldman Departamento de Ciência da Computação IME / USP 3 de Abril de 2003 VI Semana da Computação.
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.
3. A Testing Framework. © O. Nierstrasz P2 — A Testing Framework 3.2 A Testing Framework Sources  JUnit 4.0 documentation (from
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.
JUnitTest Infected: Programmers Love Writing Tests A little test, a little code, a little test, a little code…
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
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Chapter 11, Testing.
1 Code Quality, Maintainability, Reusability, Debugging, Testing SIF8080, Sep. 27th 2001 Customer-driven project Carl-Fredrik Sørensen
Unit Testing Bartosz Walter Software Engineering Lecture XXX.
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.
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.
Software Testing, Debugging and JUnit Ananda Gunawardena Hao Cen.
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.
JUnit Dwight Deugo Nesa Matic
ESO - Garching 23 June – 02 July, 2003 ACS Course JUnit for Java Unit Testing H. Sommer.
Chapter 11, Testing: Unit Testing with JUnit 4
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
CSC 480 Software Engineering Lecture 15 Oct 21, 2002.
JUnit Dwight Deugo Nesa Matic
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-
Program Testing, Debugging and JUnit. Program Development Process A simplified process –User requirement  Development  Delivery A simplified process.
JUnit Don Braffitt Updated: 10-Jun-2011.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
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.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 11, Testing.
Introduction to Software Testing
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.
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 Testing CSSE 514 Programming Methods 4/19/01.
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.
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 For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Software Construction Lab 10 Unit Testing with JUnit
Introduction to JUnit CS 4501 / 6501 Software Testing
Unit testing Java programs Using JUnit
Computer Science 209 Testing With JUnit.
null, true, and false are also reserved.
Chapter 11, Testing.
Learning JUnit for Unit Testing
Joel Adams and Jeremy Frens Calvin College
Shane Cantrell Zach Crisman
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:

A Unit Testing Framework: JUnit COMP 302 Software Engineering Koc University, Istanbul

JUnit2 The Money class to be tested class Money { private int fAmount; private String fCurrency; public Money(int amount, String currency) { fAmount= amount; fCurrency= currency; } public int amount() { return fAmount; } public String currency() { return fCurrency; } public Money add(Money m) { return new Money(amount()+m.amount(), currency()); }

JUnit3 A Simple TestCase public class MoneyTest extends TestCase { //… protected void runTest() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); Money expected= new Money(26, "CHF"); Money result= m12CHF.add(m14CHF); Assert.assertTrue(expected.equals(result)); } Junit API

JUnit4 Scenarios  Used to test A whole object Part of an object - a method or some interacting methods Interaction between several objects  A test case represents one scenario A tester class contains more than one scenario Each senario is written into one test method

JUnit5 A Simple TestCase public class MoneyTest extends TestCase { //… public void testEquals() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); Assert.assertTrue(!m12CHF.equals(null)); Assert.assertEquals(m12CHF, m12CHF); Assert.assertEquals(m12CHF, new Money(12, "CHF")); Assert.assertTrue(!m12CHF.equals(m14CHF)); }

JUnit6 Overriding “equals()” method public boolean equals(Object anObject) { if (anObject instanceof Money) { Money aMoney= (Money)anObject; return (aMoney.currency().equals(currency())) && (amount() == aMoney.amount()); } return false; }

JUnit7 Decomposing tests into methods public class MoneyTest extends TestCase { public void testSimpleAdd() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); Money expected= new Money(26, "CHF"); Money result= m12CHF.add(m14CHF); Assert.assertTrue(expected.equals(result)); } public void testEquals() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); Assert.assertTrue(!m12CHF.equals(null)); Assert.assertEquals(m12CHF, m12CHF); Assert.assertEquals(m12CHF, new Money(12, "CHF")); Assert.assertTrue(!m12CHF.equals(m14CHF)); }

JUnit8 Using “setUp” to share objects across test methods public class MoneyTest extends TestCase { private Money f12CHF; private Money f14CHF; protected void setUp() { f12CHF= new Money(12, "CHF"); f14CHF= new Money(14, "CHF"); } protected void tearDown() throws java.lang.Exception {... }

JUnit9 Test methods using shared objects public void testEquals() { Assert.assertTrue(!f12CHF.equals(null)); Assert.assertEquals(f12CHF, f12CHF); Assert.assertEquals(f12CHF, new Money(12, "CHF")); Assert.assertTrue(!f12CHF.equals(f14CHF)); } public void testSimpleAdd() { Money expected= new Money(26, "CHF"); Money result= f12CHF.add(f14CHF); Assert.assertTrue(expected.equals(result)); }

JUnit10 Running a TestCase 1 public class MoneyTest extends TestCase { // Override runTest and define the method to be invoked protected void runTest() {... } // Running TestCase test = new MoneyTest(“adding test”); test.run();

JUnit11 Running a TestCase 2 public class MoneyTest extends TestCase { //… public void testSimpleAdd() {... } //… public void testEquals() {... } // Running TestCase test = new MoneyTest("simple add and equals"){ public void runTest(){ testSimpleAdd(); testEquals(); } }; test.run();

JUnit12 Running a TestCase 3 public class MoneyTest extends TestCase { //… public void testSimpleAdd() {... } //… public void testEquals() {... } // Running TestCase test = new MoneyTest(“testSimpleAdd”); test.run();... TestCase test = new MoneyTest(“testEquals”); test.run();

JUnit13 Running more than one tests: Test Suites public class MoneyTest extends TestCase { public void testSimpleAdd() {...} public void testEquals() {...} public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest( new MoneyTest("money equals") { protected void runTest() { testEquals(); } } ); suite.addTest( new MoneyTest("simple add") { protected void runTest() { testSimpleAdd(); } } ); return suite; }

JUnit14 Test Suites 2 public class MoneyTest extends TestCase { public void testSimpleAdd() {...} public void testEquals() {...} public static Test suite(){ TestSuite suite= new TestSuite(); suite.addTest(new MoneyTest("testEquals")); suite.addTest(new MoneyTest("testSimpleAdd")); return suite; }

JUnit15 Running Test Suites MoneyTest test = new MoneyTest(“tests”); TestSuite suite = test.suite(); suite.run();

JUnit16

JUnit17 Testing Hints  During Development: When you need to add new functionality to the system, write the tests first. Then, you will be done developing when the test runs.  During Debugging: When someone discovers a defect in your code, first write a test that will succeed if the code is working. Then debug until the test succeeds.

JUnit18

JUnit19