Professional Documents
Culture Documents
Outline
Testing Unit Testing Unit Testing Frameworks JUnit the Javas Unit Testing Framework Introduction Benefits JUnit Notions Assertion Statement Reference Example Unit Testing in Eclipse using JUnit Introduction JUnit Test Cases JUnit Test Suits
JUnit & Eclipse 2
Testing
:: Definition
Software testing is meant to avoid software failure. A failure is caused by a fault in the code base. A symptom is an observable behavior of the system that enables us to observe a failure and possibly find its corresponding fault. The process of discovering what caused a failure is called fault identification. The process of ensuring that the failure does not happen again is called fault correction, or fault removal. Fault identification and fault correction is popularly called debugging. Software testing, in practice, is about identifying a certain possible system failure and design a test case that proves that this particular failure is not experienced by the software. testing can reveal only the presence of faults, never their absence. [Dijkstra]
Testing
:: Definition
Unit Testing, Integration Testing, Function Testing, Acceptance Testing, Installation Testing.
At the day-to-day programming level, unit testing can easily be integrated in the programming effort by using a Unit Testing Framework.
However, unit testing cannot be applied for higher-level testing purposes such as function testing or acceptance testing, which are system-level testing activities.
JUnit & Eclipse 4
Unit Testing
:: Definition
Defintion: A unit test is a piece of code written by a developer that exercises a very small, specific area of functionality applied to one of the units of the code being tested. Usually a unit test exercises some particular method in a particular context. Example: add a large value to a sorted list, then confirm that this value appears at the end of the list. The goal of unit testing is to isolate important parts of the program and show that the individual parts are free of certain faults.
Unit Testing
:: Benefits
Facilitates change:
Unit testing allows the programmer to change or refactor code at a later date, and make sure the module still works correctly (i.e. regression testing).
Simplifies integration:
Unit testing helps to eliminate uncertainty in the units and can be used in a bottom-up integration testing style approach.
Documentation:
Unit testing provides a sort of living documentation of the specifications of the units of the system. Developers looking to learn what functionality is provided by a unit and how to use it can look at the unit tests to gain understanding of the units API specifications.
Unit Testing
:: Benefits (cont.)
Identifies defects
early in the development cycle. Many small bugs ultimately leads to chaotic system behavior, which becomes increasingly difficult to work on. Successful (and meaningful) tests breed confidence. Makes sure that further changes do not introduce problems into previously correct code. Testing forces the programmers to read and analyze their code, thus removing defects through constant code verification.
a large system, there can be thousands of unit tests, which can be tedious to maintain and execute. Automated tests support maintainability and extensibility along with efficiency. A xUnit Testing Framework lets a programmer associate Classes and Methods to corresponding Test Classes and Test Methods. Automation is achieved by automatically setting up a testing context, calling each test case, verifying their corresponding expected result, and reporting the status of all tests. Can be combined with the use of a Software Versioning Repository: prior to any commit being made, unit testing is reapplied to make sure that the committed code is still working properly.
JUnit & Eclipse 8
In Java, the standard unit testing framework is known as JUnit. Test Cases and Test Results are Java objects. JUnit was created by Erich Gamma and Kent Beck, two authors best known for Design Patterns and eXtreme Programming, respectively. Using JUnit you can easily and incrementally build a test suite that will help you measure your progress, spot unintended side effects, and focus your development efforts.
Class the class that is being tested. Tested Method the method that is tested. Test Case the testing of a classs method against some specified conditions. Test Case Class a class performing the test cases. Test Case Method a Test Case Classs method implementing a test case. Test Suite a collection of test cases that can be tested in a single batch.
Tested
10
The Template Method pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses.
All Test Case classes need to be subclasses to the TestCase class. JUnit & Eclipse
11
JUnit test runners automatically invoke the setUp() method before running each Test Class. This method typically initializes fields, turns on logging, resets environment variables, and so forth, i.e. it sets up a context for the test cases to be applied.
In JUnit 4, the initialization method no longer needs to be called setUp(). It just needs to be denoted with the @Before annotation. We can have multiple methods noted @Before, each running before testing.
If we need at the end of each test to do a cleanup operation, we can use JUnits tearDown() method. For example we can call the garbage collector there in case our tests consume large amount of memory.
In JUnit 4, we can give it a more natural name and annotate it with @After. @After protected void disposeObjects () { System.out.println(After testing"); System.gc(); }
JUnit & Eclipse 13
3.
Create the class that you want to test. Build the test class - with the needed imports and extensions for JUnit. Extend this class from junit.framework.TestCase. Name all the test methods with a prefix of test. Code the actual test cases. Validate conditions and invariants using one of the several assert methods. Test Case Class
public void testSquareRootException() { Test Case Method try { Tested Class and Method SquareRoot.sqrt(-4, 1); fail("Should raise an exception"); } Assertion Statement catch (Exception success) { } } }
JUnit & Eclipse 14
Tests are identified by an @Test annotation and we no longer need to prefix our test methods with test. This lets us follow the naming convention that best fits our application.
import junit.framework.*; import org.junit.Test; public class TestAddition extends TestCase { private int x = 1; private int y = 1; @Test public void addition() { int z = x + y; assertEquals(2, z); } }
JUnit & Eclipse 15
16
17
assertNotSame(expected, actual) assertNotSame(message, expected, actual) assertNull(object) assertNull(message, object) assertSame(expected, actual) assertSame(message, expected, actual) assertTrue(condition) assertTrue(message, condition) fail() fail(message) failNotEquals(message, expected, actual) failNotSame(message, expected, actual) failSame(message)
JUnit & Eclipse 19
Eclipse comes with both JUnit and a plug-in for creating and working with JUnit tests. Eclipse allows you to quickly create test case classes and test suite classes to write your test code in. With Eclipse, Test Driven Development (TDD), becomes very easy to organize and implement. Eclipse facilitates the testing by generating automatically stubs for testing class methods.
20
Once the class we want to test, is created we can start with building the test cases. To create a test case do [File New JUnit Test Case] Put the test case class into the same package as the tested class.
21
22
23
24
In order to increase the precision, we increase the number of iterations from 5 to 6, to arrive at:
Assert.assertEquals("SquareRoot precision less than 0.000001", Math.sqrt(67), SquareRoot.sqrt(67, 6), 0.000001);
JUnit & Eclipse 25
In order to fix this problem we increase the number of iterations from 6 to 7, to arrive at:
Assert.assertTrue(67 == (SquareRoot.sqrt(67, 7) * SquareRoot.sqrt(67, 7)));
JUnit & Eclipse 26
27
have performed tests on only one class, i.e. we have tested methods under the consideration they belong to the same class. In large projects we have many classes with methods that should be tested. For testing multiple classes Eclipse and JUnit expose the concept of Test Suit. A Test Suit is a collection of test cases that can be tested in a single batch. A Test Suite is a simple way of running one program that, in turn, runs all test cases.
28
30
31
32
33
Resources
JUnit JUnit FAQ JUnit API Eclipse An early look at JUnit 4 Pragmatic Unit Testing in Java with JUnit http://newton.cs.concordia.ca/~paquet/wiki/index.php/Testing http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks
34