Automated Unit Testing and TDD


Published on

Slides from a presentation I gave at CRineta ( at its April '10 meeting.

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Automated Unit Testing and TDD

  1. 1. Unit Testing and Test Driven Development Presented By Greg Sohl
  2. 2. Agenda <ul><li>Testing? Dude, I already test. </li></ul><ul><li>Unit testing and TDD Overview </li></ul><ul><li>Simple TDD example </li></ul><ul><ul><li>Calculation Class </li></ul></ul><ul><li>But I already have code </li></ul><ul><li>More complex example, with injection </li></ul><ul><ul><li>File Copy Class </li></ul></ul><ul><li>Tools </li></ul><ul><li>Q & A </li></ul>
  3. 3. Testing? Dude, I Already Test Do you currently unit test your code? What types of unit tests do you create? CRineta Member Survey, 8/08
  4. 4. Unit Testing and TDD The process of designing software by writing a validating client before the code. What is Test-Driven Development (TDD)? Programmatically and continuously validating the correctness the code. What is Automated Unit Testing?
  5. 5. Unit Testing and TDD Automated Unit Testing != Test-Driven Development TDD is a way of thinking about your code, but it doesn’t require automated unit testing. Agile processes adapt to change. Unit tests help to manage change in our code. Unit Testing is a key component of the Agile Methodology
  6. 6. Unit Testing
  7. 7. Writing Tests First
  8. 8. Testing - Before and After
  9. 9. TDD
  10. 10. TDD Demo
  11. 11. Test Parts <ul><li>The 3 A’s </li></ul><ul><ul><li>Arrange, Act, Assert </li></ul></ul><ul><li>[Test] </li></ul><ul><li>Public void Send_SingleRecipient_Success </li></ul><ul><li>{ </li></ul><ul><li>EmailManager emailManager = new EmailManager(); </li></ul><ul><ul><li>bool result = emailManager.Send(“”, “”, “Hello”, “message”); </li></ul></ul><ul><ul><li>Assert.AreEqual(true, result, “The email was not successfully sent”); </li></ul></ul><ul><li>} </li></ul><=Arrange <=Act <=Assert
  12. 12. Sounds good, but…
  13. 13. Misconception #1 <ul><li>Won’t writing unit tests increase development time? </li></ul><ul><ul><li>Unit tests are an investment towards time savings over the lifetime of the project. </li></ul></ul><ul><ul><li>Provides regression testing when code changes </li></ul></ul><ul><ul><li>Find bugs early </li></ul></ul><ul><ul><li>Good tests on fundamental areas saves integration testing time </li></ul></ul>
  14. 14. Misconception #2 <ul><li>Won’t all my unit tests just get out of date? </li></ul><ul><ul><li>Unit tests are not parallel to your code like documentation. You will be driving your code with them. </li></ul></ul><ul><ul><li>TDD is a programming paradigm. </li></ul></ul><ul><ul><li>Unit tests are not an artifact of your code. They represent how you write it! </li></ul></ul>
  15. 15. Misconception #3 <ul><li>Won’t the client refuse to pay for it? </li></ul><ul><ul><li>The client is paying for clean, correct code. Unit testing is the means to that end. </li></ul></ul><ul><ul><li>Do they pay us to test our code? </li></ul></ul><ul><ul><li>Do they pay us to use design patterns? </li></ul></ul><ul><ul><li>Do they pay us to debug our code? </li></ul></ul>
  16. 16. Unit Testing Benefits <ul><li>Makes you a user of your software </li></ul><ul><li>Easier to find bugs </li></ul><ul><ul><li>Unless your tests have bugs </li></ul></ul><ul><li>Easier to maintain </li></ul><ul><ul><li>Except when changes require massive updates to tests </li></ul></ul><ul><li>Easier to understand </li></ul><ul><ul><li>Unless the tests are not well written / documented </li></ul></ul><ul><li>Easier to develop </li></ul><ul><ul><li>When tests are maintained as part of process </li></ul></ul>
  17. 17. TDD Helps… <ul><li>Understanding the Problem </li></ul><ul><li>Code Coverage </li></ul><ul><li>Design </li></ul><ul><li>Incremental Development </li></ul>
  18. 18. Different Than Integration Tests <ul><li>Doesn’t rely on external resources </li></ul><ul><li>Exercise only a single unit of code </li></ul>
  19. 19. Good Unit Tests
  20. 20. Good Unit Tests
  21. 21. Good Unit Tests <ul><li>Ask Yourself: </li></ul><ul><ul><li>Can I run and get results from a unit test I wrote two weeks or months or years ago? </li></ul></ul><ul><ul><li>Can any member of my team run and get the results from unit tests I wrote two months ago? </li></ul></ul>
  22. 22. Effective Unit Testing <ul><li>Trustworthy </li></ul><ul><li>Maintainable </li></ul><ul><li>Readable </li></ul>
  23. 23. Writing Testable Code <ul><li>Use interfaces </li></ul><ul><li>Dependency injection </li></ul><ul><li>Focused methods </li></ul><ul><li>Refactoring your code if it is not testable </li></ul>
  24. 24. Demo #2
  25. 25. Unit Testing Tools <ul><li>nUnit </li></ul><ul><li>MS Test </li></ul><ul><li>mbUnit </li></ul><ul><li>xUnit </li></ul>
  26. 26. References <ul><li>Book: The Art of Unit Testing – Roy Osherove </li></ul>
  27. 27. Other Useful Info <ul><li>Database Testing </li></ul><ul><ul><li>Article - </li></ul></ul><ul><ul><li>Article - </li></ul></ul><ul><li>Web Page Testing </li></ul><ul><li>WinForm Testing </li></ul><ul><ul><li>White </li></ul></ul><ul><ul><li>NUnitForms </li></ul></ul>
  28. 28. Questions