Unit Testing and Tools
@wbsimms
@southshorenet
Vocabulary
Unit test
•

Code you write to test your code

Coverage
•

Measure of how much code is tested

Mock
•

Replacement for something else

Dependency Injection (DI)
•
•

Builder design pattern
Object and dependencies are created for you

Continuous Integration (CI)
• Responds to checkins, builds code, and generates code
metrics (coverage, unit test pass fail, etc.)
Why Unit Test?
Which would you rather do?

Write Tests

Fix
Production
Bugs
That’s cute… but really… why?
1) Catches regression bugs at the
developers desk
2) Documents how code works
3) Catalog of the business rules
What makes a good unit test?
•
•
•
•

Repeatable
Fast
Test by layers
Good Coverage
Layers?
Web
• Controllers
• ViewModels
• Helpers

DataAccessLayer
• Repositories (or what ever)
• Models
• Helpers

Other Libraries
• Rules Logic
• Etc…
Test documentation?
1. public void MethodName_Case_ExpectedResultTest()
2. [Description(“Your text here”)]
public void MethodNameCaseTest()
When to run your test?

All tests all the
time!






After pull / sync
Before commits
During CI builds
Before you merge
After you merge
So… the tools?
• MSTest
•

Unit testing framework

• Moq
•

Creating mock objects from interfaces

• Unity
•

Dependency Injection

• MSBuild
•

Build automation

• DotCover/Ncover
•

For coverage analysis
Demo time…
Deep breath….

Unit Testing and Tools

  • 1.
    Unit Testing andTools @wbsimms @southshorenet
  • 2.
    Vocabulary Unit test • Code youwrite to test your code Coverage • Measure of how much code is tested Mock • Replacement for something else Dependency Injection (DI) • • Builder design pattern Object and dependencies are created for you Continuous Integration (CI) • Responds to checkins, builds code, and generates code metrics (coverage, unit test pass fail, etc.)
  • 3.
    Why Unit Test? Whichwould you rather do? Write Tests Fix Production Bugs
  • 4.
    That’s cute… butreally… why? 1) Catches regression bugs at the developers desk 2) Documents how code works 3) Catalog of the business rules
  • 5.
    What makes agood unit test? • • • • Repeatable Fast Test by layers Good Coverage
  • 6.
    Layers? Web • Controllers • ViewModels •Helpers DataAccessLayer • Repositories (or what ever) • Models • Helpers Other Libraries • Rules Logic • Etc…
  • 7.
    Test documentation? 1. publicvoid MethodName_Case_ExpectedResultTest() 2. [Description(“Your text here”)] public void MethodNameCaseTest()
  • 8.
    When to runyour test? All tests all the time!      After pull / sync Before commits During CI builds Before you merge After you merge
  • 9.
    So… the tools? •MSTest • Unit testing framework • Moq • Creating mock objects from interfaces • Unity • Dependency Injection • MSBuild • Build automation • DotCover/Ncover • For coverage analysis
  • 10.