KBD

Keith Devens .com

Monday, December 1, 2008 Flag waving
Whereof one cannot speak, thereof one must be silent. – Ludwig Wittgenstein (Tractatus Logico-Philosophicus)
← Weblog URLsPyMeld →

Daily link icon Friday, May 9, 2003

Unit testing in PHP

It probably makes me a bad programmer, but I still don't get unit tests. I get what they do, of course, but I haven't integrated it into my development. I mean, for something that crunches numbers (and probably doesn't change program state!), it's really easy to write something that verifies the answer. Either write a different algorithm and make sure the result is the same, or check the inverse of the function (assuming that makes sense) against the input (for instance, you can test that square(sqrt(x)) == x).

But for stuff that makes network connections, messes with the database, deletes files, etc. it seems less straightforward. How do you write unit testing for a parser, which is what I've been working on for the past day or two?

Anyway, John Lim came across Simple_Test for PHP. It has a lot of examples, and since I tend to use PHP a lot, maybe it'll help me get into unit testing.

← Weblog URLsPyMeld →

Comments XML gif

Reverend Jim (http://revjim.net/) wrote:

Don't feel bad. I don't get it either. I understand how to write a test for a parser. Write a test document. Deduce the expected results. Run the parser against the test document. Compare the results. I guess this could come in handy if you make lots of changes to your parser code, and want to make certain that it is parsing correctly. But, for guys like you and me, just running it once will tell me whether it's parsing correctly: I don't need a unit test to tell me that.

Maybe I've missed the point, some how.

∴ Reverend Jim | 9-May-2003 12:49pm est | http://revjim.net/ | #2032

Marcus Baker (http://www.lastcraft.com) wrote:

You have to try it!

You say you ran a comparison test. Did you use diff on the results or inspect it by eye? If you typed diff at the keyboard then you can make it a script. If you make it a script, you can run it again, perhaps after trying out a change.

Did you break anything? No? Try another change. hey, you don't have to think so much anymore. Did you break it this time? Yes? Where is the bug? It's in the line you just changed so debugging has become easier.

When the test works, you get faster, when it fails debugging is faster. Why throw it away the test? It's just so damn useful!

(Oh yes, you don't have to write the tests at the end so you save even more time)

yours, Marcus.

p.s. Sorry to sound preachy, but really - you have to try it. Eric Gamma describes it as test infected.

∴ Marcus Baker | 9-May-2003 8:33pm est | http://www.lastcraft.com | #2033

Simon Willison (http://simon.incutio.com/) wrote:

The best explanation of unit testing I've ever seen is the one in Dive Into Python:

http://diveintopython.org/roman_divein.html

It has a superb example and describes the unit testing process in depth. That said, I am yet to use the technique myself (although I really need to try it out some time this weekend for a university project).

∴ Simon Willison | 10-May-2003 5:24am est | http://simon.incutio.com/ | #2035

J$ (http://alpha-geek.com/right) wrote:

I had the same sort of reaction to unit testing and test driven development until I forced myself to give it a shot. I have been blogging about my experiences with it so far -- my first baby steps into unit testing and test driven development, the first time TDD and unit testing actually saved me some development work and caught an odd bug, a short, simple tutorial on what TDD is at its most basic form.

So, far, it is slowly turning me into a convert.

∴ J$ | 10-May-2003 1:28pm est | http://alpha-geek.com/right | #2045

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

December 2008
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

new⇒Free image hosting sites

Well, TinyPic has this in its​FAQ:

> Images and videos is in​your accoun...

Keith: Dec 1, 1:13am

new⇒Join a NameValueCollection into a querystring in C#

Well with a lamba expression, this​is what I came up​with:

?!code:csharp...

Gustaf Lindqvist: Nov 30, 4:38pm

Why no generic OrderedDictionary?

Check​http://www.codeproject.com/KB/recip​es/GenericOrderedDictionary.aspx?d...

Gabrielk: Nov 27, 6:57am

WhatIsMyIP.com

http://www.thesysteminfo.com is​another good alternate to​whatismp.com... I...

Kripz: Nov 26, 8:51pm

Girls, please don't get breast implants

Actually I think it's sweet when a​man loves a woman whether she's big​or n...

218.186.12.228: Nov 26, 9:40am

Generated in about 0.544s.

(Used 8 db queries)

mobile phone