3. Types of Testing
●
Unit testing: Does a single object work?
●
Integration testing: Do multiple objects work together?
●
Functional testing: Does my application work?
●
Performance testing: Does my application work well?
●
Acceptance testing: Does the customer like my
application?
4. Why Test?
●
No-brainers: Functional, Acceptance, Performance testing
●
Why Unit Test?
– Coverage, especially of strange conditions
– Facilitates teamwork, lessens 'ownership'
– Confidence in your own work
– Enable refactoring
– Self-documentation of API
– Improve design
5. How to use tests
●
A common testing cycle:
– Write some new code OR Fix a bug OR Refactor
some code
– Run tests to ensure the now-changed code works
– Check the new code in
6. Basic testing principles
●
For things that can be tested automatically, tests
should be:
– easy to write
– easy to run
– objective (easy to see if they pass or fail)
– repeatable (deterministic)
7. Writing tests
Methods of Junit.gramework.assert class
●
assertTrue(boolean)
●
You can supply any boolean expression to this method, and as long as the boolean expression
evaluates to true, the method reports success. If the condition you want to check should be false,
simply prepend the "!" negation operator to your parenthesized expression.
●
assertTrue(String, boolean)
●
Same as the first form of assertTrue, except that the supplied String is printed if the assertion fails.
Most methods have a version of this form with a message string.
●
assertEquals(Object, Object)
●
Compares the two objects you pass in using the equals( ) method.
●
assertNull(Object)
●
Succeeds if the Object reference passed in is null. There is also an assertNotNull method.
●
fail(String)
●
Causes the test to fail, printing out the supplied String.
8. JUnit
●
Open source framework for testing Java code
●
Attempts to make writing unit tests easy
●
Structure:
●
TestCase [Java class]
– Test [Java method]
– Most often you write TestCase
9. Example
import junit.framework.TestCase;
public class TestString extends TestCase {
public void testEndsWith() {
assertTrue(“abcabc”.endsWith(“abc”));
}
public void testEndsWithEmptyString() {
assertTrue(“Doesn't end with empty string!”,
“abcabc”.endsWith(“”));
}
}
10. Some best practices
●
TestCases (Java classes)
– Usually test a single Java class
– Name starts or ends with Test
●
Tests (Java methods)
– test a single method
●
testXxx tests method xxx
●
multiple tests of method xxx are named testXxxYyy,
where Yyy describes a particular condition, e.g.,
testEndsWithEmpty
●
Label your assertions
●
One test per test method
11. More details
●
fixtures: setUp, tearDown
●
assertFalse, assertEquals, assertNotEquals,
assertNull, etc., etc.
●
JUnit 4/Java 5 – some incremental improvements
12. Running JUnit tests
●
Many ways:
– Command line
– IDE (Eclipse plug-in, etc.)
– Ant
– Maven
●
Various ways of viewing output (text, XML ->
Web page, ...)