Test Infected Presentation


Published on

TTD talk I did with Chris Roff

  • 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
  • Don't mention the tools since Chris will be talking about them...
  • Blame game,  Always blame the team further up the Waterfall  until we decided in the clients fault for not knowing what they want.
  • TDD gone wronghow many tests do i need What should i test Never test the database mock everything   Testing types Load User Interface Source code analysis Assembly analysis Code coverage etc...
  • Behaviour Driven Design. Test First Development, Acceptance Test Driven Design Domain Driven Design
  • I think we all know what we want from the requirements thare are being given...
  • Talk about the back story... Everyone know how to play snap. Who should go first? Does the Giant make mistakes? Who goes next after someone wins? How high is the beanstalk? How many cards should there be? Can we walk through a game before we write code? Can assumptions be documented for the development team??
  • Show the before and after view of the Fitnesse Show them failing Indicate that we don't want to go into too much detail since Gojko has a talk coming up soon.
  • Skip very quickly
  • Test Infected Presentation

    1. 1.   <ul><li>  </li></ul>
    2. 2. Test Driven Development Tools and techniques <ul><li>Talk overview </li></ul><ul><ul><li>From idea to requirements, to development, to deployment </li></ul></ul><ul><li>  </li></ul><ul><li>Topics </li></ul><ul><ul><li>Stories to Acceptance Tests </li></ul></ul><ul><ul><li>Unit Testing </li></ul></ul><ul><ul><li>Integration testing </li></ul></ul><ul><ul><li>Deployment testing </li></ul></ul>
    3. 3. What is testing... <ul><li>Verification </li></ul><ul><li>  </li></ul><ul><ul><li>Are we building the software right ? </li></ul></ul><ul><li>  </li></ul><ul><li>   </li></ul><ul><li>Phases </li></ul><ul><ul><li>Unit, Integration, System, User Acceptance, Deployment </li></ul></ul><ul><li>Validation </li></ul><ul><li>  </li></ul><ul><ul><li>Are we building the right software ? </li></ul></ul><ul><li>  </li></ul><ul><li>   </li></ul><ul><li>Phases </li></ul><ul><ul><li>User Acceptance Test or ALL? </li></ul></ul>
    4. 4. What is testing... <ul><li>Verification </li></ul><ul><li>  </li></ul><ul><ul><li>Are we building the software right ?   </li></ul></ul><ul><li>Engineering focused </li></ul><ul><li>   </li></ul><ul><li>Tools </li></ul><ul><ul><li>MbUnit, Watin, RhinoMocks, FXCop, NDepend, NCover... </li></ul></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li>Validation </li></ul><ul><li>  </li></ul><ul><ul><li>Are we building the right software ? </li></ul></ul><ul><li>   </li></ul><ul><li>Analysis focused </li></ul><ul><li>    </li></ul><ul><li>Tools </li></ul><ul><ul><li>Fitnesse, Story Teller, xBehave... </li></ul></ul>
    5. 5. Classic development <ul><li>Quality is a side effect of the process not the driver of the process. </li></ul><ul><li>  </li></ul><ul><ul><li>Developers, Business Analysts, Testers throw requirements, code and bug reports over the wall </li></ul></ul><ul><ul><li>No shared quality/testing strategy </li></ul></ul><ul><ul><ul><li>Different groups use different tools and techniques  </li></ul></ul></ul><ul><ul><li>Often manual </li></ul></ul><ul><ul><li>Regression tests labour/time intensive </li></ul></ul>
    6. 6. Test Driven Development <ul><li>The day to day philosophy Red ,  Green ,  Green </li></ul><ul><li>Red - Analysis/Design Hat </li></ul><ul><ul><li>Create a test that defines the expected outcome </li></ul></ul><ul><ul><li>Asking &quot;What I want is...&quot; </li></ul></ul><ul><li>Green - Creative Hat (Fun part) </li></ul><ul><ul><li>Experiment/Prototype/Build until the test passes </li></ul></ul><ul><li>Green - Engineering </li></ul><ul><ul><li>Refactor, refactor, refactor </li></ul></ul>
    7. 7. Testing Dimensions
    8. 8. Modern TDD <ul><li>Verification and Validation performed continually, during all phases with everyone involved.   </li></ul><ul><ul><li>xUnit/CI is only the start </li></ul></ul><ul><ul><li>TDD mindset is pushing the boundaries of where tests can become automated </li></ul></ul><ul><ul><li>New focus on down stream testing during Requirements gathering  </li></ul></ul><ul><ul><li>Breaking project into Stories promotes detailed requirements over classical analysis which promotes &quot;sweeping open ended statements&quot; </li></ul></ul>
    9. 9. Requirements <ul><li>Precise and clear over verbose </li></ul><ul><li>We want requirements that </li></ul><ul><li>  </li></ul><ul><ul><li>Can be tested!!! </li></ul></ul><ul><ul><li>Are attainable!!!  </li></ul></ul><ul><ul><li>Can be easily mapped to User Stories!!! </li></ul></ul><ul><ul><li>Are traceable into code!!! </li></ul></ul>
    10. 10. Acceptance Tests <ul><li>Using Fitnesse to Verify & Validate </li></ul><ul><li>FitNesse  enables customers, testers, and programmers to  learn what their software should do , and to automatically compare that to  what it actually does do . It compares customers' expectations to actual results.  </li></ul><ul><li>  </li></ul><ul><li>http://fitnesse.org/FitNesse.OneMinuteDescription </li></ul><ul><li>  </li></ul>
    11. 11. Fitnesse <ul><li>What is it? </li></ul><ul><ul><li>A wiki for writing acceptance tests </li></ul></ul><ul><ul><li>Allows Text, Images and Test case data to be intermingled on the same page </li></ul></ul><ul><ul><li>Promotes shared terminology between analysis and development (Ubiquitous language) </li></ul></ul>
    12. 12. Its almost Christmas...   <ul><li>Midland Organic Foods are thinking about Easter - Jack Plays Snap </li></ul><ul><ul><li>Jack climbs beanstalk when correctly snapping </li></ul></ul><ul><ul><li>Jack wins when he reaches the top of the Beanstalk </li></ul></ul><ul><ul><li>Giant snaps when Jack takes too long </li></ul></ul>
    13. 13.   Fitnesse Demo
    14. 14. Fitnesse <ul><li>Demo code was horrible... </li></ul><ul><ul><li>No transactions </li></ul></ul><ul><ul><li>Not N-Tiered </li></ul></ul><ul><ul><li>Inadequate error handling </li></ul></ul><ul><ul><li>Prototype... </li></ul></ul>
    15. 15. Fitnesse <ul><li>As code is productioised </li></ul><ul><ul><li>Fixtures are updated </li></ul></ul><ul><ul><li>Wiki pages ARE NOT TOUCHED!! </li></ul></ul><ul><ul><li>Fixtures are a bridge between analysis and implementation </li></ul></ul><ul><ul><ul><li>Complex fixture implies translation of ideas </li></ul></ul></ul><ul><ul><ul><li>Strive for Ubiquitious language... </li></ul></ul></ul><ul><ul><ul><li>Refactor, refactor, refactor... </li></ul></ul></ul>
    16. 16. Unit Testing
    17. 17. What is Unit Testing <ul><ul><li>Testing individual units of code </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Take smallest piece of testable software in the application & isolate it from the remainder of the code </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Simple, Repeatable, Fast </li></ul></ul>
    18. 18. Advantages Disadvantages <ul><ul><li>  Client & Developer confidence </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Refactoring </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Self documenting </li></ul></ul><ul><ul><li>  Have to write more code </li></ul></ul><ul><ul><ul><li>But..is this REALLY a disadvantage? </li></ul></ul></ul>
    19. 19. Tools <ul><ul><li>  Testing frameworks </li></ul></ul><ul><ul><ul><li>MBUnit, NUnit, xUnit...many more </li></ul></ul></ul><ul><li>  </li></ul><ul><ul><li>Test runners </li></ul></ul><ul><ul><ul><li>Testdriven.Net, Resharper, Gallio, TeamCity </li></ul></ul></ul><ul><li>  </li></ul><ul><ul><li>Mocking </li></ul></ul><ul><ul><ul><li>RhinoMocks, NMock, Typemock </li></ul></ul></ul><ul><li>  </li></ul><ul><ul><li>Code Coverage </li></ul></ul><ul><ul><ul><li>NCover </li></ul></ul></ul>
    20. 20. Unit Test Syntax <ul><ul><li>  Arrange </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Act </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Assert </li></ul></ul>
    21. 21. Example <ul><li>        [ Test ]         public void FindByName_Returns_FarmyardAnimal_When_In_Collection()  { </li></ul><ul><li>            //Arrange </li></ul><ul><li>             string Animal_Name = &quot;test&quot; ; </li></ul><ul><li>             FarmYardAnimal testAnimal = new FarmYardAnimal  </li></ul><ul><li>                                                                  {Name = Animal_Name};             FarmYardAnimals farmYardAnimals = new FarmYardAnimals ();             farmYardAnimals.Add(testAnimal); </li></ul><ul><li>            //Act             FarmYardAnimal foundAnimal =  farmYardAnimals.FindByName(Animal_Name);    </li></ul><ul><li>            //Assert             Assert .AreEqual(testAnimal,foundAnimal);         } </li></ul>
    22. 22. Mocking <ul><ul><li>  Simulating objects that mimic the behavior of real objects in controlled ways. </li></ul></ul><ul><li>  </li></ul><ul><ul><li>  Isolate dependancies </li></ul></ul><ul><li>  </li></ul><ul><ul><li>  Only used in UNIT testing </li></ul></ul>
    23. 23. Mocking cont... <ul><ul><li>  RhinoMocks </li></ul></ul><ul><ul><ul><li>  currently version 3.5 </li></ul></ul></ul><ul><ul><ul><li>  written by Ayende </li></ul></ul></ul><ul><ul><ul><li>  well maintained </li></ul></ul></ul><ul><ul><ul><li>  lots of examples </li></ul></ul></ul><ul><ul><ul><li>  can mock  </li></ul></ul></ul><ul><ul><ul><ul><li>everything on interfaces or </li></ul></ul></ul></ul><ul><ul><ul><ul><li>  virtuals on classes </li></ul></ul></ul></ul>
    24. 24. Mocking cont... <ul><ul><li>  Stubs </li></ul></ul><ul><ul><ul><li>  Supply the SUT with data </li></ul></ul></ul><ul><li>  </li></ul><ul><ul><ul><li>  No constraints </li></ul></ul></ul><ul><li>  </li></ul><ul><ul><ul><li>  Assert on SUT </li></ul></ul></ul><ul><li>  </li></ul><ul><ul><li>  Mocks </li></ul></ul><ul><ul><ul><li>  For interaction testing </li></ul></ul></ul><ul><li>  </li></ul><ul><ul><ul><li>  &quot;Record&quot; actions on the mock </li></ul></ul></ul><ul><li>  </li></ul><ul><ul><ul><li>  Assert on Mock using constraints </li></ul></ul></ul>
    25. 25. Stubbing - What we’re testing <ul><li>public bool GameFinished </li></ul><ul><li>{ </li></ul><ul><li>get </li></ul><ul><li>{ </li></ul><ul><li>return </li></ul><ul><li>(Beanstalk.AtTop || </li></ul><ul><li>(Jack.Cards.Count == 0 && Giant.Cards.Count == 0)); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    26. 26. Example of Stubbing <ul><li>[Test] public void GameFinished_True_When_BeanstalkAtTop_And_Neither_Jack_Or_Giant_HaveCards() { </li></ul><ul><li>  //Arrange </li></ul><ul><li>Queue < IPlayingCard > noCards = new Queue < IPlayingCard >(); </li></ul><ul><li>  </li></ul><ul><li>IBeanstalk stubBeanstalk = MockRepository . GenerateStub < IBeanstalk >(); stubBeanstalk. Expect (sb => sb.AtTop).Return( true ); IPlayer stubJack = MockRepository . GenerateStub < IPlayer >(); IPlayer stubGiant = MockRepository . GenerateStub < IPlayer >(); stubJack. Expect (j => j.Cards).Return(noCards); stubGiant. Expect (j => j.Cards).Return(noCards); Game gameToTest = new Game (stubJack , stubGiant , stubBeanstalk );   </li></ul><ul><li>//Act bool isFinished = gameToTest.GameFinished; //Assert Assert .IsTrue(isFinished); </li></ul><ul><li>} </li></ul>
    27. 27. Mocking - What we're testing <ul><li>public void Save( IGame game){             var status = new GameStatus                              {                                  Giant = game.Giant.Name,                                  Jack = game.Jack.Name,                                  CanSnap = game.CanSnap,                                  IsFinished = game.GameFinished,                                  ActivePlayer = game.ActivePlayer.Name                              };             try             {                 _session.SaveOrUpdate(status);             }             catch ( Exception ex){                 throw new GameRepositoryException ( &quot;Game Save Error&quot; , ex);             } } </li></ul>
    28. 28. Example of Mocking cont.. <ul><li>[Test] public void GameRepository_Should_Convert_GameToGameStatus_And_Use_Session_To_Save (){ </li></ul><ul><li>//Arrange </li></ul><ul><li>GameStatus expectedStatus = new GameStatus {                         ActivePlayer = &quot;active&quot; ,CanSnap = true , </li></ul><ul><li>                         Giant = &quot;giant&quot; ,Jack = &quot;jack&quot; ,IsFinished = false }; </li></ul><ul><li>  </li></ul><ul><li>  //Dont use stubs if creating the object directly is easier </li></ul><ul><li>IPlayer jack = new Player (expectedStatus.Jack); IPlayer giant = new Player (expectedStatus.Giant); IPlayer activePlayer = new Player (expectedStatus.ActivePlayer); </li></ul><ul><li>//Create Game stub </li></ul><ul><li>IGame stubGame = MockRepository .GenerateStub< IGame >(); </li></ul><ul><li>stubbedGame.Expect(g => g.Giant).Return(giant); stubbedGame.Expect(g => g.Jack).Return(jack); stubbedGame.Expect(g => g.ActivePlayer).Return(activePlayer); stubbedGame.Expect(g => g.CanSnap).Return(expectedStatus.CanSnap); stubbedGame.Expect(g => g.GameFinished).Return(expectedStatus.IsFinished); </li></ul>
    29. 29. Example of Mocking....cont <ul><li>  //create mock ISession mockedSession = </li></ul><ul><li>MockRepository . GenerateMock < ISession >(); </li></ul><ul><li> GameRepository repository = new </li></ul><ul><li>GameRepository (mockedSession);   </li></ul><ul><li>//Act repository.Save(stubGame); //Assert mockedSession. AssertWasCalled (ms => ms.SaveOrUpdate( null ),                                           options => </li></ul><ul><li>options.Constraints( Property . AllPropertiesMatch (expectedStatus)); </li></ul><ul><li>} </li></ul>
    30. 30. Constraints <ul><ul><li>Is </li></ul></ul><ul><ul><ul><li>  Is .Null(), Is .NotNull(), Is .GreaterThan() etc </li></ul></ul></ul><ul><ul><li>Property   </li></ul></ul><ul><ul><ul><li>Value( “Status” , Status .Valid), </li></ul></ul></ul><ul><li>        IsNotNull( “User” ), IsNull( “User” ) etc </li></ul><ul><ul><li>List   </li></ul></ul><ul><ul><ul><li>constraints on lists e.g List .Count(2) </li></ul></ul></ul><ul><ul><li>Text </li></ul></ul><ul><ul><ul><li>constraints on text e.g Text .StartsWith( “foo” ) </li></ul></ul></ul><ul><ul><li>Many more...   </li></ul></ul>
    31. 31. Exception Path Testing <ul><li>stubbedSession          .Expect(s => s.SaveOrUpdate( null ))          .IgnoreArguments()          . Throw ( new Exception ()); </li></ul><ul><li>var gameRepository = new GameRepository (stubbedSession); //Act try {        gameRepository.Save(stubbedGame); } </li></ul><ul><li>catch ( GameRepositoryException exception) </li></ul><ul><li>{ </li></ul><ul><li>     //Assert </li></ul><ul><li>} </li></ul>
    32. 32. State vs Interaction Testing <ul><ul><li>Arrange and Act </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Verify state of SUT </li></ul></ul><ul><ul><li>as expected </li></ul></ul><ul><li>  </li></ul><ul><ul><li>  Uses only Stubs </li></ul></ul><ul><ul><li>Arrange and Act </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Verify SUT has interacted with dependancy correctly </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Can use Stubs & Mocks </li></ul></ul>
    33. 33. MbUnit <ul><li>  </li></ul><ul><ul><li>  Testing framework </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Currently at version 3 </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Bundled with Gallio </li></ul></ul><ul><li>  </li></ul><ul><li>  </li></ul>
    34. 34. Row Attribute <ul><li>[Test] [ Row ( &quot;name1&quot; , &quot;name2&quot; )] [ Row ( &quot;name1&quot; , &quot;name1&quot; , ExpectedException = typeof ( ArgumentOutOfRangeException ))] </li></ul><ul><li>public void Can_Only_Add_Animals_With_Unique_Names_To_FarmyardAnimals </li></ul><ul><li>( string firstAnimalName, string secondAnimalName) { //Arrange FarmYardAnimals animals = new FarmYardAnimals (); //Act animals.Add( new FarmYardAnimal {Name = firstAnimalName}); animals.Add( new FarmYardAnimal {Name = secondAnimalName}); } </li></ul>
    35. 35. Integration Testing
    36. 36. What is Integration Testing <ul><ul><li>  Testing components working together </li></ul></ul><ul><li>  </li></ul><ul><ul><li>  No Mocking </li></ul></ul><ul><ul><li>  Long running </li></ul></ul><ul><ul><li>Never sacrifice repeatability for speed </li></ul></ul><ul><ul><li>Large setup, multiple Asserts </li></ul></ul><ul><li>  </li></ul><ul><li>  </li></ul>
    37. 37.   Database Integration Testing <ul><li>[Test] public void Using_The_DTC_To_Rollback_Transactions(){ </li></ul><ul><li>using ( new TransactionScope ()){ using ( ISession session = _sessionFactory.OpenSession()) {    var status = new GameStatus     {         ActivePlayer = &quot;active&quot; ,         CanSnap = true ,         Giant = &quot;giant&quot; ,         IsFinished = true ,         Jack = &quot;jack&quot;      }; session.SaveOrUpdate(status); Assert .IsTrue(status.Id > 0);}} </li></ul>
    38. 38. Database Integration Testing cont... <ul><li>[Test, Rollback ] public void Using_MbUnit_To_Rollback_Transaction() {   using ( ISession session = _sessionFactory.OpenSession()) </li></ul><ul><li>{    var status = new GameStatus     {         ActivePlayer = &quot;active&quot; ,         CanSnap = true ,         Giant = &quot;giant&quot; ,         IsFinished = true ,         Jack = &quot;jack&quot;      };  session.SaveOrUpdate(status);  Assert .IsTrue(status.Id > 0); </li></ul><ul><li>} </li></ul>
    39. 39. Code Coverage - NCover <ul><ul><li>  Commercial & Free versions available </li></ul></ul><ul><li>  </li></ul><ul><ul><li>  Bundled as part of Testdriven.Net </li></ul></ul><ul><li>  </li></ul><ul><ul><li>  Good indication of code requiring tests </li></ul></ul><ul><li>                              </li></ul><ul><li>                                 BUT </li></ul><ul><li>  </li></ul><ul><ul><li>  High test coverage != Fully tested </li></ul></ul>
    40. 40. NCover cont... <ul><li>  </li></ul>
    41. 41. Production Testing
    42. 42. Deployment Verification Tests <ul><li>Automated tests in production </li></ul><ul><li>Is the deployment ok? </li></ul><ul><ul><li>Verify resources have been installed </li></ul></ul><ul><ul><li>Verify software has permission to resources (file, databases, services, etc...) </li></ul></ul><ul><ul><li>Verify that manual software environment is correct (Windows version etc) </li></ul></ul>
    43. 43. Deployment Verification Tests <ul><ul><li>DVT  Hippocratic Oath </li></ul></ul><ul><ul><ul><li>DO NO DAMAGE! </li></ul></ul></ul><ul><ul><ul><li>How much mess can you live with </li></ul></ul></ul><ul><ul><ul><ul><li>Log changes? </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Test data in production? </li></ul></ul></ul></ul><ul><ul><li>Not testing functionality that is done in lower environments </li></ul></ul>
    44. 44. Deployment Verification Tests <ul><li>  </li></ul><ul><li>Steps </li></ul><ul><ul><li>Create a mbUnit project specifically for DVT </li></ul></ul><ul><ul><li>Execute test runner console in production </li></ul></ul><ul><ul><li>Ensure that the Unit tests load the SAME configuration files as the SUT </li></ul></ul><ul><ul><li>Ensure test runner is executed using the same Windows credentials as the SUT </li></ul></ul>
    45. 45. Deployment Verification Tests <ul><li>Create a library of common tests... </li></ul><ul><li>  </li></ul><ul><ul><li>DatabaseAssert library </li></ul></ul><ul><ul><ul><li>DatabaseExists(); </li></ul></ul></ul><ul><ul><ul><li>StoredProcedureExists(); </li></ul></ul></ul><ul><ul><li>FileServerAssert library </li></ul></ul><ul><ul><ul><li>CanOpen(); </li></ul></ul></ul><ul><ul><ul><li>CanDelete(); </li></ul></ul></ul>
    46. 46. Monitoring and Alerting - Heartbeats <ul><li>Continuous tests in production </li></ul><ul><ul><li>If a server is down, how long does it take to find out? </li></ul></ul><ul><ul><ul><li>Within 5 minutes or when the users complain </li></ul></ul></ul><ul><ul><li>Create a service that can be remotely executed  </li></ul></ul><ul><ul><li>Ensure it touches all the systems it depends upon </li></ul></ul>
    47. 47. Merry Christmas <ul><li>Thanks for listening  </li></ul><ul><li>   </li></ul><ul><li>  </li></ul><ul><ul><li>http://www.fitnesse.org </li></ul></ul><ul><ul><li>http://www.mbunit.com/ </li></ul></ul><ul><ul><li>http://ayende.com/projects/rhino-mocks.aspx </li></ul></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li>Code @ www.pebblesteps.com </li></ul>
    48. 48. Back Story for Jack Plays Snap
    49. 49. Its almost Christmas...   <ul><li>Midland Organic Foods are thinking about Easter </li></ul><ul><li>  </li></ul><ul><ul><li>Temporary product for Easter called Beanstalk Munchies </li></ul></ul><ul><ul><li>Organic biscuits coated in chocolate </li></ul></ul><ul><ul><li>Biscuits shaped like Farm Yard Animals based on Jack and Beanstalk fairy tail </li></ul></ul><ul><ul><li>Golden Gooses Egg in the middle of the box </li></ul></ul>
    50. 50. The Pitch - Vision <ul><li>Increase sales by creating Buzz... </li></ul><ul><ul><li>Client wants to create buzz around product launch </li></ul></ul><ul><ul><li>Want a Web game that they can advertise on TV and in retail stores </li></ul></ul><ul><ul><li>Ideas...  Puzzle game or Education game </li></ul></ul>