27
Unit Testing, Principles @QuadraticBE Jul. 2014

Unit testing, principles

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Unit testing, principles

Unit Testing,Principles

@QuadraticBEJul. 2014

Page 2: Unit testing, principles

So it’s done...

I’m Renato Primavera from Quadratic

I write software that helps customers to manage and make use of their geographical data

@RenatoPrimavera [email protected]

www.quadratic.be

Page 3: Unit testing, principles

Unit testing is a method by which individual units of source code

are tested to determine if they are fit for use

In Java, it means mainly testing methods

Page 4: Unit testing, principles

Unit testing is different from Integration testing

in which individual software modules are combined and tested as a group in

their runtime environment

Page 5: Unit testing, principles

Unit testing is also different from Validation testing

which is the final process of checking that a software system meets

specifications and that it fulfills its intended purpose

Page 6: Unit testing, principles

So the goal of unit testing is to isolate each part of the program and show that the

individual parts are correct

A unit test provides a strict, written contract that the piece of code must satisfy

Page 7: Unit testing, principles

As a result,it affords several

benefits

Page 8: Unit testing, principles

Unit testing finds problems early in the development cycle

When the tests pass, that code is considered complete

Tests can be played frequently in an automated way, so any regression is highlighted ASAP

Page 9: Unit testing, principles

In test-driven development (TDD), unit tests are even created before the

code itself is written

Page 10: Unit testing, principles

Unit testing facilitates change

Unit testing allows the programmer to refactor code at a later date, and make

sure the module still works correctly

Page 11: Unit testing, principles

Unit testing provides a sort of living documentation 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

a basic understanding of the unit's interface (API)

Page 12: Unit testing, principles

Unit testing improves

Software Quality

Page 13: Unit testing, principles

> Guidelines please...

Page 14: Unit testing, principles

First a few terminology

Assertiona statement that something should be true

Test Suitea collection of tests

Pass / Successtest that runs ok

Page 15: Unit testing, principles

Fail / Failuretest that does not pass

Fixtureall the things we need to have in place in order to run a test and expect a particular outcome. Some people call

this the test context

Test Double / Fake Object / Mock Objectssimulated objects that mimic the behavior of real

(complex) objects in controlled ways

Page 16: Unit testing, principles

Know what you're testing

A test written without a clear objective in mind is easy to spot. This type of test is long, hard to understand, and usually tests more

than one thing

(When a developer has a problem naming a test, that probably means the test lacks focus)

Page 17: Unit testing, principles

UT should be self-sufficient

A good unit test should be isolated. Avoid static variables usage and dependencies on external data (i.e. database, env settings)

A single test should not depend on running other tests before it, nor should it be affected by the order of

execution of other tests

Page 18: Unit testing, principles

UT should be deterministic

The worst test is the one that passes some of the time. A test should either pass all

the time or fail until fixed

Avoid writing tests with random input, that introduces uncertainty and prevent to reproduce the failure

Page 19: Unit testing, principles

Respect Naming Conventions

This is a good test name

public void throwsIllegalArgumentExceptionIfIconIsNull()

This is no so good

public void iconNullThrowsException()

This is terrible

public void throwsException3()

Page 20: Unit testing, principles

Duplicate code if needed

Readability is very important in unit testing, so it is acceptable to have

duplicate code

Having to change 4-5 similar tests is preferable to not understanding one non-duplicated test when it fails

Page 21: Unit testing, principles

Test results, not implementation

Successful unit testing requires writing tests that would only fail in case of an

actual error or requirement change

Testing the inner workings of how a feature was implemented may lead to test failure even if the result is the same

Page 22: Unit testing, principles

Use Isolation Frameworks

Writing unit tests can be hard when the class has complex dependencies.

Fake objects can help...

Instead of creating fake objects by hand, we can use mocking framework to create them with only a few API calls.

Page 23: Unit testing, principles

Arrggh! Anything else?

Page 24: Unit testing, principles

Adopt Unit Tests

Make them part of your development tasks

(as documentation ;) )

Page 25: Unit testing, principles

Avoid that !“I’ve no time…”

“It’s not my job…”“Tests ? But I never do bugs…”

“The real coder doesn’t test...”

Page 27: Unit testing, principles

Thanks