TDD Introduction to NUnit and Test Driven Design
What is TDD Simply: It is writing the test code before writing any other code Requires a shift in thinking Requires you to decouple from other classes
Steps to follow Don’t write any production code until you have written a failing test Don’t write any more of a test than is sufficient to fail or fail to compile. Don’t write any more production code than is sufficient to make the test pass.
NUnit Things to get started Attributes [TestFixture] [Setup] [TearDown] [ExpectedException] [Test] Assert Validate your logic by using Assert static methods
[TestFixture] Must have a public class, not sealed,  not static,  not virtual…  Looks like this: [TestFixture] public class TestHarness {
[Test] Must be a public void method Looks like this: [Test] public void MyTest() { //code here }
Assert Static methods That Unique syntax but reads nice. AreEqual NotEqual …others
Example TDD Requirements: Make accounts that can hold a monetary balance An Account can transfer funds to another account Accounts cannot transfer if it will make them go negative. We could be more complex but this is a start  
Make the failing test [TestFixture] public class TestHarness { [Test] public void CreateAccount() { Account a = new Account(); Assert.IsNotNull(a); } }
Try to Run our test Run our test, verify that it fails!  It won’t even build We met condition 1 We didn’t writing any code until we had a failing test We wrote just enough code to fail We met condition 2 It feels ridiculous…  Working in very short cycles, one or two minutes per write and fail. This suite of tests acts as a regression later on when you’re very deep in 1000s of methods.  Coverage is king!
Add the code Add just enough code to make it sufficient to pass the failing test. Pass condition 3 Looks like this: public class Account { }
Repeat over and over [Test] public void TestAccountBalance() { Account a = new Account(); a.Balance = 1200M; Assert.AreEqual(a.Balance, 1200M); }
VS Demo Pause here and do in VS
Why
Q & A Question and answers

Tdd

  • 1.
    TDD Introduction toNUnit and Test Driven Design
  • 2.
    What is TDDSimply: It is writing the test code before writing any other code Requires a shift in thinking Requires you to decouple from other classes
  • 3.
    Steps to followDon’t write any production code until you have written a failing test Don’t write any more of a test than is sufficient to fail or fail to compile. Don’t write any more production code than is sufficient to make the test pass.
  • 4.
    NUnit Things toget started Attributes [TestFixture] [Setup] [TearDown] [ExpectedException] [Test] Assert Validate your logic by using Assert static methods
  • 5.
    [TestFixture] Must havea public class, not sealed, not static, not virtual… Looks like this: [TestFixture] public class TestHarness {
  • 6.
    [Test] Must bea public void method Looks like this: [Test] public void MyTest() { //code here }
  • 7.
    Assert Static methodsThat Unique syntax but reads nice. AreEqual NotEqual …others
  • 8.
    Example TDD Requirements:Make accounts that can hold a monetary balance An Account can transfer funds to another account Accounts cannot transfer if it will make them go negative. We could be more complex but this is a start 
  • 9.
    Make the failingtest [TestFixture] public class TestHarness { [Test] public void CreateAccount() { Account a = new Account(); Assert.IsNotNull(a); } }
  • 10.
    Try to Runour test Run our test, verify that it fails! It won’t even build We met condition 1 We didn’t writing any code until we had a failing test We wrote just enough code to fail We met condition 2 It feels ridiculous… Working in very short cycles, one or two minutes per write and fail. This suite of tests acts as a regression later on when you’re very deep in 1000s of methods. Coverage is king!
  • 11.
    Add the codeAdd just enough code to make it sufficient to pass the failing test. Pass condition 3 Looks like this: public class Account { }
  • 12.
    Repeat over andover [Test] public void TestAccountBalance() { Account a = new Account(); a.Balance = 1200M; Assert.AreEqual(a.Balance, 1200M); }
  • 13.
    VS Demo Pausehere and do in VS
  • 14.
  • 15.
    Q & AQuestion and answers