180 likes | 196 Views
JUnit. Syed Nabeel. Motivation. Unit Testing Responsibility of developer Rarely done properly Developers Excuse: “I am too much in a hurry”. JUnit to the rescue. JUnit is a regression testing framework Used by developers to implement unit tests in Java
E N D
JUnit Syed Nabeel
Motivation • Unit Testing • Responsibility of developer • Rarely done properly • Developers Excuse: “I am too much in a hurry”
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.
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
Features A Test framework providing tools for: • assertions • running tests • aggregating tests (suites) • reporting results
Goals of JUnit • Approachable • Clean • Simple • Easy-to-use • Minimalist • Isolated tests • Fast • Flexible
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
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
Central classes • junit.framework.Assert • junit.framework.TestCase • junit.framework.TestSuite
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);
junit.framework.Assert • assertEquals • assertFalse • assertNotNull • assertNotSame • assertNull • assertSame • assertTrue Additional details: http://www.junit.org/junit/javadoc/3.8.1/index.htm
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
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
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
xUnit Family The same framework is used for unit testing other languages: • NUnitC# • PyUnitPython • fUnitFortran • CPPUnitC++
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 ???