views:

64

answers:

2

I have a file with unit tests

    testOne() {...}
    testTwo() {...}
...

but when I am troubleshooting I would like to turn off all except the unit test that is causing problems (due to massive amount of logging). At the moment I have done like this:

#if 0
testOne() {...}
#endif
..
#if 1
testTroublesome() {...}
#endif

But I was wondering if there is some better more convenient way to do this?

+1  A: 

You could for example have an environment variable to signify which test to run, with unset meaning run them all (which would be the usual case). Like so: (this is C, but I think it'll work in objetive C as well)

char *env = getenv ("MY_TEST_ENV");

and then when running each test

if (! env || 0 == strcmp (env, "testOne"))
  testOne();

Or you could put the same condition inside the test itself and just return if it fails. This wouldn't keep your tests from being compiled though, but I don't think that's your problem is it? Just set the environment variable to the test you want to run, and none of the others will.

EDIT
To make it even easier, put that in a macro

#define RUN_TEST(fn) do{if(!getenv("MY_TEST_ENV")||!strcmp(getenv("MY_TEST_ENV"),#x))x();}while(0)

and always execute your test with that

RUN_TEST(test_one);

... and you've got yourself a little unit test framework going. Before taking it too far and reinventing too many wheels though, you should (as has been pointed out) perhaps take a look at existing frameworks.

roe
thanks i'll try that.
BuggerMe
+1  A: 

Two notes:

  • most unit test frameworks offer the possibility to organize your tests into suites, thus you can run only specific tests.
  • you should consider reconfiguring logging in unit tests so that no logs are produced. Unit test output should be the simplest possible, to avoid drowning your focus into useless details. I.e. if all tests pass, the only output you should see is something like "123 tests passed." or if there are errors, the names of the failing test(s) and the relevant error messages.
Péter Török
the first point is assuming you're using a unit test framework, which isn't always the case (it adds an extra dependency, and for small projects, that's usually more hassle than it's worth). Totally agree on the second point though.
roe
@roe, I don't know Objective-C unit test frameworks, but e.g. for C you have UnitTest++ which is a single header file - I don't think you can get any simpler than that :-)
Péter Török
UnitTest++ is C++, not C, and it's definitely more than a single header file. It's also got a fairly simple license, but it's still a dependency which you might not want to manage. It's easier still to just have your tests, no framework and weird stuff to clutter up your workspace. I'm not arguing for or against anything here, just saying that sometimes it's preferable not to have a unit test framework.
roe
@roe, oops, apparently it's been too much time since I used it last time :-( I am sure there are publicly available unit test frameworks implemented in a single header though - I have used one in a previous project, although it was an in-house fork of UnitTest++.
Péter Török