Courses

The Art of Unit Testing & TDD Master Class in .NET

14 lessons 9 h 6 min 5 sections

Start course Watch on YouTube playlist (external link, opens in new tab)

Markdown version

Recorded LIVE during five days of TDD (external link, opens in new tab) master class with Roy Osherove.

About this course

Roy Osherove teaches unit testing (external link, opens in new tab) and test-driven development (external link, opens in new tab) in .NET, starting with NUnit (external link, opens in new tab), test naming (external link, opens in new tab) and the cycle of failing test, passing code and refactoring, practiced on the string calculator kata. The course then covers hand-written fakes (external link, opens in new tab), mocks and stubs (external link, opens in new tab), isolation frameworks (external link, opens in new tab) such as Moq (external link, opens in new tab), FakeItEasy (external link, opens in new tab) and Typemock Isolator (external link, opens in new tab), and reviews of real test code (external link, opens in new tab) from NerdDinner (external link, opens in new tab) and Microsoft's Unity (external link, opens in new tab). It ends with techniques for legacy code, such as extract and override (external link, opens in new tab) and static forwarding, and with talks on faking whole host systems such as Silverlight and SharePoint.

Curriculum

Section 1

The Basics

2 lessons · 2 h 23 min

This section covers what a unit test is, the readability, maintainability and trust that good tests need, and how to write and run NUnit tests in Visual Studio. Roy Osherove then shows the test-driven development cycle of a failing test, the simplest passing code and refactoring, first on a calculator class and then in the string calculator kata, which the audience does in pairs.

  1. Unit Testing And TDD 101 Duration 1:55:52

    Roy Osherove defines a unit test and the three properties of a good one, readability, maintainability and trust, and explains why unit testing efforts fail when any of them is missing. Writing NUnit tests in Visual Studio for a calculator class, he covers three-part test names, readable inputs, factory methods instead of setup methods, and Assert.Throws instead of the ExpectedException attribute. He also separates unit from integration tests, runs tests with TestDriven.NET, and adds features test first: a failing test, the simplest passing code, then refactoring.

  2. Test Driving a String Calculator Duration 27:29

    Roy Osherove introduces code katas for practicing TDD and starts the string calculator kata, whose Add method returns the sum of the comma-separated numbers in a string. He writes one test at a time, starting with an empty string returning zero, and makes each pass in the simplest way, from always returning zero to int.Parse, finding a bug on the way. He runs the tests with NCover, saying coverage means little without test reviews, and sends the audience to do the kata in pairs.

Section 2

Fakes, Mocks and Stubs

3 lessons · 1 h 26 min

This section covers replacing dependencies in tests: redirecting a console application's output in memory, a hand-written fake logger behind an ILogger interface, and the difference between mocks and stubs. The last lesson compares proxy-based isolation frameworks such as Rhino Mocks, Moq and FakeItEasy with profiler-based ones such as Typemock Isolator, which can also fake static methods and future instances.

  1. Test Driving a Console Application Using String Calculator Duration 16:54

    Roy Osherove test-drives a console application that uses the string calculator by calling its Main method in memory from a unit test, after making Program and Main public. He mentions compiler flags and the InternalsVisibleTo attribute for keeping members hidden, then redirects console output with Console.SetOut to a StringWriter over a StringBuilder and checks it with StringAssert. The aim is to connect the string calculator layer to the console layer with a couple of tests, and he moves the console replacement into a helper method.

  2. Introduction to Hand Written Fakes, Mocks and Stubs Duration 7:18

    With a login manager whose save method must also call a logger written by another team, Roy Osherove introduces an ILogger interface so a hand-written fake logger can replace the real one. A fake the test asserts against is a mock object, one it does not assert against is a stub, and the same class can serve as either. He also explains that injecting the fake through a constructor, rather than a property, tells the user of the API the dependency is not optional.

  3. Isolation Frameworks Duration 1:01:27

    Roy Osherove explains isolation frameworks, which create fakes at runtime, and divides the .NET ones into proxy-based frameworks such as Rhino Mocks, Moq and FakeItEasy and profiler-based ones such as Typemock Isolator, JustMock and Moles. Profiler-based frameworks rewrite the IL at runtime through the CLR profiler API, so they can fake static and private methods; proxy-based ones need interfaces or virtual methods. He compares their APIs on the same logger tests, from string-based NUnit Mocks to Rhino Mocks extension methods, and shows Isolator faking static methods and future instances.

Section 3

Test Review Insights

3 lessons · 1 h 53 min

Roy Osherove explains why to review tests and what to look for under readability, maintainability and trustworthiness, with examples of logic, magic numbers, multiple asserts and over-specified mocks in real tests. He then reviews the NerdDinner tests and goes through the audience's review of the tests of Microsoft's Unity project.

  1. Test Review Practices Duration 1:00:31

    Roy Osherove explains that test reviews spread knowledge and that reviews starting from the tests are shorter because tests show the developer's intent, and names readability, maintainability and trustworthiness as the pillars of good tests. In tests from NerdDinner, ASP.NET MVC and other projects he points out logic, magic numbers, dynamically built expected values and several tests in one method. He also warns against over-specified mocks that should be stubs, allows at most one mock per test, and breaks production code by hand in reviews to see whether a test fails.

  2. NerdDinner Test Review Duration 27:17

    Roy Osherove reviews the NerdDinner tests, starting with a single test project that mixes unit and integration tests; he wants them separated into a safe green zone of tests that always pass. A failing HTML rendering test depends on the machine's locale, and he discusses test names, var, magic numbers such as 999, and mock helpers that are really stubs. Different naming conventions show him there was no test review process, and he objects to test dinners created in a hidden helper with a dynamic date.

  3. Unity Test Review Duration 25:37

    After the audience reviewed the tests of Microsoft's Unity project, Roy Osherove goes through their findings, including a test with no assert and setup methods that hide the arrange part, which he replaces with factory methods. They discuss bug recreation tests, which belong in a clearly named class, a test that depends on release or debug build flags and is therefore an integration test, and a structure that does not separate unit from integration tests. He ends by writing an Assert.Throws-style helper for MSTest, which lacks one.

Section 4

Legacy Code

3 lessons · 55 min

This section shows ways to test code that was not written to be testable: extract and override on a login manager with static dependencies, and a static forwarder on a static ping manager class in BlogEngine.NET. The last lesson tests the same blog engine code with Typemock Isolator, faking static methods, a singleton and future instances of a class.

  1. Using Extract & Override Duration 21:33

    Roy Osherove introduces extract and override, from the book Working Effectively with Legacy Code, on a login manager calling a static logger: the call moves into a protected virtual method with as little logic as possible, which a testable subclass overrides. He extends it to simulate a logger exception, check a static web service call and control the machine name rather than build the expected value dynamically. He also uses it to return fake objects, compares it with Moq's protected API, and discusses keeping extracted methods protected or public.

  2. Static Forwarding on a real legacy app Duration 13:44

    In BlogEngine.NET, downloaded from CodePlex, Roy Osherove looks at the static Send method of a ping manager class, which finds links to other blogs in a new post and sends trackbacks or pingbacks. Since the class is static, extract and override cannot be used, so he creates a static forwarder: the static method calls a non-static copy on a new instance, whose dependencies become virtual methods. He advises having integration tests before such a risky refactoring and sets an exercise: test when trackbacks and pingbacks get sent.

  3. Testing Legacy Code with Typemock Isolator Deep Dive Duration 20:08

    Roy Osherove tests the same blog engine code with Typemock Isolator, explaining why Visual Studio must be restarted for a profiler-based framework and what the Isolated attribute does. He fakes the static methods of the trackback class to verify that Send was called, then follows each failure, which he calls wishful invocation, faking a non-public static method, the blog settings singleton and all instances of a remote file class. The test passes without any debugging, and he asks the audience to get to the same point.

Section 5

Bonus tracks on advanced patterns

3 lessons · 2 h 28 min

Two talks show how to unit test code that runs inside a host system such as Silverlight or SharePoint by intercepting the system with CThru and faking its internals, the approach behind the SilverUnit framework. The other lesson uses extract and override with a test specific subclass to test-drive password validation logic separately from each rule.

  1. Code Excavations and Domain Specific Test Frameworks Duration 54:14

    In a talk, Roy Osherove looks at code hosted inside a framework, such as a Silverlight control that cannot be created in a unit test, and at Microsoft's Silverlight test framework, which runs tests in the browser. He compares faking interaction points with an isolation framework, wrapping the system in role-based abstractions, and excavation, where wishful invocations reveal deep dependencies to fake. Using the open-source CThru framework, he disables Silverlight constructors and methods one dependency at a time, which led to SilverUnit, a domain specific test framework running tests in memory.

  2. Testing Complex Validation Knowledge using Extract & Override Duration 43:35

    Roy Osherove explains how to test validation logic that combines many rules, using a password validator, by testing each rule separately from the logic that combines them. He introduces extract and override, also called the test specific subclass pattern, with a testable subclass that overrides the current time so a backup check does not have to wait seven days. He then writes a validator test first, with flags in a testable validator that set what each rule returns, and tests a single rule method directly using the InternalsVisibleTo attribute.

  3. Mocking On The Edge Duration 49:54

    In a 400 level talk, Roy Osherove builds a domain specific isolation framework that stubs out a third-party system by default, using the Typemock Open AOP API, which hooks into the CLR profiler API. For SharePoint he fakes an SPSite title in one test, writes an interceptor that logs every method call, and uses CThru aspects in a custom test attribute to disable the SPSite constructor and skip SP global methods. He shows SilverUnit using the same approach for Silverlight, unlike Microsoft's browser-based test framework, and ends with a song.