JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.

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

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.
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.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
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
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.
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.
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.
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.
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.
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…
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:
Junit Training Chris Yeung 8 th Sept, Introduction JUnit is a regression testing frameworkJUnit is a regression testing framework Written by Erich.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Unit Testing.
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.
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
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
Testing and Build JUnit and Ant. JUnit Testing is not closely integrated with development. This prevents you from measuring the progress of development-
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Program Testing, Debugging and JUnit. Program Development Process A simplified process –User requirement  Development  Delivery A simplified process.
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.
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.
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 / JUnit Building automatically repeatable test suites.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Advanced programming Practices
Software Construction Lab 10 Unit Testing with JUnit
Introduction to JUnit CS 4501 / 6501 Software Testing
Computer Science 209 Testing With JUnit.
Test-driven development (TDD)
Introduction to JUnit CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
Introduction to JUnit IT323 – Software Engineering II
Advanced programming Practices
Learning JUnit for Unit Testing
TCSS 360, Spring 2005 Lecture Notes
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.
Chapter 20: Software Testing - Using JUnit, and Cobertura
JUnit Tutorial Hong Qing Yu Nov 2005.
Presentation transcript:

JUnit Introduction and Advanced Features

Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

JUnit Introduction  JUnit is a Unit Testing Tool  JUnit is a regression testing framework  Programmed by Erich Gamma and Kent Beck  Open Source –Hosted on SourceForge – – –  Used by Java programmer

JUnit Framework

junit How to Test with JUnit? Student TestCase exercise 1..* TestRunner run 1..* TestStudent test 1 test 2 …

Currency Example class Money { private int fAmount; private String fCurrency; public Money(int amount, String currency) { fAmount= amount; fCurrency= currency; }

Currency (cont) public int getAmount() { return fAmount; } public String getCurrency() { return fCurrency; } public Money add(Money m) { return new Money(amount()+m.amount(), currency()); } }// Class Money Ends

Test Expression without JUnit public class MoneyTest { public static void main(String[] args){ Money m10USD=new Money(10,”USD”); Money m20USD=new Money(20,”USD”); Money result = m10USD.add(m20USD); if (!(result.amount() == 30 && result.currency() == “USD”)) System.out.println(“add failed!”); } }// Class Money Test ends

Using Assert method import junit.framework.*; public class MoneyTest extends TestCase{ public void testAdd { Money m10USD=new Money(10,”USD”); Money m20USD=new Money(20,”USD”); Money result = m10USD.add(m20USD); Assert.assertEquals(result, (new Money(30, “ USD")); }

More Assert Methods  static void assertTrue(boolean test ) static void assertTrue(String message, boolean test ) Asserts that the value is true  assertNull(Object object ) assertNull(String message, Object object ) Asserts that the object is null  assertEquals( expected, actual ) assertEquals(String message, expected, actual ) –This method is heavily overloaded: expected and actual must be both objects or both of the same primitive type mework/Assert.html

Over-riding Equals in our example public boolean equals(Object anObject) { if (anObject instanceof Money) { Money aMoney= (Money)anObject; return aMoney.currency().equals(currency()) && amount() == aMoney.amount(); } return false; }

Testing equals (TestCase) import junit.framework.*; public class TestMoney extends TestCase { public TestMoney (String name) { super (name); } public void testEquals(){ Money m10USD=new Money(10,”USD”); Money m20USD=new Money(20,”USD”); Money result = m10USD.add(m20USD); assertTrue(result. equals(new Money(30, “USD")); } //other test methods of the form testGet, testSet, etc }

Test Fixture  What if you have two or more tests that operate on the same or similar sets of objects?  Tests need to run against background of a known set of objects.  This set of objects is called a test fixture.

Test Fixture (Cont.)  Steps of using Test Fixture –Create a subclass of TestCase –Create a constructor which accepts a string as a parameter and passes it to the superclass. –Add an instance variable for each part of fixture –Override setUp() to initialize the variables –Override tearDown() to release any permanent resources you allocated in setUp.

junit Foo TestCase exercise 1..* TestRunner run 1..* FooTest test 1 test 2 … setUp () tearDown ()

Test Fixture Example public class MoneyTest extends TestCase { private Money m10USD; private Money m20USD; public MoneyTest (String s) { super(s); } protected void setUp() { m10USD=new Money(10,”USD”); m20USD=new Money(20,”USD”); } protected void teardDown() throws Exception { } // other testX methods here }

Test Suite  A TestSuite is a collection of test cases. Two ways of invoking the tests The simple way is public static Test suite() { return new TestSuite(TestStudent.class); }

Test Suite (Cont.) 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() { testAdd(); } } ); return suite; }

junit TestSuite MyClass TestCase exercise 1..* TestRunner run 1..* MyClassTest test 1 test 2 … test case TestSuite test suite

TestRunner

JUnit Rules and Conventions  Subclass TestCase  Test methods –public void testXXX() [throws …] –Any number of assertions per method  Implement main to run from command-line, but not necessary  Optionally add setUp / tearDown methods.  Add a suite method if you want to create a test suite

Terminology  A test case tests a particular method of you class. (Eg testEquals, testGet, testSet in our example)  A test suite is a collection of test cases  A test runner is software that runs tests and reports results

Questions? vinubalaji at gmail dot com