Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

  • Be the first to comment

  • Be the first to like this


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