47
Unit Testing Foyzul Karim [email protected] Secure Link Services Limited

Unit testing (workshop)

Embed Size (px)

Citation preview

Page 1: Unit testing (workshop)

Unit Testing Foyzul Karim

[email protected]

Secure Link Services Limited

Page 2: Unit testing (workshop)

Unit testing? No need

1. Lets write a Math Calculator class.

It can

Add

Subtract

We know it will work because we have 'enough' confidence on it.

Page 3: Unit testing (workshop)

Unit testing? No need

2. Lets write a Regular Expression generator class.

It can

Generate Only Alpha Allowed expression with different length

Generate Only Alpha & Numeric Allowed expression with different length

Do you know your code will work before you test by yourself?

If you want to know whether your code is working or not, how would you test?

Create an User Interface Project? then

Put some input fields? then

Put an event trigger control (like button etc)? then

Call your regex method from this project UI?

You would do that.. won't you?

Any other idea?

Page 4: Unit testing (workshop)

How about...

If I say you to put your code under a testing machine. You don't need to test by

yourself using the UI, would you mind?

Lets see one of the most common testing tool of the world.

Anyone wouldn't dare to test that by his/her finger to calculate the result.

Or would anyone?

Page 5: Unit testing (workshop)
Page 6: Unit testing (workshop)
Page 7: Unit testing (workshop)

Why would you need that?

Because you don't need to manually input the values each of the time you want

to test

Because you don't need to input different values each of the time you want to

test

Because you don't need to click and press keyboards each of the time you

want to test

Because after you modify some of your classes you don't want to test manually

the rest other classes

And many more because left...

Page 8: Unit testing (workshop)

Who says to write unit test?

"Any program feature without an automated test simply doesn't exist"

Industrial Logic

Page 9: Unit testing (workshop)

I Don't know those guys

I have enough skill to write codes which act what I expect that to do..

I don't want to write Test Code because I am a

"Super Geek Alien Type Genius Programmer, Not a Tester"

Page 10: Unit testing (workshop)

Probably you know these

JQuery

Json.NET

Entity Framework

JQuery Validation

Microsoft.Web.Infrastructure

JQuery UI

Modernizer

Microsoft ASP.NET Razor 2

ELMAH

Ninject

Page 11: Unit testing (workshop)
Page 12: Unit testing (workshop)
Page 13: Unit testing (workshop)
Page 14: Unit testing (workshop)

So...

You will be unusual if you don’t write test code for your production code.

Page 15: Unit testing (workshop)

Lets test

Required:

Visual Studio 2012

Resharper 7.0

NUnit library

Moq library

Sql Server (any)

Page 16: Unit testing (workshop)

Lets test our Add method

public class MathCalculator

{

public int Add(int first, int second)

{

return first + second;

}

}

[TestFixture]

public class MathCalculatorFixture

{

[Test]

public void TestAddReturnsSum()

{

MathCalculator calculator=new MathCalculator();

int result = calculator.Add(10, 20);

Assert.AreEqual(30, result);

}

}

Page 17: Unit testing (workshop)

Debug vs Run

Page 18: Unit testing (workshop)

Displaying Error message

Page 19: Unit testing (workshop)

Criteria of GOOD Unit

Tests

Each of your test should focus on a single behaviour of a single method.

So it should be

Small

Precise

Independent

Fast

Flexible

Page 20: Unit testing (workshop)

Manual testing is superb !!!

What would you do if you don't have any unit testing framework to test the

Add() method we wrote just now?

Page 21: Unit testing (workshop)

Automated VS Manual Testing

[Industrial Logic]

Manual Tests is more expensive and time consuming

Manual Testing becomes mundane and boring

Automated Tests are reusable

Manual Tests provide limited Visibility and have to be repeated by all

Stakeholders

Automated Tests can have varying scopes and may require less complex setup

and teardown

Automated Testing ensures repeatability (missing out)

Automated Testing drives cleaner design

Automated Tests provide a Safety Net for Refactoring

Automated Tests are living up-to-date specification document

Automated Tests dose not clutter your code/console/logs

Page 22: Unit testing (workshop)

What the Guru's are

saying:

“Any program feature without an automated test simply doesn’t exist.”

from Extreme Programming Explained, Kent Beck

Page 23: Unit testing (workshop)

Practice time

We want to check that whether the username of the student object has

minimum 6 characters in length.

Lets test it.

Hints:

public class StudentManager

{

public bool IsAllowedToProceed(string userName)

{

...

}

}

Page 24: Unit testing (workshop)

Steps we should follow

Arrange

Prepare the objects and appropriate data

Act

Call the appropriate method

Assert

Assert the expected values with the actual values

Page 25: Unit testing (workshop)

What should we focus?

One test method should focus on a single behaviour of the method to be

tested.

If a single method can behave multiple ways, we should write different test

methods to test each of the expected scenarios.

Page 26: Unit testing (workshop)

More Practice...

Discussion and change of the requirements according to the audience level.

For example:

1. Create different criteria for a username to have and test those criteria

Page 27: Unit testing (workshop)

Lets discuss a project

Shortest path finder

Page 28: Unit testing (workshop)

Another Example scenario

A user can attempt to login 3 times sequentially. On failing the 3rd time, his

account will be locked. On the 4th time, his account can't be accessed

whether he gives valid credentials or not. After an hour he will be again able

to try to login.

Page 29: Unit testing (workshop)

Brainstorming time

Find out the possible criteria to test the scenario.

Lets discuss

Page 30: Unit testing (workshop)

Possible scenarios to test

So, the possible scenarios might be

1. Ideal case, where user inputs are valid and he will be given pass to proceed

2. Worst case, using invalid password, the user will try login 3 times in a

sequence and in the 4th time he will receive a account lock notification

3. User can not proceed to the next step if he inputs 3 times in a row, but in the

4th time he use the valid password because the system is locked

4. After one hour user can access the system and the counter is 1

5. After one hour blocking time user can login into the system with valid

password on the 4th time

Page 31: Unit testing (workshop)

Test Method is explaining

1. TestSigninIdealCase

2. TestSigninBlockCase

3. TestSigninBlockCaseResetWithValidPassword

4. TestSigninBlockCaseResetWithDelayedTime

5. TestSigninBlockCaseResetWithDelayedTimeWithValidPassword

and if required we would add more..

Page 32: Unit testing (workshop)

Now Lets discuss about the

Smells

http://www.revealingassets.ca/Home%20Staging%20Addresses%20Bad%20Smells.jpg

Page 33: Unit testing (workshop)

Unbearable Test Code

Smell

Don’t copy and paste

Don’t Repeat your code

Know your fixture

Don’t be optimistic

Do Assert

Be clear about the Unit Test Coverage

Know your Testing Framework

Don’t test everything at a time (image in next page)

Avoid inappropriate dependencies of your test method

Try: One Assertion per test

At least One concept per test

Appropriate error message

Page 34: Unit testing (workshop)
Page 35: Unit testing (workshop)

Designing Effective Micro

Tests

Do the Black-box testing with your class. Avoid white-box testing.

assertSame(element, list.get(0));

vs

assertSame(element, list.contents[0]);

Page 36: Unit testing (workshop)

A good unit test

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

Page 37: Unit testing (workshop)

Test is not unit test if

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

Page 39: Unit testing (workshop)

Sometimes we tweak !!!

At least we can tweak according to our need instead of not writing them at all

Page 40: Unit testing (workshop)

So what we are tweaking

while testing Database

[Test]

public void TestGetShouldReturnUser()

{

using (TransactionScope scope=new TransactionScope())

{

IUserRepository repository = new UserRepository(new

GoldfishDbEntities());

User user = CreateUser();

repository.Save(user);

User returnedUser = repository.Get(user.Id);

Assert.AreEqual(user.Id,returnedUser.Id);

Assert.AreEqual(user.Gender,returnedUser.Gender);

}

}

Page 41: Unit testing (workshop)

Unit testing Advanced

Lets talk about the other attributes

Setup

Teardown

Page 42: Unit testing (workshop)

Inheritance in Unit Testing

Page 43: Unit testing (workshop)

Exception Expectations

How to write the test when our production code throws exceptions?

Page 44: Unit testing (workshop)

Practice (Sales tax)

Sales Tax Introduction

Basic sales tax is applicable at a rate of 10% on all goods, except books, food,

and medical products that are exempt.

Import duty is an additional sales tax applicable on all imported goods at a rate

of 5%, with no exemptions.

When I purchase items I receive a receipt, whichlists the name of all the items

and their price (including tax), finishing

with the total cost of the items, and the total amounts of sales taxes paid. The

rounding rules for sales tax are that for

a tax rate of n%, a shelf price of p contains (np/100 rounded up to the nearest

0.05) amount of sales tax.

Write an application that prints out the receipt details for these shopping

baskets.

Page 45: Unit testing (workshop)

Practice (Sales tax)

INPUT/OUTPUT:

Input 1:

1 book at 12.49

1 music CD at 14.99

1 chocolate bar at 0.85

Output 1:

1 book : 12.49

1 music CD: 16.49

1 chocolate bar: 0.85

Sales Taxes: 1.50

Total: 29.83

Input 2:

1 imported box of chocolates at 10.00

1 imported bottle of perfume at 47.50

Output 2:

1 imported box of chocolates: 10.50

1 imported bottle of perfume: 54.65

Sales Taxes: 7.65

Total: 65.15

Page 46: Unit testing (workshop)

Practice (Sales tax)

Input 3:

1 imported bottle of perfume at 27.99

1 bottle of perfume at 18.99

1 packet of headache pills at 9.75

1 box of imported chocolates at 11.25

Output 3:

1 imported bottle of perfume: 32.19

1 bottle of perfume: 20.89

1 packet of headache pills: 9.75

1 imported box of chocolates: 11.85

Sales Taxes: 6.70

Total: 74.68

Page 47: Unit testing (workshop)