You are on page 1of 23

Test it like you mean it!

What do you mean?...


Levels of testing MSTest & Fakes How do I write my acceptance tests How do I write my unit tests Coding guidelines

Levels of testing
Unit do our objects do the right thing? Integration does our code work against code we cant change? End-to-end does the whole system work?

Enough chatting, I want to start writing tests!

Say Hello to MSTest

Creating an MSTest project


Test project template VS2010: Context menu option for Create Unit Tests Anatomy of an MSTest project:
Test classes .runsettings .testsettings* .vsmdi*

*deprecated in VS 2012

Anatomy of a test class


Standard C# class wearing the [TestClass()] attribute The TestContext auto-generated property Test methods [TestMethod()] and Assert Initialization methods [ClassInitialize()] and [TestInitialize()] Cleanup methods [ClassCleanup()] and [TestCleanup()] ExpectedExceptionAttribute

Five steps to create a test in MSTest


1. 2. 3. 4. 5. Initialize* Arrange Act Assert Cleanup*
[TestMethod()] public void SetLicenseServerToLocalhost() { LMHost target = new LMHost(); string server = "127.0.0.1"; int expected = 1; int actual; actual = target.SetLicenseServer(server); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }

Initialization & Cleanup

Microsoft Fakes Fake it (till you make it)


Isolate the system for testing Stub = acts on interface @ compile time Shim = acts on implementation @ run time

Using Stubs
Create Fakes assembly Dependency Injection

Using Shims
Code that cant be stubbed: statics, Framework classes

Running & debugging tests


VS 2012 Tests Explorer view Context menus on:
A test method A test class The solution

Data driven tests


XML/Database the data will be available in the TestContext object

Analyzing the result

How do I write an acceptance test?

Test a walking skeleton


Walking skeleton = the thinnest possible slice of functionality that can be built, deployed and tested end-to-end 5 Steps:
Decide on the walking skeletons shape Mock external libraries Write your skeleton Listen to it to eliminate uncertainty Take it out for a run

Make sure its


Exercising the system end-to-end Telling a user story Independent NOT testing 3rd party libraries every external reference should be mocked

How do I write a Unit Test?

Test one concept at a time


Decide on the systems capabilities Test one capability, not necessarily one method => Unit Test == 1xAssert No conditional logic No loops No tests for private methods (?)

Make sure its..


Fast Isolated Repeatable Self Validating Timely

Make sure its NOT


talking to a database communicating across the network touching the file system asking to be executed in a certain order related to the other tests asking you to change your environment in order for it to work.

Coding guidelines
The test method name should describe a capability. #The client can use localhost as a license server => Client_SetLicenseServerToLocalhost_Success #The sessions controller throws ArgumentException when starting a session with an empty name=> SessionsController_StartEmptyName_ArgumentException Arrange, Act, Assert! Explanatory Assert messages

You might also like