[help] unit testing when dealing with files

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
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? Should
I use ruby mock? How? Or something else? And then what? I need help, please!

Thanks,
Lio

“shasckaw” shasckaw@skynet.be wrote in message

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
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?

You are in good company here. The TestUnit framework comes with the standard
Ruby install and is amazingly easy to use. For details see:

http://testunit.talbott.ws/doc/index.html

Should I use ruby mock? How? Or something else? And then what?

I will recommend FlexMock for this purpose. You can find FlexMock at:
http://onestepback.org/software/flexmock/

Take a look at the article “Mock Turtle Soup”:
http://onestepback.org/index.cgi/Tech/Ruby/MockTurtleSoup.rdoc

I need help, please!

Hope that helps. If you can show snippets of your code,
I am sure fellow Rubyists will help more specifically.

– shanko

shasckaw shasckaw@skynet.be 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 :slight_smile:
(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

Shashank Date wrote:

shasckaw wrote:

Should I use ruby mock? How? Or something else? And then what?

I will recommend FlexMock for this purpose. You can find FlexMock at:
http://onestepback.org/software/flexmock/

To mock what? Just write a file and run a checksum on it, right?

Use a Mock to test.

  • what time is it
  • releasing resources
  • pseudo-random numbers
  • without really weird side-effects
  • expensive or slow hardware
  • modules that depend on modules that don’t exist yet, which you can’t
    start
  • 3rd party modules that can’t Get everything they Set
  • responses to failing system calls.

Writing and reading a modern file won’t get farther than the memory buffers,
so “slow hardware” doesn’t apply.

···


Phlip
http://www.xpsd.org/cgi-bin/wiki?TestFirstUserInterfaces

Phlip wrote:

Shashank Date wrote:

shasckaw wrote:

Should I use ruby mock? How? Or something else? And then what?

I will recommend FlexMock for this purpose. You can find FlexMock at:
http://onestepback.org/software/flexmock/

To mock what? Just write a file and run a checksum on it, right?

Use a Mock to test.

  • what time is it
  • releasing resources
  • pseudo-random numbers
  • without really weird side-effects
  • expensive or slow hardware
  • modules that depend on modules that don’t exist yet, which you can’t
    start
  • 3rd party modules that can’t Get everything they Set
  • responses to failing system calls.

Writing and reading a modern file won’t get farther than the memory buffers,
so “slow hardware” doesn’t apply.

Well, I know the basis of unit testing but not of mocking. Thanks for
the indications. Well, that’s true that I don’t need anything more than
just random files with random content that will be randomly modified or
not, and tests will be done on the functions that process them in
various ways (checksum for example).
But the fact is I have no idea where to begin such a thing, I mean the
creation of random files with random content… and if I could randomly
generate a random directory tree with all those random files, it would
be even better.
Can someone help me, please?

Thanks,
Lio