Unit testing: method actions vs. results

Can unit testing be used to test (or test-first develop) methods that
do something rather than return something? E.g., play a sound,
display an image, or drop down a menu. How? TIA,

  • Jim -

Can unit testing be used to test (or test-first develop) methods that
do something rather than return something? E.g., play a sound,
display an image, or drop down a menu. How? TIA,

Essentially, these things are all various forms of output. Your question is
quite broad. Any testing can pretty much be boiled down to put something in,
verify what comes out. Obviously, some forms of output are much more
difficult to test than others.

The XP (extremeprogramming@yahoogroups.com) list archives might turn up some
interesting threads on this.

Chris
http://clabs.org

You might want to use mock objects to check that your code is passing
the right messages; e.g., suppose you have something like this

class SoundPlayer
def initialize
# init device
end

def play(aSound)
end
end

at some other point you’re doing
sp = Soundplayer.new


sp.play ( whatever )

If you replace sp by a mock object with the same interface you can check
that your code is calling the #play method. This sounds exceedingly
trivial here, but you might also want to check that some methods are
called sequentially… However, at some point (the lowest level) it
could be difficult/impossible to unit-test automatically; hopefully you
won’t modify that layer once it is working so unit-testing wouldn’t make
that much sense anyway.

As for the real sound:
in Linux you’d for instance use /dev/dsp and at some point write the
waveform data to that device file; you can instead use an object that
keeps track of what was written and compares it with the expected sound.

When you want to check whether some events happen in a GUI, things become
complicated as you have to play with the event loop. You can however try
to locate a place to substitute mock objects for the real ones.

My 2 (Euro) cents.

···

On Sat, Feb 22, 2003 at 04:43:31AM +0900, James Davis wrote:

Can unit testing be used to test (or test-first develop) methods that
do something rather than return something? E.g., play a sound,
display an image, or drop down a menu. How? TIA,


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

(It is an old Debian tradition to leave at least twice a year …)
– Sven Rudolph