Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, 23 - 24, 2013 SWU, Blagoevgrad.

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

+ Introduction to JUnit IT323 – Software Engineering II By: Mashael Al-Duwais 1.
J-Unit Framework.
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
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
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.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
Approach of Unit testing with the help of JUnit Satish Mishra
Introduction to Eclipse, Unit Testing and JUnit David Rabinowitz.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
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.
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.
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.
© 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 The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Testing in Extreme Programming
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.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
First BlueJ Day Houston, 2006 Unit Testing with BlueJ Bruce Quig Deakin University.
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
Simple Java Unit Testing with JUnit 4 and Netbeans 6.1 Kiki Ahmadi JUG-Bonek.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
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.
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,
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.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
JUnit, Bugzilla James Atlas July 24, 2008 *part of today’s slides courtesy of Dwight Deugo and Nesa Matic under the EPL.
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.
Automated Testing with PHPUnit. How do you know your code works?
Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
CompSci 280 S Introduction to Software Development
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
Unit Testing with JUnit
JUnit Automated Software Testing Framework
Unit testing Java programs Using JUnit
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Junit with.
JUnit Automated Software Testing Framework
History, Characteristics and Frameworks
Selenium HP Web Test Tool Training
Introduction to Testing, SUnit and Error Handling
Credit to Eclipse Documentation
Introduction to JUnit IT323 – Software Engineering II
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.
Presentation transcript:

Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad

3 Lesson Contents: §Unit Testing §Introduction to JUnit §JUnit within/under NetBeans

Unit Testing 4

Categories of Testing 5 User needs Acceptance testing Requirement specification System testing Design Code Integration testing Unit testing

Testing6 Unit Testing §Different modules tested separately §Focus: to detect defects injected during coding §UT is closely associated with coding §Frequently the developer himself does UT §SDM coding phase sometimes called “coding and unit testing” phase

What is Unit Testing?  A Unit Test is a procedure to validate a single Functionality of an application. One Functionality  One Unit Test  Unit Tests are automated and self-checked.  They run in isolation of each other.  They do NOT depend on or connect to external resources, like DB, Network etc.  They can run in any order and even parallel to each other. 7 Impetus Confidential

JUnit – An Introduction - 8

JUnit – An introduction  JUnit is a unit testing framework for Java.  It comes from the family of unit testing frameworks, collectively called xUnit, where ‘x’ stands for the programming language, e.g. NUnit for C#, CPPUnit, JSUnit, PHPUnit, PyUnit, RUnit etc. Kent Beck & Erich Gamma originally wrote this framework for ‘smalltalk’, called SUnit. 9 Impetus Confidential

Why choose JUnit?  JUnit is simple and elegant.  JUnit checks its own results and provide immediate customized feedback.  JUnit is hierarchial.  JUnit has the widest IDE support, Eclipse, NetBeans, BlueJ, ….  JUnit is recognized by popular build tools like, ant, maven etc.  And… it’s free. 10 Impetus Confidential

Design of JUnit 11 Impetus Confidential  junit.framework.TestCase is the abstract command class which you subclass into your Test Classes.  junit.framework.TestSuite is a composite of other tests, either TestCase or TestSuite. This behavior helps you create hierarchal tests with depth control.

Design of JUnit 12 Impetus Confidential Image Courtesy: JUnit: A Cook’s Tour

Write a test case  Define a subclass XXXTest of junit.framework.TestCase public class CalculatorTest extends TestCase { … }  Define one or more testXXX() methods that can perform the tests and assert expected results. public void testAddition(){ Calculator calc = new Calculator(); int expected = 25; int result = calc.add(10, 15); assertEquals(result, expected); // asserting } 13 Impetus Confidential

Write a test case  Each test method has to be structured like this: public void testAddition(){ // declarations Calculator calc = new Calculator(); // preparing input /expected/ data int expected = 25; // computing result /real, actual/ data int result = calc.add(10, 15); // comparing result data to expected data assertEquals(result, expected); // asserting } 14 Impetus Confidential

Write a test case  Override setUp() method to perform protected void setUp() { // Initialize anything, like calc = new Calculator(); }  Override tearDown() method to clean protected void tearDown() { // Clean up, like calc = null; // gc, no need of delete operator } 15 Impetus Confidential

Asserting expectations  assertEquals (expected, actual)  assertEquals (message, expected, actual)  assertEquals (expected, actual, delta)  assertEquals (message, expected, actual, delta)  assertFalse ((message)condition)  Assert(Not)Null (object)  Assert(Not)Null (message, object)  Assert(Not)Same (expected, actual)  Assert(Not)Same (message, expected, actual)  assertTrue ((message), condition) 16 Impetus Confidential

Failure?  JUnit uses the term failure for a test that fails expectedly. That is  An assertion was not valid or  A fail() was encountered. 17 Impetus Confidential

Write a test case public class CalculatorTest extends TestCase { // initialize protected void setUp()...S public void testAddition()... T1 public void testSubtraction()...T2 public void testMultiplication()...T3 // clean up protected void tearDownUp()...D } Execution will be S T1 D, S T2 D, S T3 D in any order. 18 Impetus Confidential

Write a test suite Write a class with a static method suite() that creates a junit.framework.TestSuite containing all the Tests. public class AllTests { public static Test suite() { TestSuite suite = new TestSuite(); suite.addTestSuite(.class); return suite; } 19 Impetus Confidential

Run your tests  You can either run TestCase or TestSuite instances.  A TestSuite will automatically run all its registered TestCase instances.  All public testXXX() methods of a TestCase will be executed. But there is no guarantee of the order. 20 Impetus Confidential

Run your tests  JUnit comes with TestRunners that run your tests and immediately provide you with feedbacks, errors, and status of completion.  JUnit has Textual and Graphical test runners.  Textual Runner >> java junit.textui.TestRunner AllTests or, junit.textui.TestRunner.run(AllTests.class);  Graphical Runner >> java junit.swingui.TestRunner AllTests or, junit.swingui.TestRunner.run(AllTests.class);  IDE like Eclipse, NetBeans “Run as JUnit” 21 Impetus Confidential

Mocks  Mocking is a way to deal with third-party dependencies inside TestCase.  Mocks create FAKE objects to mock a contract behavior.  Mocks help make tests more unitary. 22 Impetus Confidential

How to approach? 23 Impetus Confidential  Test a little, Code a little, Test a little, Code a little … doesn’t it rhyme? ;)  Write tests to validate functionality, not functions.  If tempted to write System.out.println() to debug something, better write a test for it.  If a bug is found, write a test to expose the bug.

JUnit – within/under NetBeans - .. 24 Impetus Confidential

25 JUnit within/under NetBeans §Practical Manual: §Practical conventions §How to build unit tests? §How to run unit tests? §Demo task §Practical exercise task

26 Practical conventions §Each production code class X has a test code class Xtest derived from TestCase class §Each method y(){…} from class X has a method testy(){…} from Xtest class §Each test code class Xtest has setUp() method and tearDown() method

27 Practical conventions §All production code located in a separate project folder Source Packages §All test code located in another separate project folder Test Packages §The skeletal structure and mostly the contents of a test code class Xtest and test code methods generated automatically

28 How to build unit tests? Part 1 §Create New project §Add Java class with main() method §Add user defined class to be tested l as part of file with Java main() class or l as a separate file within the same package. §Build the project and run it as a regular Java application Run > Run Main project (F6)

29 How to run unit tests? Part 2a §Select Java class to be tested (from Projects…) §Click the right mouse button and invoke Tools > Create Tests §Modify the generated text if necessary §Re-Build the project and run it as a JUnit test Run > Test Project(…) (Alt+F6) §Analyze the test results

30 How to run unit tests? OR (follow option 2b)

31 How to run unit tests? Part 2b §Add a new File to the project File > new File… §Select category: Unit Test §Select File Type: Test for Existing Class §You have to specify the class to be tested §You have to select JUnit version: 3 §The test class automatically generated §Modify the generated text if necessary §Re-Build the project and run it as a JUnit test Run > Test Project(…) (Alt+F6) §Analyze the test results

32 Demo Task §NetBeans project: SBJUnitTestClassLibrary

33 Demo Task §NetBeans project: SBJUnitDistance01

34 Practical Exercise Task §Expand project SBJUnitTestClassLibrary l Develop a class Power to implement arithmetic power operator with double base and integer exponent in two versions – iterative and recursive l Iterative method: double powi(double base, int n){ } l Recursive method: double powr(double base, int n){ } l Run the powx methods regular way l Run the powx methods using Junit Create PowerTest class Create testpowi() method and testpowr() method Run JUnit

35 Practical Exercise Task §Expand project SBJUnitDistance01 l Develop a class Counter to implement a counter as a general purpose programming component with a count data item and methods void incCount() and method void decCount() l Run the Counter class regular way l Run the Counter class methods using Junit Create CounterTest class Create testincCount() method and testdecCount() method Run JUnit

36 Questions? And/Or Thank You For Your Attention!