Introduction to test programming


Published on

Introduction to test programing, what is a unit test and TDD flow

Published in: Technology
  • Be the first to comment

  • Be the first to like this

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

No notes for slide

Introduction to test programming

  1. 1. Introduction to Test Programming Implemented with NUnit
  2. 2. Excuses for not testing <ul><li>It takes too much time to write the tests </li></ul><ul><li>How much time do you spend debugging code that you or others have written? </li></ul><ul><li>It takes too long to run the tests . </li></ul><ul><li>It shouldn’t. Most unit tests should execute extremely quickly, so you should be able to run hundreds, even thousands of them in a matter of a few seconds. </li></ul><ul><li>It’s not my job to test my code </li></ul><ul><li>Pray tell, what is your job, exactly? Presumably your job, at least in part, is to create working code. </li></ul><ul><li>I don’t really know how the code is supposed to behave so I can’t test it </li></ul><ul><li>If you truly don’t know how the code is supposed to behave, then maybe this isn’t the time to be writing it. Maybe a prototype would be more appropriate </li></ul><ul><li>But it compiles! </li></ul><ul><li>It’s easy to get lulled into thinking that a successful compile is somehow a mark of approval, that you’ve passed some threshold of goodness. </li></ul>
  3. 3. 12 Benefits <ul><li>Unit tests prove that your code actually works </li></ul><ul><li>You get a low-level regression-test suite </li></ul><ul><li>You can improve the design without breaking it </li></ul><ul><li>It's more fun to code with them than without </li></ul><ul><li>They demonstrate concrete progress </li></ul><ul><li>Unit tests are a form of sample code </li></ul><ul><li>It forces you to plan before you code </li></ul><ul><li>It reduces the cost of bugs </li></ul><ul><li>It's even better than code inspections </li></ul><ul><li>It virtually eliminates coder's block </li></ul><ul><li>Unit tests make better designs </li></ul><ul><li>It's faster than writing code without tests </li></ul>
  4. 4. Nunit in a nutshell
  5. 5. xUnit test patterns <ul><li>Tests should reduce risk, not introduce it. </li></ul><ul><li>Tests should be easy to run. </li></ul><ul><li>Tests should be easy to maintain as the system evolves around them </li></ul><ul><li>Tests should only fail because of one reason. (Tests should only test one thing, avoid multiple asserts for example.) </li></ul><ul><li>There should only be one test that fails for that reason. (this keeps your testbase maintainable) </li></ul><ul><li>Minimize test dependencies (no dependencies on databases, files, ui etc.) </li></ul>
  6. 6. xUnit test patterns Naming Have a descriptive name. Tests-names should read like specifications. If your names get too long you're probably testing too much. Structure Use AAA structure. Arrange your context Act , do the things that need to be tested Assert , assert what you want to check Mocks vs. Stubs The term 'Mock Objects' has become a popular one to describe special case objects that mimic real objects for testing. Mock objects are but one form of special case test object
  7. 7. A test is not a unit test if <ul><li>It talks to the database </li></ul><ul><li>It communicates across the network It touches the file system </li></ul><ul><li>It can't run at the same time as any of your other unit tests </li></ul><ul><li>You have to do special things to your environment (such as editing config files) to run it. </li></ul>
  8. 8. Red – Green - Refactor Red — Write a test that expresses how you’ll use the code and what you need it to do. This test will fail, producing a red bar on many UI’s. Green — Write enough code to get the test to pass, but no more. If you need more code, for example, to check for errors, first write another test to demonstrate that feature. For now, just write enough code to get the test to pass. Refactor — Clean up the code to remove redundancy and improve the design. Then re-run the tests to make sure you didn’t break anything.
  9. 9. The Giants Martin Fowler is an author and international speaker on software development, specializing in object-oriented analysis and design, UML , patterns , and agile software development methodologies, including extreme programming . Fowler is a member of the Agile Alliance and helped create the Manifesto for Agile Software Development in 2001. He popularized the term Dependency Injection [2] as a form of Inversion of Control . Kent Beck is an American software engineer and the creator of Extreme Programming [1] and Test Driven Development . Beck was one of the 17 original signatories of the Agile Manifesto in 2001. He has pioneered software design patterns , the rediscovery of Test-driven development , along with Erich Gamma created the JUnit unit testing framework.