JunIt Testing
JunIt Testing
JunIt Testing
Unit Testing
Testing concepts
Unit testing
Testing tools
JUnit Examples
Why?
Why testing?
Improve software design Make software easier to understand Reduce debugging time Catch integration errors
Test for boundary conditions Test for both success and failure Test for general functionality Etc..
At the time of starting the projects How we start the projects ?? Do we have any formal way ??
Service
Acceptance test
Fact of testing
A test case is a document that describes an input, action, or event and an expected response, to determine if a feature of an application is working correctly
Reasonable probability of catching an error Does interesting things Doesnt do unnecessary things Neither too simple nor too complex Not redundant with other tests Makes failures obvious Mutually Exclusive, Collectively Exhaustive
Unit testing with JUnit 10
Test case design techniques can be broadly split into two main categories
11
Input
Output
Targeted at the apparent simplicity of the software Makes assumptions about implementation Good for testing component interactions Tests the interfaces and behavior
Unit testing with JUnit 12
Input
Output
Suppose we have two parameters we want to cover in a set of tests. Parameters are as follows..
Operating system
Printers
HP 4100 HP 4200
Types of Tests
Unit
Component
Integration
15
Is easier to use (e.g. dont have to write the same code for each class) Is standardized and reusable Provides a base for regression tests
16
Each class must be tested when it is developed Each class needs a regression test Regression tests need to have standard interfaces Thus, we can build the regression test when building the class and have a better, more stable product for less work
17
Regression testing
New code and changes to old code can affect the rest of the code base
We need to run tests on the old code, to verify it works these are regression tests Regression testing is required for a stable, maintainable code base
18
Testing tools
Tools are part of the quality equation, but not the entire equation
19
JUnit
A unit test is a test of a single class A test case is a single test of a single method A test suite is a collection of test cases
Code often has to be refactored to incorporate the changes Unit testing helps ensure that the refactored code continues to work
Unit testing with JUnit 20
JUnit..
Define and execute tests and test suites Formalize requirements and clarify architecture Write and debug code Integrate code and always be ready to release a working version
21
JUnit runs a suite of tests and reports results For each test in the test suite:
This method should create any objects you may need for testing
22
JUnit calls one test method The test method may comprise multiple test cases;
that is, it may make multiple calls to the method you are testing In fact, since its your code, the test method can do anything you want The setUp() method ensures you entered the test method with a virgin set of objects; what you do with them is up to you JUnit calls tearDown() This method should remove any objects you created
Unit testing with JUnit 23
Define a subclass of TestCase Override the setUp() method to initialize object(s) under test. 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. Define a static suite() factory method that creates a TestSuite containing all the testXXX() methods of the TestCase. Optionally define a main() method that runs the TestCase in batch mode.
24
Fixtures
A fixture is just a some code you want run before every test You get a fixture by overriding the method protected void setUp() { } The general rule for running a test is: protected void runTest() { setUp(); <run the test> tearDown(); } so we can override setUp and/or tearDown, and that code will be run prior to or after every test case
Unit testing with JUnit
25
Override setUp() to initialize the variables, and objects Since setUp() is your code, you can modify it any way you like (such as creating new objects in it) Reduces the duplication of code
26
The next time you run setUp(), your objects will be replaced, and the old objects will be available for garbage collection Like the finally clause in a try-catch-finally statement, tearDown() is where you would release system resources (such as streams)
27
A test method doesnt return a result If the tests run correctly, a test method does nothing If a test fails, it throws an AssertionFailedError The JUnit framework catches the error and deals with it; you dont have to do anything
28
Test suites
In practice, you want to run a group of related tests (e.g. all the tests for a class) To do so, group your test methods in a class which extends TestCase Running suites we will see in examples
29
assertX methods
static void assertTrue(boolean test) static void assertFalse(boolean test) assertEquals(expected, actual)
This method is heavily overloaded: arg1 and arg2 must be both objects or both of the same primitive type For objects, uses your equals method, if you have defined it properly, as public boolean equals(Object o) --otherwise it uses ==. Asserts that two objects refer to the same object (using ==)
assertX methods
Causes the test to fail and throw an AssertionFailedError Useful as a result of a complex test, when the other assert methods arent quite what you want .
All the above may take an optional String message as the first argument, for example, static void assertTrue(String message, boolean test)
31
Create test cases in the same package as the code under test For each Java package in your application, define a TestSuite class that contains all the tests for validating the code in the package Define similar TestSuite classes that create higherlevel and lower-level test suites in the other packages (and sub-packages) of the application Make sure your build process includes the compilation of all tests
Unit testing with JUnit 32
Testing client
Test
fTests
setUp() runTest() tearDown() ConcreteTestCase TestedClass action() setUp() runTest() tearDown() test1() test2() fName
JUnit framework
test1() or test2() Unit testing with JUnit
33
runTest()
For the sake of example, we will create and test a trivial counter class
The constructor will create a counter and set it to zero The increment method will add one to the counter and return the new value The decrement method will subtract one from the counter and return the new value
34
This has the advantages described earlier Depending on the JUnit tool we use, we may have to create the class first, and we may have to populate it with stubs (methods with empty bodies)
Dont be alarmed if, in this simple example, the JUnit tests are more code than the class itself
Unit testing with JUnit 35
36
37
TestCase lifecycle
1. 2. 3. 4.
39
Test Suites
import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite;
Demo
public class AllTests { static public Test suite() { TestSuite suite = new TestSuite(); suite.addTestSuite(SimpleTest.class); suite.addTestSuite(HtmlDocumentTest.class); return suite; } }
Unit testing with JUnit 40
1. 2. 3. 4. 5. 6.
Separate production and test code But typically in the same packages Compile into separate trees, allowing deployment without tests Dont forget OO techniques, base classing Test-driven development
Write failing test first Write enough code to pass Refactor Run tests again Repeat until software meets goal Write new code only when test is failing
Unit testing with JUnit 41
Why JUnit
Allow you to write code faster while increasing quality Elegantly simple Check their own results and provide immediate feedback Tests is inexpensive Increase the stability of software Developer tests Written in Java Free Gives proper uniderstanding of unit testing
Unit testing with JUnit 42
JUnit is designed to call methods and compare the results they return against expected results
This ignores:
Programs that do work in response to GUI commands Methods that are used primary to produce output
Unit testing with JUnit 43
Heavy use of JUnit encourages a functional style, where most methods are called to compute a value, rather than to have side effects
This can actually be a good thing Methods that just return results, without side effects (such as printing), are simpler, more general, and easier to reuse
44
45
Extension point: org.eclipse.ui.views Class extends ViewPart Create widgets in the view by instantiating the classes of those widgets. Only a StyledText is needed!
46
handleCursorPositionChanged
In your Editor Class. Override handleCursorPositionChanged method to implement the update action, and checking if cursor select a strategy or xpath.
47
selection.getOffset());
48
org.eclipse.jface.text.IDocumentPartitioner
public ITypedRegion[] computePartitioning(int offset, int length) When document is changed, you need to recalculated
49
StyledText
org.eclipse.swt.custom.StyledText SWT widget append(String string) setStyleRanges(StyleRange[]) StyleRange specifies various styles for some parts of the text
50
Construct Traversal
Traversal.getEdgeSets() Traversal.getNodeSets() Tricky part: Create ClassGraph from source files
51