Unit Testing: Validation Method in Which A Programmer
Unit Testing: Validation Method in Which A Programmer
Unit testing is a software verification and
validation method in which a programmer
tests if individual units of source code are fit for
use.
The primary goal of unit testing is to take the
smallest piece of testable software in the
application, isolate it from the remainder of the
code, and determine whether it behaves
exactly as you expect.
But I don't want to follow it because:
Tests keep you out of the (time hungry)
debugger
Tests reduce bugs in New features and in
Existing features
Reduce the cost of change
Improve design of a software
Builds a safety net and defend against other
programmers
Reduce fear
Good Unit Tests [Naresh Jain]
Express intent, not implementation details
Small and Simple: Easy to quickly comprehend
Run fast (they have short setups, run times,
and break downs)
Run in isolation (reordering possible)
Run in parallel
Use data that makes them easy to read and to
understand
Frequently Run with every code change
Test is NOT a Unit Test if [Naresh Jain]
Has External Dependencies
It talks to the Database
It communicates across the Network
It touches the File System
You have to do special things to your
Environment – (such as editing config files)
to run it
It can't run at the same time as any of your
other unit tests
Order Dependencies - Only works when run in
certain order
Enough said. Show me some
CODE....will you?
Sample Project: Salary Calculator
• Entities
– Manager
– Officer
– Worker
Manager
Increment: 10%
House rent: 20%
Others: 5%
Starting salary: 25000/= BDT
Officer
Increment: 8%
House rent: 15%
Others: 5%
Starting salary: 15000/= BDT
Worker
Increment: 6%
House rent: 12%
Others: 5%
Starting salary: 10000/= BDT
Refactoring time
Id
Name
Address
Increment
House rent
Others
Starting salary
GetSalary()
Years 0 1 2
queryParameters.Add(GetQueryParameterForBranchId(expectedBranch));