691fc29813dce30bca636046d08d1d73.ppt
- Количество слайдов: 50
Test Driven Development Tools and techniques Talk overview • From idea to requirements, to development, to deployment Topics • Stories to Acceptance Tests • Unit Testing • Integration testing • Deployment testing
What is testing. . . Verification Validation • Are we building the software right? right software ? Phases • Unit, Integration, System, User Acceptance, Deployment • User Acceptance Test or ALL?
What is testing. . . Verification • Are we building the software right? Engineering focused Tools • Mb. Unit, Watin, Rhino. Mocks, FXCop, NDepend, NCover. . . Validation • Are we building the right software ? Analysis focused Tools • Fitnesse, Story Teller, x. Behave. . .
Classic development Quality is a side effect of the process not the driver of the process. • Developers, Business Analysts, Testers throw requirements, code and bug reports over the wall • No shared quality/testing strategy o Different groups use different tools and techniques • Often manual • Regression tests labour/time intensive
Test Driven Development The day to day philosophy Red, Green Red - Analysis/Design Hat • Create a test that defines the expected outcome • Asking "What I want is. . . " Green - Creative Hat (Fun part) • Experiment/Prototype/Build until the test passes Green - Engineering • Refactor, refactor
Testing Dimensions
Modern TDD Verification and Validation performed continually, during all phases with everyone involved. • x. Unit/CI is only the start • TDD mindset is pushing the boundaries of where tests can become automated • New focus on down stream testing during Requirements gathering • Breaking project into Stories promotes detailed requirements over classical analysis which promotes "sweeping open ended statements"
Requirements Precise and clear over verbose We want requirements that • Can be tested!!! • Are attainable!!! • Can be easily mapped to User Stories!!! • Are traceable into code!!!
Acceptance Tests Using Fitnesse to Verify & Validate Fit. Nesse 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. http: //fitnesse. org/Fit. Nesse. One. Minute. Description
Fitnesse What is it? • A wiki for writing acceptance tests • Allows Text, Images and Test case data to be intermingled on the same page • Promotes shared terminology between analysis and development (Ubiquitous language)
Its almost Christmas. . . Midland Organic Foods are thinking about Easter - Jack Plays Snap • Jack climbs beanstalk when correctly snapping • Jack wins when he reaches the top of the Beanstalk • Giant snaps when Jack takes too long
Fitnesse Demo
Fitnesse Demo code was horrible. . . • No transactions • Not N-Tiered • Inadequate error handling • Prototype. . .
Fitnesse As code is productioised • Fixtures are updated • Wiki pages ARE NOT TOUCHED!! • Fixtures are a bridge between analysis and implementation o Complex fixture implies translation of ideas o Strive for Ubiquitious language. . . o Refactor, refactor. . .
Unit Testing
What is Unit Testing • Testing individual units of code • Take smallest piece of testable software in the application & isolate it from the remainder of the code • Simple, Repeatable, Fast
Advantages Disadvantages • Client & Developer confidence • Refactoring • Self documenting • Have to write more code o But. . is this REALLY a disadvantage?
Tools • Testing frameworks o MBUnit, NUnit, x. Unit. . . many more • Test runners o Testdriven. Net, Resharper, Gallio, Team. City • Mocking o Rhino. Mocks, NMock, Typemock • Code Coverage o NCover
Unit Test Syntax • Arrange • Act • Assert
Example [Test] public void Find. By. Name_Returns_Farmyard. Animal_When_In_Collection() { //Arrange string Animal_Name = "test"; Farm. Yard. Animal test. Animal = new Farm. Yard. Animal {Name = Animal_Name}; Farm. Yard. Animals farm. Yard. Animals = new Farm. Yard. Animals(); farm. Yard. Animals. Add(test. Animal); //Act Farm. Yard. Animal found. Animal = farm. Yard. Animals. Find. By. Name(Animal_Name); //Assert Assert. Are. Equal(test. Animal, found. Animal); }
Mocking • Simulating objects that mimic the behavior of real objects in controlled ways. • Isolate dependancies • Only used in UNIT testing
Mocking cont. . . • Rhino. Mocks o currently version 3. 5 o written by Ayende o well maintained o lots of examples o can mock §everything on interfaces or § virtuals on classes
Mocking cont. . . • Stubs o Supply the SUT with data o No constraints o Assert on SUT • Mocks o For interaction testing o "Record" actions on the mock o Assert on Mock using constraints
Stubbing - What we’re testing public bool Game. Finished { get { return (Beanstalk. At. Top || (Jack. Cards. Count == 0 && Giant. Cards. Count == 0)); } }
Example of Stubbing [Test] public void Game. Finished_True_When_Beanstalk. At. Top_And_Neither_Jack_Or_Giant_Have. Cards(){ //Arrange Queue
Mocking - What we're testing public void Save(IGame game){ var status = new Game. Status { Giant = game. Giant. Name, Jack = game. Jack. Name, Can. Snap = game. Can. Snap, Is. Finished = game. Game. Finished, Active. Player = game. Active. Player. Name }; try { _session. Save. Or. Update(status); } catch (Exception ex){ throw new Game. Repository. Exception("Game Save Error", ex); }}
Example of Mocking cont. . [Test] public void Game. Repository_Should_Convert_Game. To. Game. Status_And_Use_Session_To_Save(){ //Arrange Game. Status expected. Status = new Game. Status{ Active. Player = "active", Can. Snap = true, Giant = "giant", Jack = "jack", Is. Finished = false}; //Dont use stubs if creating the object directly is easier IPlayer jack = new Player(expected. Status. Jack); IPlayer giant = new Player(expected. Status. Giant); IPlayer active. Player = new Player(expected. Status. Active. Player); //Create Game stub IGame stub. Game = Mock. Repository. Generate. Stub
Example of Mocking. . cont //create mock ISession mocked. Session = Mock. Repository. Generate. Mock
• Is Constraints o Is. Null(), Is. Not. Null(), Is. Greater. Than() etc • Property o Value(“Status”, Status. Valid), Is. Not. Null(“User”), Is. Null(“User”) etc • List o constraints on lists e. g List. Count(2) • Text o constraints on text e. g Text. Starts. With(“foo”) • Many more. . .
Exception Path Testing stubbed. Session . Expect(s => s. Save. Or. Update(null)) . Ignore. Arguments() . Throw(new Exception()); var game. Repository = new Game. Repository(stubbed. Session); //Act try { game. Repository. Save(stubbed. Game); } catch(Game. Repository. Exception exception) { //Assert }
State vs Interaction Testing • Arrange and Act • Verify state of SUT • Verify SUT has interacted with dependancy correctly as expected • Can use Stubs & Mocks • Uses only Stubs
Mb. Unit • Testing framework • Currently at version 3 • Bundled with Gallio
Row Attribute [Test] [Row("name 1", "name 2")] [Row("name 1", Expected. Exception = typeof (Argument. Out. Of. Range. Exception))] public void Can_Only_Add_Animals_With_Unique_Names_To_Farmyard. Animals (string first. Animal. Name, string second. Animal. Name) { //Arrange Farm. Yard. Animals animals = new Farm. Yard. Animals(); //Act animals. Add(new Farm. Yard. Animal {Name = first. Animal. Name}); animals. Add(new Farm. Yard. Animal {Name = second. Animal. Name}); }
Integration Testing
What is Integration Testing • Testing components working together • No Mocking • Long running • Never sacrifice repeatability for speed • Large setup, multiple Asserts
Database Integration Testing [Test] public void Using_The_DTC_To_Rollback_Transactions(){ using (new Transaction. Scope()){ using (ISession session = _session. Factory. Open. Session()) { var status = new Game. Status { Active. Player = "active", Can. Snap = true, Giant = "giant", Is. Finished = true, Jack = "jack" }; session. Save. Or. Update(status); Assert. Is. True(status. Id > 0); }}
Database Integration Testing cont. . . [Test, Rollback] public void Using_Mb. Unit_To_Rollback_Transaction() { using (ISession session = _session. Factory. Open. Session()) { var status = new Game. Status { Active. Player = "active", Can. Snap = true, Giant = "giant", Is. Finished = true, Jack = "jack" }; session. Save. Or. Update(status); Assert. Is. True(status. Id > 0); }
Code Coverage - NCover • Commercial & Free versions available • Bundled as part of Testdriven. Net • Good indication of code requiring tests BUT • High test coverage != Fully tested
NCover cont. . .
Production Testing
Deployment Verification Tests Automated tests in production Is the deployment ok? • Verify resources have been installed • Verify software has permission to resources (file, databases, services, etc. . . ) • Verify that manual software environment is correct (Windows version etc)
Deployment Verification Tests • DVT Hippocratic Oath o DO NO DAMAGE! o How much mess can you live with § Log changes? § Test data in production? • Not testing functionality that is done in lower environments
Deployment Verification Tests Steps • Create a mb. Unit project specifically for DVT • Execute test runner console in production • Ensure that the Unit tests load the SAME configuration files as the SUT • Ensure test runner is executed using the same Windows credentials as the SUT
Deployment Verification Tests Create a library of common tests. . . • Database. Assert library o Database. Exists(); o Stored. Procedure. Exists(); • File. Server. Assert library o Can. Open(); o Can. Delete();
Monitoring and Alerting - Heartbeats Continuous tests in production • If a server is down, how long does it take to find out? o Within 5 minutes or when the users complain • Create a service that can be remotely executed • Ensure it touches all the systems it depends upon
Merry Christmas Thanks for listening • http: //www. fitnesse. org • http: //www. mbunit. com/ • http: //ayende. com/projects/rhino-mocks. aspx Code @ www. pebblesteps. com
Back Story for Jack Plays Snap
Its almost Christmas. . . Midland Organic Foods are thinking about Easter • Temporary product for Easter called Beanstalk Munchies • Organic biscuits coated in chocolate • Biscuits shaped like Farm Yard Animals based on Jack and Beanstalk fairy tail • Golden Gooses Egg in the middle of the box
The Pitch - Vision Increase sales by creating Buzz. . . • Client wants to create buzz around product launch • Want a Web game that they can advertise on TV and in retail stores • Ideas. . . Puzzle game or Education game


