Download presentation
Presentation is loading. Please wait.
Published byHoward Morris Modified over 9 years ago
1
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University
2
S Ramakrishnan2 JUnit Eric Gamma and Kent Beck created a simple & effective unit testing framework for Java called Junit in 1997. Junit in 1997. z part of XP (Extreme Programming method) but can be used independently. but can be used independently. zJunit (Framework) provides a reusable structure which can be shared between apps. which can be shared between apps. z Developers can incorporate this framework into their apps. and extend it to meet their specific their apps. and extend it to meet their specific requirements. requirements.
3
S Ramakrishnan3 JUnit Junit (junit.org) is an open source software and is hosted on SourceForge. z defacto standard framework for developing unit tests in Java. z many unit testing frameworks available: Eiffel, PhP, Perl, ASP, C++, C# and …
4
S Ramakrishnan4 JUnit A typical unit test: Check that a method accepts input in an expected range & returns the expected result value for each test input. z test the behaviour of a method through its interface. Test for expected values, also for exception z check if the method obeys the design by contract principles in terms of API contract.
5
S Ramakrishnan5 Unit testing frameworks z A simple Calculator class: z The core method, add: takes 2 doubles & returns the sum as a double public class Calculator { public double add(double num1, double num2) { return num1 + num2; } z Put in some work to test this simple “unit of work” will do the adding 2 doubles & returning the sum as a double correctly. And, also preserve this effort so that you can replicate this test and run the tests again later! z Objective test cases turned into reusable test cases -> Test Program
6
S Ramakrishnan6 Unit testing frameworks z A simple TestCalculator program – manual test public class TestCalculator { public static void main(String[ ] args) { Calculator calculator = new Calculator ( ); double result = calculator.add(10, 20); if (result != 30) { System.out.println(“Incorrect result: “ + result); } zThe test will pass when you compile & run this test program. If you change this code deliberately to make it fail, you are not testing the code! You should handle error conditions in Java by throwing exceptions.
7
S Ramakrishnan7 Unit testing frameworks z A simple TestCalculator program - Modified public class TestCalculator { private int errCount = 0; public void testAdd ( ); // moving the test into its own method { Calculator calculator = new Calculator ( ); double result = calculator.add(10, 20); if (result != 30) { throw new RuntimeException(“Incorrect result: “ + result); } public static void main (String [ ] args) { TestCalculator test = new TestCalculator ( ); try { test.testAdd ( ); // can add more methods like testAdd with more unit tests in the test calculator & invoke them here } catch (Throwable e) { test.errCount++; e.printStackTrace ( ); } if (test.errCount > 0) { throw new RuntimeException(“Number of Errors : “ + test.errCount “ ); }
8
S Ramakrishnan8 Unit testing frameworks z Lessons from the last simple example re: unit testing frameworks: Each unit test must run independently of all other unit tests must be easy to define which unit tests will run errors must be detected and reported for each test separately. The modified vers.(last slide) can be improved. Each unit test must be independent & run in a different classloader instance.
9
S Ramakrishnan9 Unit testing frameworks z A better option is to consider a unit test suite. problem with this approach: large try/catch block can be a maintenance headache Another approach is to use Java’s reflection & introspection features. A program looks at itself & decide to run methods which are named in a certain way, eg. those begin with letters, test. The Junit framework supports introspecting methods, supports the use of a different classloader instance for each test and reports errors for each test separately.
10
S Ramakrishnan10 Testing with JUnit Create a test script with a number of small java methods the idea is to create java objects, do something with these and check if the objects exhibit correct properties Assertions methods to check properties such as identity of objects, equality of variables can check if objects are null or non-null; equality of objects (via == or equals () depending on the type) use assertions to determine the verdict of the test case. In XP methodology, a JUnit test should be written first before any code, and executed. the implementation code should be written to get the test to pass re-execute the test with this code and it should pass.
11
S Ramakrishnan11 Junit TestCase (Usually, a test case is considered to be a single test that can pass or fail, and a testsuite is a bunch of related testcases). In Junit, run multiple test cases withTestSuite & Testcase. They are implemented as classes. No difference between running a testsuite & a testcase. A TestCase is a class and a single test is a method. TestCalculator program written with Junit import junit.framework.TestCase; // Junit needs it to automatically run the tests public class TestCalculator extends TestCase; { public void testAdd ( ); // method name follows the pattern testXXX { Calculator calculator = new Calculator ( ); // start the test by creating an instance (object under test) double result = calculator.add(10, 20); // execute the test by calling the method to test, passing it known values assetEquals ( 30, result, 0); // check the result by calling assertEquals method, inherited from base class, TestCase. } Javadoc for assertEquals method /** *Asserts that two doubles are equal concerning a delta. … */ Static public void assertEquals (double expected, double actual, double delta) In the above code, assertEquals is passed: expected = 30, actual = result, delta = 0 Often, delta parameter can be zero and can be ignored. It is used when floating point calc. are done. Delta provides a + / - factor. If the actual data is within the range of expected +/- delta, the test passes.
12
S Ramakrishnan12 Junit TestCase public void run(TestResult result) // run method in TestCase Class { result.startTest (this); setUp ( ); // set up for all tests in class try { runTest ( ); // run the test } catch (AssertionFailedError e) { result.addFailure (this, e); } catch (Throwable e) { result.addError (this, e); } finally { tearDown ( ); // cleanup for all tests in class }
13
S Ramakrishnan13 The Junit Framework TestResult Test TestCaseTestSuite yourTestClass-1 yourTestClass-n run(TestResult) runTest ( ) setup ( ) tearDown ( ) run(TestResult) fName fTests *suite ( ): TestSuite
14
S Ramakrishnan14 Junit workings Junit uses reflection to build the testsuite dynamically use getClass ( ) on an object to know which class it belongs to use getMethods ( ) to find out the methods in a class use invoke ( ) on each method to run it This ensures that test suite need not be updated if a method is added or deleted from the test suite. Test class is re loaded each time the tests are run this means that JUnit test execution window need not be restarted if test cases are recompiled To run a Test object - either a TestCase or a TestSuite, simply invoke the run ( ) method.
15
S Ramakrishnan15 Reference Junit web site: http://junit.org
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.