Test Driven Developer
You got a TEST for that?
Home
|
Syndication
|
Sign In
Tuesday, September 22, 2009
Find a BUG, Write A TEST
There are many different kinds of bugs that we encounter in software development:
missing functionality (doesn't do what's required)
unanticipated/undesired behavior (it did what??)
failure to check boundaries (or boundary conditions [e.g null])
user-interface issues (usability)
miscalculations (and logic errors)
control flow errors (failing to break out of a loop, etc.)
failure to handle errors (unhandled exceptions)
security issues (Spoofing, Tampering, Repudiation, Information Disclosure, Denial of Service, Elevation of privilege)
data interpretation errors (what date is 11/12/13 anyway?)
interoperability errors (sending/receiving incorrect messages or data)
and most likely lots of others. Each of these areas has bugs that are all examples of opportunities to write a test that were missed along the development path.
Here is a simple real-world example. Today my Log file writing routine threw an unhandled exception when the file it was trying to write to was in use by another application (this is failure to handle errors, above). This caused the app to crash with an unhandled exception. In truth, I had written the code TDD, so it had tests, but none of them held the file open while it attempted to log data.
This was an unanticipated condition, but probably one that should have been pretty obvious too. However, I was trying to keep the code as simple as possible, and it's not a multi-threaded app, so there wouldn't have been *internal* collisions anyway. Regardless, the app crashing because its log file is being edited is probably not the desired behavior. So (being the test driven developer) I wrote this test first, to illustrate the bug:
[
Test
]
public void
TestLogFileInUse()
{
using (
FileStream
stream =
File
.OpenWrite(
Utilities
.LogFileName))
{
Utilities
.Log(
"test"
);
}
}
it's really simple, but it definitely did fail when the Log() method threw an exception because his file was being held open by the test. Only after I had a failing test, did I then modify the code to catch the IOException and handle it without crashing the application. I could also check for an event log entry if the file write fails, if that was the desired behavior.
The test is fast and simple, and it now guarantees not only that I fixed the bug by making the test pass [not crashing, the only requirement here] but also that the issue will never come back again.
This may be an almost trivial case, but it does illustrate how to use TDD when fixing bugs. I shouldn't really ever be adding new functionality to the mainline code without a failing test of some kind. Refactoring doesn't count, because we only refactor when all the tests are passing. Refactoring doesn't add any new functionality, it is just reorganizing the existing code to make it better.
The worst thing about fixing bugs is when they pop back up later... We hate regressions, because then we have to re-do re-done work YET AGAIN... Lean says that defects are waste, and if they come back after being fixed, then that makes the time spent on fixing them the second time even more wasteful.
This method of "find a bug >> write a test" is a Test-Driven approach to solving issues and making sure they never come back. Let's make regressions an artifact of history, and never fix a bug without having a failing test first!
TDD
|
Bugs
Tuesday, September 22, 2009 7:24:00 PM (Pacific Standard Time, UTC-08:00)
Comments [0]
|
Trackback
Comments are closed.
© Copyright 2013, John E. Boal
Calendar
<
May 2013
>
Sun
Mon
Tue
Wed
Thu
Fri
Sat
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
Total Posts: 57
This Year: 1
This Month: 0
This Week: 0
Comments: 23
Archives
April, 2013 (1)
July, 2012 (1)
June, 2012 (1)
May, 2012 (1)
December, 2011 (1)
November, 2011 (1)
September, 2011 (1)
August, 2011 (1)
May, 2011 (1)
April, 2011 (1)
March, 2011 (0)
February, 2011 (1)
November, 2010 (1)
September, 2010 (1)
June, 2010 (1)
April, 2010 (1)
March, 2010 (1)
February, 2010 (1)
September, 2009 (1)
August, 2009 (1)
June, 2009 (1)
May, 2009 (1)
April, 2009 (1)
February, 2009 (1)
January, 2009 (2)
December, 2008 (2)
November, 2008 (2)
October, 2008 (2)
September, 2008 (4)
August, 2008 (3)
July, 2008 (5)
June, 2008 (4)
May, 2008 (1)
April, 2008 (2)
March, 2008 (4)
February, 2008 (2)
January, 2008 (1)
On this page
categories
ABN
Acceptance Criteria
ATDD
Automation
Bugs
C#
Continuous Integration
Mocks
MSTest
Python
Refactoring
Selenium
SQL
TDD
Testing
Tools
Unit Tests
Video
WatiN
Links
Home
Test Driven Development, Defined (Wikipedia)
Test Driven Design
Test-Driven.com - Agile development tools
NUnit
Book: Test-Driven Development in Microsoft .NET
CodeProject - Advanced Unit Testing: Unit Test Patterns
John Boal's Personal Blog
John Boal's Agile Development Blog
Blogroll
OPML
Lazy Coder
Scott Koon's Blog
#2782
Ade Miller's Tech Blog
Agile Development
Mitch Lacey's Agile Development Blog
Espresso Fueled Agile Development
Mike Puleio's Blog
Geek Noise
Noise de Peter Provost
Sneal's Blog
Shawn Neal's Blog
Search
About
© Copyright 2013, John E. Boal
Sign In