Top Banner
Introduction to Unit Testing SE 2 Tutorial Herman Lee May 26, 2008 Based on tut orial sli des prepared by Thiago Bartol omei SE 3 lecture slides prepared by Paulo Alencar 
21

2 Unit Testing

Apr 07, 2018

Download

Documents

Eel Dde
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 1/21

Introduction to Unit Testing

SE 2 Tutorial

Herman Lee

May 26, 2008

Based on tutorial slides prepared by Thiago Bartolomei

SE 3 lecture slides prepared by Paulo Alencar 

Page 2: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 2/21

Testing Stages Unit testing

Testing of individual components

Integration testing

Testing to expose problems arising from the combinationof components

System testing

Testing the complete system prior to delivery

Acceptance testing

Testing by users to check that the system satisfies

requirements. Sometimes called alpha testing

Page 3: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 3/21

Approaches to Low Level Testing

Debugging/Output calls

Cannot be automated

Time consuming

Application and test code get tangled (output calls)  Unit testing

Set of test cases targeting a single module and

organized in test suites

Strongly encouraged by the agile community

Page 4: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 4/21

More Reasons to do Unit Testing Provide a working specification of the

functional code

Automation

Write test once, use many times

Regression tests – incorrect changes discovered

immediately – more confidence in the code

Page 5: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 5/21

Test Driven Design (TDD) 

TDD = Test First Development + Refactoring

Page 6: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 6/21

Test First Development “The act of writing a unit test is more an act of design

than of verification. It is also more an act of documentation than of verification.”  (Robert C. Martin) 

Idea is to write first the unit tests for a module, andthen implement the module. Why?

Writing the test makes you understand better the functional

code and thus design it better (from the client perspective)  Test becomes a documentation artifact, that clearly states the

intention of the module

Page 7: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 7/21

Test Driven Design Steps

Write tests that fail

Implement modules to make

tests passRun all tests to make sure

nothing is brokenRefactor to keep code clean

Continue development

Page 8: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 8/21

Page 9: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 9/21

Preparing Eclipse Project Create a regular project

Project properties

Build Path–

add JUnit as a library (alreadyshipped with Eclipse)  Build Path – optionally add a folder for tests

Compiler – if JUnit version 4, use Java version 5

That's it!

Page 10: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 10/21

Example 1–

Test Case (JUnit 3) 1) Test class must subclass TestCase. Usually it is

named by adding suffix  “Test”  to tested class name.

2) Test methods must have prefix  “test” 

3) assertTrue(), assertEquals(), etc, give

test pass conditions. E.g. assertTrue(x==10);

4) Test fixture – a common environment to run a set of similar tests. Use setUp() and tearDown()

methods to create and destroy the environment

Page 11: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 11/21

Page 12: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 12/21

JUnit 4 Test Cases

 No longer have to extend TestCase

Test methods annotated with @org.junit.Test

setUp/tearDown annotated with @Before/@After Assertions statically accessed from Assert

Test Suites

 No longer extend TestSuite

Annotated with RunWith and Suite

Page 13: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 13/21

Example

Source:

http://www.devx.com/Java/Article/31983/0/page/1

Page 14: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 14/21

JUnit 3

vs. JUnit 4

Source:

http://www.devx.com/Java/Article/31983/0/page/2

Page 15: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 15/21

DEMO in Eclipse

Page 16: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 16/21

Page 17: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 17/21

Page 18: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 18/21

Some problems you may encounter  “Where do I put my tests?” 

Either in the same package as the tested class:

pack.TheClass, pack.TheClassTest

Or in a test package:

pack.TheClass, pack.test.TheClassTest

Also, in an Eclipse project, you can have differentsource folders with the same package:

src/pack.TheClass,

test/pack.TheClassTest

Advantage – you have protected access to the class

Page 19: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 19/21

Some problems you may encounter  How to test UI?

Test functionality by accessing directly the lower 

tier (maybe a Facade or MVC pattern?)  Testing UI itself usually requires specific tool

support - Ex. HTTPUnit to do webpages testing

Page 20: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 20/21

References / Further Study Test Driven Development (TDD) 

Test Driven Development: By Example, by Kent Beck 

http://www.agiledata.org/essays/tdd.html

Agile Software Development, Refactoring, Test FirstDesign

 Agile Software Development: Principles, Patterns, and 

 Practices, by Robert C. Martin

 Refactoring: Improving Design of Existing Code, by

Martin Fowler 

http://www.xprogramming.com and .org

Page 21: 2 Unit Testing

8/3/2019 2 Unit Testing

http://slidepdf.com/reader/full/2-unit-testing 21/21

References / Further Study Unit test design patterns

http://www.marcclifton.com/tabid/87/Default.aspx

http://www-106.ibm.com/developerworks/library/j-

mocktest.html

JUnit

http://www.junit.org

JUnit 3 vs 4

http://www.devx.com/Java/Article/31983