I've written a complicated function foo.1

I'd now like to simplify this function's code, and later enhance it with more features. With each change, however, I'd like to be confident that I'm not breaking previously met specifications. So, I'm looking for a test harness to run between each change.

The obvious, "poor man's" way of doing this seems to be to write a series of If[...] statements that check a given piece of code against expected output, and Print messages according to the results. Or, I could get fancier, specify my tests in a table, for example—

enter image description here

—and automate the testing. (If I were to continue down this route, then of course I'd have to Hold@ the entire table to prevent cells from executing, and write a function that Extracts each row in turn, running the expression and checking its return value.)

But, I'm wondering if there's a package in $Mathematica$ built for this purpose, or at least some canonical way of doing things that I should start getting used to, especially if I'm heading toward writing larger and larger pieces of code, maybe even packages and applications, eventually.

Googling Mathematica test harness, I've come across some pages about MUnit, which appear specific to Workbench, which I don't have.

1 In case it'd be helpful to know, my foo is essentially a data wrapper. Given a symbol, it copies the symbol's OwnValue elsewhere, unsets the symbol, then redefines that symbol with DownValues and UpValues.

  • 2
    Other than Workbench functionality, I'm not aware of any. I use the Assert function much when testing though... – ciao Feb 27 '14 at 5:10
  • Oh, dear. Inputting tables via TableForm seems to be a Bad Idea. I can't insert or add rows through the GUI. I can convert it to InputForm, manipulate that, then convert it back to TableForm, but this fails as I have to Hold@ the entire table. This whole approach seems non-trivial. – Andrew Cheong Feb 27 '14 at 6:04
  • 2
    @m_goldberg - I'm sorry I did not get around to upvoting and commenting on your answer before it was removed. Although I was after a more programmatic, reusable testing harness, I still found your answer helpful. Sometimes, it's good to know what experts are not doing, to solve the problems I face. For example, perhaps the lack of a canonical testing framework in Mathematica hints that the Mathematica "way" might exclude any need for such frameworks. That is, maybe it's an old or foreign way of thinking that compels me to test in ways that are familiar to me. – Andrew Cheong Feb 28 '14 at 5:10

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.