Sie sind auf Seite 1von 30

Unit Testing


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:

 I know what I am coding. I trust my code.


 I don't want to reduce my productivity by
writing double amount of code
 I don't want to waste time by thinking different
test cases for a test method
What would you do if..

 You want to add a new functionality or


business logic on the existing project
 The running software is not working properly
 You have to integrate your software with
another software
 There is no documentation of the code and
you are forced to understand the logics
The real situation is

 Fixing a bug is easy


 But finding a bug is nightmare
 The bug can appear somewhere else
 Add new feature is easy
 But ensuring the other parts are working
properly is quite impossible
Why on Earth we would do this?
[Naresh Jain]


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

 But..they share the same types of information.


 So, new class introduces named: Employee
Employee (base class)


Id

Name

Address

Increment

House rent

Others

Starting salary
GetSalary()

 To simplify my calculation, I used here simple


increment formula. It is placed in Employee
class
GetSalary()
{
double salary = startingSalary + startingSalary * yearsPassed *
increment;
salary = salary + salary * houseRent + salary * others;
return salary;
}
Data Sheet for Manager's Salary

Basic 25000 25000 25000

Years 0 1 2

Increment 0 2500 5000


amount

Present basic 25000 27500 30000

House rent 5000 5500 6000

Other 1250 1375 1500

Total 31250 34375 37500


First Test : Test Manager object.

 Set passed year equals 0


 Total salary should be: 31250
New Business Logic!!!

 If any of the employee's salary exceed 20000


unit, he must pay 1% of his total salary he got
from the office.
TaxCalculator.cs
Add New Test For New Logic
Add Another Test

What do you think?????


WHYYYYYYY it is red???

Go back to 14th slide on TaxCalculator.cs


heading
Find out the problem occured
Correct the code (logic) and run
Quick testing/debuging

 To test a method in DateTransformer.cs class.


public static string GetMonth(DateTime date)
{
return
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(date.Month);
}

public void GetMonthFromDateTest()


{
DateTime date = Convert.ToDateTime("3/1/2010");
string month = DateTransformer.GetMonth(date);
Assert.AreEqual("March",month);
}
What about database?

 If we want to save a branch object, how can


we do that in unit testing??
[Test]
public void SaveTest()
{
Branch expectedBranch = CreateNewBranch();
expectedBranch.Branch_Id = Save(expectedBranch);

List<QueryParameter> queryParameters = new List<QueryParameter>();

queryParameters.Add(GetQueryParameterForBranchId(expectedBranch));

Branch fetchedBranch = GetBranchFromDatabase(queryParameters);


Assert.IsTrue(TestBranchProperties(expectedBranch,fetchedBranch));
Assert.IsTrue(Delete(fetchedBranch,queryParameters));
}
Thank you.
Foyzul Karim
foyzulkarim@gmail.com
24th June, 2010
Bangladesh

Das könnte Ihnen auch gefallen