From: Frank Schmitt Date: 2004-03-18T22:24:32+09:00 Subject: Re: [help] unit testing when dealing with files shasckaw writes: > Hello! > I'm writting some code wich makes MD5 checksum on some files. I'd like > to write a unit test for that code, but I don't know how to proceed. I'd use regression testing - compute the reference MD5 sum of your testfiles using a known working MD5 implementation (e.g. md5 on Linux :-), and check if your results are the same; sth like #!/usr/bin/env ruby require 'test/unit' class MD5Test < Test::Unit::TestCase def test_empty_file # value computed by md5 for an empty file assert_equal("d41d8cd98f00b204e9800998ecf8427e",md5sum("empty_file")) end end Then, you just add additional testfiles until you're satisfied :-) (BTW, I wouldn't use random files - when such a test fails, it can be hard to reproduce the failure, but YMMV). > I have the same problem with another piece of code which launch a > command line which generate some files. How can I unit test such > things? Hm. You could create some reference data files (validated by yourself), run your piece of code and just check for any differences between the reference files and the generated output - not very elegant, but should work, e.g. def test_external_program call_external_program(outfile) assert(!system("diff #{outfile} #{referencefile} > /dev/null")) end HTH & kind regards frank -- Frank Schmitt quattro research GmbH e-mail: schmitt NO at SPAM quattro-research !@! dot com