Unit & integration testing

  • Published on

  • View

  • Download

Embed Size (px)



Unit & Integration TestingA way to a better software


IntroductionThe main work is performed in implementation & testing phases.

Unit tests save time & improve design.

What is a unit test?A unit test is a piece of a code (usually a method) that invokes another piece of code and checks the correctness of some assumptions afterward.

If the assumptions turn out to be wrong, the unit test has failed. A unit is a method or function.

What a unit test should not do?Unit tests are not about finding bugs, theyre all about code verification.

Unit tests are not supposed to be built on top of existing design for the sake of code coverage.

In fact, they should drive your system towards flexible & testable design, thus providing an interface to verify the correctness of the input/output of the unit.

Unit tests scopeThe scope of Unit testing is narrow, it covers the Unit or small piece of code under test. Therefore while writing a unit test shorter codes are used that target just a single class.

Unit tests should have no dependencies on code outside the unit tested.

What exactly should be tested?Tests should be written for a specific CUTTests should cover only the API of this CUT and are not supposed to test the implementation details of a unitTest only units with some logic (e.g. methods and functions not properties)It makes sense to test only those units over which you have controlTest CUTs against interfaces

Single ResponsibilityTest behaviour, not methods:One method, multiple behaviours -> Multiple TestsOne behaviour, multiple methods -> One TestA method calls private & protected methodsA method calls propertiesOne behaviour, one result -> One AssertMultiple asserts are okay as long as they check the same behaviour

What makes a good unit test?A good unit test should have the following properties:It should be automated and repeatable.It should be easy to implement.It should be relevant tomorrow.Anyone should be able to run it at the push of a button.It should run quickly.It should be consistent in its results.It should have full control of the unit under test.

What makes a good unit test?A good unit test should have the following properties:It should be fully isolated from other tests.When it fails, it should be easy to detect what was expected and determine how to pinpoint the problem.Fails when the logic against which it was tested changesDoesnt make you want to comment themDoesnt call other testsAre implemented quicklyCovers multiple behaviours

How to verify that test is correct?After implementing a test try changing the behaviour of the unit, so it down not meet the requirement anymore, and run it again. It should fail.

Failing tests are as important as passing ones.

What is an integration test?Integration testing is testing a unit of work without having full control over all of it and using one or more of its real dependencies, such as time, network, database, threads, random number generators, and so on.

Integration tests scopeThe scope of Integration testing is wide, it covers the whole application under test and it requires much more effort to put together.

Integration testing is dependent on other outside systems like databases, hardware allocated for them etc.

Benefits of unit & integration testsHelps to improve and build flexible designVerifies that code is working as intendedHelps to stick to requirementsIs a good source for code documentation

Unit tests are a form of sample codeDetect failing logic upfrontMeasures the completion of a classPinpoints the problem locationSimplifies integration

Test structure patterns A A AArrange(Given)Assert(Then)Act(When)

Naming convention


Unit of work - name of method you are testingState under test - short name for the state/input used for current testExpected behavior - short name for expected result of the test which youre going to check

Test Driven DevelopmentMany people feel that the best time to write unit tests for software is after the software has been written, but a growing number prefer writing unit tests before the production code is written. Thisapproach is called test-first or test-driven development (TDD).

Red - Green - RefactorWrite a failing test to prove code or functionality is missing from the end product.

Make the test pass by writing production code that meets the expectations of your test.

Refactor your code.

Additional resourcesThe Art of Unit Testing: with Examples in C#, Second Edition by Roy Osherove

Writing Great Unit Tests: Best and Worst Practices: http://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises/

Inspired by Technology.Driven by Value.



View more >