unit testing unit testing tdd with junit. unit testing unit testing with junit 2 testing concepts...

44
Unit testing Unit testing TDD with JUnit

Upload: cleopatra-caldwell

Post on 31-Dec-2015

373 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Unit testing Unit testing

TDD with JUnit

Page 2: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Unit Testing

Unit testing with JUnit2

Testing conceptsUnit testing

Testing tools

JUnitPractical use of tools

ExamplesHow to create JUnit TestCase in Eclipse

Page 3: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Why?

Unit testing with JUnit3

Why testing?Improve software designMake software easier to understandReduce debugging timeCatch integration errors

In short, to Produce Better CodePreconditions

Working codeGood set of unit tests

Page 4: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

What should be tested ?

Unit testing with JUnit4

Test for boundary conditionsTest for both success and failureTest for general functionalityEtc..

Page 5: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

When to start testing

Unit testing with JUnit5

Software quality and testing is alife-cycle process

Page 6: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

When to start testing...

Unit testing with JUnit6

At the time of starting the projectsHow we start the projects ??Do we have any formal way ??

Page 7: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

The V-model of development

Unit testing with JUnit7

Requirementsspecification

Systemspecification

Systemdesign

Detaileddesign

Module andunit codeand tess

Sub-systemintegrationtest plan

Systemintegrationtest plan

Acceptancetest plan

ServiceAcceptance

testSystem

integration testSub-system

integration test

Page 8: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Fact of testing

Unit testing with JUnit8

Testing does not guaranteethe absence of defects

Page 9: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

What is test case

Unit testing with JUnit9

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

Page 10: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Good test case design

Unit testing with JUnit10

An good test case satisfies the following criteria:Reasonable probability of catching an errorDoes interesting thingsDoesn’t do unnecessary thingsNeither too simple nor too complexNot redundant with other testsMakes failures obviousMutually Exclusive, Collectively Exhaustive

Page 11: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Test case design technique

Unit testing with JUnit11

Test case design techniques can be broadly split into two main categories

Black box (functional)

White box (structural)

Page 12: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Black Box tests

Unit testing with JUnit12

Targeted at the apparent simplicity of the softwareMakes assumptions about implementationGood for testing component interactions

Tests the interfaces and behavior

Input Output

Page 13: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

White Box tests

Unit testing with JUnit13

Targeted at the underlying complexity of the softwareIntimate knowledge of implementationGood for testing individual functions

Tests the implementation and design

Input Output

Page 14: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Test case writing example

Unit testing with JUnit14

Suppose we have two parameters we want to cover in a set of tests. Parameters are as follows..

Operating system Win98 Win2k Winxp

Printers HP 4100 HP 4200

How We should write test case for this ??

Page 15: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Types of Tests

Unit testing with JUnit15

UnitIndividual classes or types

ComponentGroup of related classes or

types

IntegrationInteraction between

classes

Page 16: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

What is a testing framework?

Unit testing with JUnit16

A test framework provides reusable test functionality which:Is easier to use (e.g. don’t have to write the

same code for each class)Is standardized and reusableProvides a base for regression tests

Page 17: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Why use a testing framework?

Unit testing with JUnit17

Each class must be tested when it is developed

Each class needs a regression testRegression tests need to have standard

interfacesThus, we can build the regression test

when building the class and have a better, more stable product for less work

Page 18: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Regression testing

Unit testing with JUnit18

New code and changes to old code can affect the rest of the code base‘Affect’ sometimes means ‘break’

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

Page 19: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Testing tools

Unit testing with JUnit19

Tools are part of the qualityequation, but not the entireequation

Page 20: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

JUnit

Unit testing with JUnit20

JUnit is a framework for writing unit testsA unit test is a test of a single class

A test case is a single test of a single methodA test suite is a collection of test cases

Unit testing is particularly important when software requirements change frequentlyCode often has to be refactored to incorporate the

changesUnit testing helps ensure that the refactored code

continues to work

Page 21: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

JUnit..

Unit testing with JUnit21

JUnit helps the programmer:Define and execute tests and test suitesFormalize requirements and clarify

architectureWrite and debug codeIntegrate code and always be ready to

release a working version

Page 22: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

What JUnit does

Unit testing with JUnit22

JUnit runs a suite of tests and reports results

For each test in the test suite:JUnit calls setUp()

This method should create any objects you may need for testing

Page 23: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

What JUnit does…

Unit testing with JUnit23

JUnit calls one test methodThe test method may comprise multiple test cases;

that is, it may make multiple calls to the method you are testing

In fact, since it’s 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

Page 24: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Creating a test class in JUnit

Unit testing with JUnit24

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.

Page 25: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Fixtures

Unit testing with JUnit25

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

Page 26: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Implementing setUp() method

Unit testing with JUnit26

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

Page 27: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Implementing the tearDown() method

Unit testing with JUnit27

In most cases, the tearDown() method doesn’t need to do anythingThe 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)

Page 28: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

The structure of a test method

Unit testing with JUnit28

A test method doesn’t return a resultIf the tests run correctly, a test method does

nothingIf a test fails, it throws an

AssertionFailedErrorThe JUnit framework catches the error and

deals with it; you don’t have to do anything

Page 29: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Test suites

Unit testing with JUnit29

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

Page 30: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

assertX methods

Unit testing with JUnit30

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 ==.

assertSame(Object expected, Object actual)Asserts that two objects refer to the same object (using

==)

assertNotSame(Object expected, Object actual)

assertNull(Object object)

Page 31: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

assertX methods

Unit testing with JUnit31

assertNotNull(Object object) fail()

Causes the test to fail and throw an AssertionFailedError

Useful as a result of a complex test, when the other assert methods aren’t 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)

Page 32: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Organize The Tests

Unit testing with JUnit32

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 higher-level 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

Page 33: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

JUnit framework

Unit testing with JUnit33

Testing client

run(TestResult)setUp()runTest()tearDown()

TestCase

fName

setUp()runTest()tearDown()test1()test2()

ConcreteTestCase

action()

TestedClass

setUp()runTest()tearDown()

TestResult

runTest()test1()ortest2()

Test

run(TestResult)addTest(Test)

TestSuite

fTests

forall test in fTests test.run(TestResult)

Page 34: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Example: Counter class

Unit testing with JUnit34

For the sake of example, we will create and test a trivial “counter” classThe constructor will create a counter and set it to

zeroThe increment method will add one to the counter

and return the new valueThe decrement method will subtract one from the

counter and return the new value

Page 35: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Example: Counter class

Unit testing with JUnit35

We write the test methods before we write the codeThis has the advantages described earlierDepending 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)

Don’t be alarmed if, in this simple example, the JUnit tests are more code than the class itself

Page 36: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

JUnit tests for Counter

Unit testing with JUnit36

public class CounterTest extends junit.framework.TestCase { Counter counter1;

public CounterTest() { } // default constructor

protected void setUp() { // creates a (simple) test fixture counter1 = new Counter(); }

protected void tearDown() { } // no resources to release

Page 37: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

JUnit tests for Counter…

Unit testing with JUnit37

public void testIncrement() { assertTrue(counter1.increment() == 1); assertTrue(counter1.increment() == 2); }

public void testDecrement() { assertTrue(counter1.decrement() == -1); }} // End from last slide

Page 38: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

The Counter class itself

Unit testing with JUnit38

public class Counter {int count = 0;public int increment() { return ++count;}public int decrement() { return --count;}

public int getCount() { return count; }}

Page 39: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

TestCase lifecycle

Unit testing with JUnit39

1. setUp

2. testXXX()

3. tearDown()

4. Repeats 1 through 3 for each testXXX method…

Page 40: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Test Suites

Unit testing with JUnit40

import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;

import example.SimpleTest;import example.HtmlDocumentTest;

public class AllTests { static public Test suite() { TestSuite suite = new TestSuite(); suite.addTestSuite(SimpleTest.class); suite.addTestSuite(HtmlDocumentTest.class); return suite; }}

Demo

Page 41: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

JUnit Best Practices

Unit testing with JUnit41

Separate production and test code But typically in the same packages Compile into separate trees, allowing

deployment without tests Don’t forget OO techniques, base classing Test-driven development

1. Write failing test first2. Write enough code to pass3. Refactor4. Run tests again5. Repeat until software meets goal6. Write new code only when test is failing

Page 42: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Why JUnit

Unit testing with JUnit42

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

Page 43: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Problems with unit testing

Unit testing with JUnit43

JUnit is designed to call methods and compare the results they return against expected resultsThis ignores:

Programs that do work in response to GUI commands

Methods that are used primary to produce output

Page 44: Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools

Problems with unit testing…

Unit testing with JUnit44

Heavy use of JUnit encourages a “functional” style, where most methods are called to compute a value, rather than to have side effectsThis can actually be a good thingMethods that just return results, without side

effects (such as printing), are simpler, more general, and easier to reuse