23
Introduction to Unit Testing and Mocking Joe Wilson, President Volare Systems, Inc. Email: [email protected] Office: 303-532-5838, ext 101 Web: http://VolareSystems.com Blog: http://VolareSystems.com/Blog Twitter: joe_in_denver

Unit Testing And Mocking

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Unit Testing And Mocking

Introduction to Unit Testing and Mocking

Joe Wilson, PresidentVolare Systems, Inc.

Email: [email protected]: 303-532-5838, ext 101

Web: http://VolareSystems.comBlog: http://VolareSystems.com/Blog

Twitter: joe_in_denver

Page 2: Unit Testing And Mocking

Quick Audience Poll

Who has done unit

testing?

Who has tried and given

up?

Page 3: Unit Testing And Mocking

Agenda

What is unit testing?

Step-by-step

What is mocking?

Dos and Don’ts

Page 4: Unit Testing And Mocking

What is unit testing?

Testing one thing at a time

Not touching anything external (DB,

file, etc.)

The developer’s job

Writing and refactoring code

Page 5: Unit Testing And Mocking

Benefits of unit testing

Safer refactoring

Smaller, tighter, decoupled code

Documentation of requirements

Continuous integration

Value of tests increase over time

Page 6: Unit Testing And Mocking

Benefits of unit testing

Benefit

Time – Life of System

$

Cost

Rev 1 Rev 2 Rev 3

Page 7: Unit Testing And Mocking

What do you need?

1. Testing framework

NUnit, MSTest, MbUnit

2. Test runner

NUnit, MSTest, ReSharper, TDD.NET

3. Mocking framework

Rhino Mocks, Moq, TypeMock

Page 8: Unit Testing And Mocking

When do you write the test?

Focus on

requirements

Thinking about how

code will be consumed

Stop coding when

reqs met

Harder initially

Focus on code

Thinking about

algorithm

More refactoring

Easier initially

Before coding (TDD, BDD)

After/During coding

Page 9: Unit Testing And Mocking

Recipe – Unit Test Project

Create unit test

project

Create project

folders

Add

references

Page 10: Unit Testing And Mocking

Recipe – Unit Test Class

Test class per

class

Name

*Tests

Usin

g

[TestFixture]

attribute

Page 11: Unit Testing And Mocking

Recipe – Unit Test Method

Test method per

scenario

[Test]

attribute

Void

method

Page 12: Unit Testing And Mocking

Recipe – Arrange, Act, Assert

Arrange

Setup code, prerequisites,

etc.

Act

One line

Exercise method under

test

Assert

One logical assert per test

Page 13: Unit Testing And Mocking

Code!

Page 14: Unit Testing And Mocking

Recipe – Pull Out a Dependency

1. Wrap dependency with an interface

2. Create a private field of interface type

3. Add interface as an argument in the

constructor

4. Assign private field to argument in

constructor

5. Use the new private field in code

Page 15: Unit Testing And Mocking

Code!

Page 16: Unit Testing And Mocking

What is mocking?

Creating fake objects for you

Nothing you can’t do manually

Set and inspect values on a fake

object

Inspect method calls and args on

a fake object

Page 17: Unit Testing And Mocking

Two kinds of unit tests

Black Box White Box

State Interaction

Page 18: Unit Testing And Mocking

Stub vs. Mock

Get/Set properties

Set method return

values

Test state

Check method calls

Check arguments

used

Test interactions

Stub Mock

Page 19: Unit Testing And Mocking

Recipe – Mocking*

1. Use the MockRepository.GenerateMock<T>()

2. If you need a return value, use myMock.Stub().

3. If the mock is a void, use the

myMock.AssertWasCalled().

* Paraphrased from Jimmy Bogard’s Los Techies blog

Page 20: Unit Testing And Mocking

Code!

Page 21: Unit Testing And Mocking

Unit Testing Dos

One test per scenario

One logical assert

Code to abstractions, wrap difficult code

Prefer state testing over interaction

AAA syntax to keep organized

Use mocking tool for dependencies

Page 22: Unit Testing And Mocking

Unit Testing Don’ts

Ignore failing tests (fix them

immediately)

Test code you didn’t write (BCL, 3rd

party)

Order tests (integration test)

Overuse Setup or Teardown (integration

test)

Overuse Arrange (may need to refactor)

Page 23: Unit Testing And Mocking

ResourcesBooks “Art of Unit Testing” - Roy Osherove “Test-Driven Development in Microsoft .NET” – James Newkirk “Pragmatic Unit Testing in C# with NUnit” – Andy Hunt, Dave Thomas

Testing Frameworks NUnit - http://www.nunit.org MbUnit - http://www.mbunit.com

Mocking Frameworks Rhino Mocks- http://www.ayende.com/projects/rhino-mocks.aspx Moq - http://code.google.com/p/moq TypeMock - http://site.typemock.com

Email: [email protected]: 303-532-5838, ext 101

Web: http://VolareSystems.comBlog: http://VolareSystems.com/Blog

Twitter: joe_in_denver