Search code examples
rubyunit-testingrubygemscommand-line-interfacethor

Which testing technology to use for a command-line Ruby app?


I have written a Ruby program that I would like to release as a Gem. It is built using Thor and command_line_reporter. I have been building it while learning, which for me means that I have no tests. Seeing as the community likes and expects tests, which I understand, I feel I should implement this before making the program public.

While this could be taken as asking for opinions, I feel there must be something that fits my specific needs much better than anything else.

Which testing technology should/can be used for a Thor-based Ruby CLI app?

More info: The app allows the user to create a list of their favorite programs with a few fields of accompanying info. It saves all data to a file in JSON format. This is my first complete program and I have never written any tests before.


Solution

  • Perhaps it might help to address HOW to write tests. There are lots of test frameworks, and lots of philosophies of how we should write tests but I try to keep it simple. I generally start with these:

    • Test to see I got back nil or an object first.
    • Test to see if the object is the right type.
    • Test to see if mandatory attributes are set, then if they're the right types.

    Once I've got those out of the way I'll start antagonizing the code, throwing out-of-bounds and evil values at it, forcing it to raise its exceptions if it's supposed to do that.

    Then, as further use/testing reveal bugs I'd add specific tests to check to see those don't reappear as I screw around with the code. ("Code screwing-around" is gonna happen, so its important I know I didn't make the program go out in flames.)

    ZenTest has the autotest command which looks for a change in your test files and runs the tests automatically. It makes it really easy to make sure I haven't borked things, because in a separate console window autotest will be doing its thing each time I save. It's a great-big safety net you'll get used to having very quickly. From the docs:

    autotest is a continous testing facility meant to be used during development. As soon as you save a file, autotest will run the corresponding dependent tests.

    Writing tests are a necessary evil. They'll double your code-writing load, but they're very important to start early and continue maintaining. Trying to add them later to a large code base is a major problem, causing too many apps to never have unit tests. Icky.