Test First Programming with Microsoft 'NET - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Test First Programming with Microsoft 'NET

Description:

Assert('Audience should've laughed', talk.AudienceResponse == 'laugh') What's the big deal? ... Finding bugs early and fixing them is critical to good ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 21
Provided by: benjamin5
Category:

less

Transcript and Presenter's Notes

Title: Test First Programming with Microsoft 'NET


1
Test First Programming with Microsoft .NET
  • Benjamin Mitchell
  • Consultant
  • Axxiant

2
Demo
  • Test
  • public void StartPresentationWithJoke
  • Audience myAudience new Audience()
  • Presentation talk new Presentation(
    myAudience, MS Soho)
  • talk.Start()
  • talk.TellJoke()
  • Assert(Audience shouldve laughed,
    talk.AudienceResponse laugh)

3
Whats the big deal?
  • Finding bugs early and fixing them is critical to
    good development
  • When youve written new code
  • How do you know it works?
  • When you have to make a change
  • Will it cause new problems?
  • Will old bugs rise from the grave?
  • Testing first is the key

4
That sounds like a good idea but
  • Testing is a Good Thing but isnt always done
  • Too difficult to set it up
  • Thats what testers are there for
  • It stops me from doing more interesting work

5
Whats different now?
  • Always writing the test before the code
  • Too hard to add testing on later
  • More interesting more likely to be done
  • Use a standard test frameworks
  • Freely available
  • Lots of community support

6
Start with the test
  • Forces thinking about what the method does
  • Provides an easy starting point
  • Provides a finishing line
  • Youre done when the test succeeds

7
What are the steps?
  • Design some minor functionality
  • Write a test that fails
  • Add the smallest amount of code to make it pass
  • GoTo 1
  • When all test pass release the software

8
NUnit
  • Written in C can be used by VB.NET or C
  • Based on the JUnit implementation
  • Be like Microsoft steal from Sun
  • Uses attributes for tests
  • Standalone application, or Visual Studio Addin

9
Lets take the car for a ride!
10
NUnit testing methods
  • Attributes
  • TestFixture
  • Test
  • Setup
  • Teardown
  • ExpectedException
  • Ignore
  • Methods
  • Assert (bool)
  • AssertEquals
  • AssertNull
  • AssertNotNull
  • Fail

11
Other benefits
  • Faster to develop
  • Consistent steady enhancements
  • Avoids Dreaded Day Of Debugging
  • Design improves
  • Requirements are nailed down early
  • Tests are the first users of code
  • Better feedback from experience
  • Creating the test can be harder than the code
  • Tests document the code
  • Others can see how the code should behave
  • Tests can be run by anyone

12
More Benefits
  • No more regression testing
  • Tests are part of the process
  • Write once, run again and again
  • Extra confidence
  • Easier to get started
  • The end of hope as a strategy
  • Develop with less stress and anxiety
  • Easier to make changes and see their impact
  • Better able to handle customer changes

13
Mock Objects
  • Mock Objects are a good idea when the real object
    is
  • Random
  • Difficult to setup
  • Has behaviour that is hard to trigger
  • Is slow
  • Has a user interface
  • Test needs to check how the object was used
  • Does not yet exist

14
Tests go with together with Refactoring
  • Keep the behaviour the same, change the
    implementation
  • Avoids the evils of early optimisation
  • Evolve without having to guess up front

15
Go for the Green Bar
  • Get there every 2 10 minutes
  • Think about the quickest way of getting a green
    bar
  • do the simplest thing first
  • You arent going to need it
  • Only 10 of that extra stuff will ever get used,
    so you are wasting 90 of your time.

16
Doing it
  • It may be slower to begin with
  • Best with component-based systems
  • More difficult with UI and web pages
  • Can and has been done successfully
  • Requires a different design
  • MockObjects frameworks exist for n-tier database
    developments

17
How to write good tests
  • Must be small, simple and fast to run
  • Focus on
  • Things that will change or might fail
  • Tests that take a few minutes to write
  • Manual tasks that need to be done more than once
  • Each Bug deserves its own test
  • Write a test that fails first
  • Then fix the bug
  • Repeat bugs become Shootable Offences
  • Avoid large Scenarios Keep tests independent

18
Where can I find out more?
  • At the pub!
  • Extreme Tuesday Club meets every Tuesday at The
    Old Bank Of England!
  • Attendees this year include Martin Fowler
    (Refactoring) and Alistair Cockburn (Agile
    Methodologies) plus the creators of Mock Objects
  • http//www.xpdeveloper.com/

19
Get going
  • http//www.nunit.org/
  • Starting takes around an hour
  • Become Test Infected
  • Never write a line of functional code without a
    broken test case Kent Beck

20
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com