350 likes | 454 Views
TDD Immersion. Jon Kruger Twitter: @ JonKruger Email: jon@jonkruger.com Blog: http://jonkruger.com/blog. Schedule. 1:30-2:00 Intro to TDD 2:05-2:35 Writing Testable Code 2:40-3:00 Mocking with Moq 3:05-3:55 Dependency Injection with StructureMap
E N D
TDD Immersion Jon Kruger Twitter: @JonKruger Email: jon@jonkruger.com Blog: http://jonkruger.com/blog
Schedule 1:30-2:00 Intro to TDD 2:05-2:35 Writing Testable Code 2:40-3:00 Mocking with Moq 3:05-3:55 Dependency Injection with StructureMap 4:00-4:30 Looking at sample code, wrap-up 4:30-5:30 Live coding action!!!
Case Studies We don’t know what code is supposed to do “The Legacy Codebase”
Case Studies We can’t prove that our code is working without someone manually verifying that it works “The Last Minute Change”
Case Studies Bugs are a waste of time “The Infinite Loop of Bugs”
Case Studies Low standards of quality “Throwing It Over the Wall”
Case Studies Bugs can be really expensive to fix “Explosions and Blackouts”
Case Studies Over time, code bases tend to become more chaotic and painful to work with “The Maintenance Nightmare”
How can we solve these problems? • We need a way to ensure that our code is working • We need a way to ensure that our code will continue to work after someone changes it • We need a way to figure out what code is supposed to do • We need to make software development less stressful
What is Test Driven Development? • A software development technique where you write automated unit tests before you write your implementation code • A technique for ensuring good quality and good design • Awesome!
Example of a unit test [TestFixture] public class When_using_the_calculator { [Test] public void Should_add_two_numbers() { int result = new Calculator().Add(2, 3); result.ShouldEqual(5); } }
Types of tests • Unit tests • Tests a small unit of functionality • Mock or “fake out” external dependencies (e.g. databases) • Run fast • Integration tests • Test the whole system working together • Can run slow • Can be brittle • Functional/Acceptance tests • End-to-end tests that test that a feature meets the needs of the business • Manual tests
Unit Testing Frameworks • NUnit • MSTest • MBUnit • xUnit • MSpec
The TDD Process • Write a test (or tests) for something. Since you haven’t written the implementation code yet, the tests should fail. • Write just enough code to get the test to pass. • Move on or refactor “Red – Green – Refactor”
Benefits of TDD We know that our code is working!
Benefits of TDD We know that our code will continue to work
Benefits of TDD We didn’t write bugs
Benefits of TDD We know when we are done
Benefits of TDD We incrementally translated the requirements
Behavior Driven Development • Testing the behavior of the system (not just data returned by a method) • Defining what it means for your system to work correctly (not just verifying that code works)
Benefits of TDD Concentrate on the requirements/tests, then concentrate on implementation
Benefits of TDD We only wrote as much code as we needed to make the tests pass
Benefits of TDD Our tests helped us design our code
Benefits of TDD We had to write testable code
Benefits of TDD We couldn’t cheat and not write the tests
Benefits of TDD Our tests are documentation of what our code does
Benefits of TDD Our tests are documentation of what our code does • Someday someone other than you will have to understand your code • Someday you will wonder what your code was supposed to do • Living, breathing documentation!
Benefits of TDD We can quickly regression test our code
Benefits of TDD We can quickly regression test our code • Fewer bugs • Able to release more often • Less time spent doing manual testing • Prevent our app from becoming “legacy code”
Benefits of TDD Peace of mind!