Download presentation
Presentation is loading. Please wait.
1
JUnit Syed Nabeel
2
Motivation Unit Testing Responsibility of developer Rarely done properly Developers Excuse: “I am too much in a hurry”
3
JUnit to the rescue JUnit is a regression testing framework Used by developers to implement unit tests in Java Goal: Accelerate programming and increase the quality of code.
4
Historical Perspective Created by Kent Beck and Erich Gamma Kent Beck is the creator of Extreme Programming Erich Gamma is popular for co authoring Design Patterns popularly known as Gang of Four and also led the design of the Eclipse Kent came up with a testing framework for unit testing Smalltalk programs-SUnit In 1997 JUnit came into existence based on similar framework as SUnit but targeted for Java
5
Features A Test framework providing tools for: assertions running tests aggregating tests (suites) reporting results
6
Goals of JUnit Approachable Clean Simple Easy-to-use Minimalist Isolated tests Fast Flexible
7
Installation Windows Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. Add JUnit to the classpath: set CLASSPATH=%JUNIT_HOME%\junit.jar Unix (bash) Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME. Add JUnit to the classpath:export CLASSPATH=$JUNIT_HOME/junit.jar Details can be found at http://www.junit.org/index.htm
8
Running JUnit Eclipse includes JUnit Eclipse provides new GUI to run JUnit test cases and suites You can run your unit tests outside of Eclipse Text TestRunner—console based interaction JUnit’s Window—simplistic GUI for JUnit
9
Central classes junit.framework.Assert junit.framework.TestCase junit.framework.TestSuite
10
Basic Testing Unit: Test Expressions (expectedResult==obtainedResult) Without JUnit if (obtainedResult == null || !expectedResult.equals(obtainedResult)) throw new MyTestException(“Failure Message"); With JUnit Assert.assertEquals(“Failure Message",expectedResults, obtainedResults);
11
junit.framework.Assert assertEquals assertFalse assertNotNull assertNotSame assertNull assertSame assertTrue Additional details: http://www.junit.org/junit/javadoc/3.8.1/index.htm
12
junit.framework.TestCase Define a subclass of the abstract class TestCase. Override the setUp() method to initialize object(s) under test. Optionally override the tearDown() method to release object(s) under test. Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results
13
junit.framework.TestSuite A provision for grouping test methods Can span over test methods in different classes Eclipse can automatically make a suit of methods in the same file
14
DEMO
15
Unit Testing best practices During Development: if new functionality is to be added write test first. Development ends when these tests run During debugging: if a defect is detected, first write a test that will succeed if code is working. Debug until test succeeds
16
xUnit Family The same framework is used for unit testing other languages: NUnit C# PyUnit Python fUnit Fortran CPPUnit C++
17
Limitations Does not separate test data from test logic UI Testing is not very easy and direct Performance testing is not incorporated Any other limitations ???
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.