Any way to run code in SenTest only on a success?

231 Views Asked by At

In my Mac Cocoa unit tests, I would like to output some files as part of the testing process, and delete them when the test is done, but only when there are no failures. How can this be done (and/or what's the cleanest way to do so)?

2

There are 2 best solutions below

0
On BEST ANSWER

Your question made me curious so I looked into it!

I guess I would override the failWithException: method in the class SenTestCase (the class your tests run in inherits from this), and set a "keep output files" flag or something before calling the super's method.

Here's what SenTestCase.h says about that method:

/*"Failing a test, used by all macros"*/
- (void) failWithException:(NSException *) anException;

So, provided you only use the SenTest macros to test and/or fail (and chances are this is true in your case), that should cover any test failure.

0
On

I've never dug into the scripts for this, but it seems like you could customize how you call the script that actually runs your tests to do this. In Xcode 4, look at the last step in the Build Phases tab of your test target. Mine contains this:

# Run the unit tests in this test bundle.
"${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests"

I haven't pored through the contents of this script or the many subscripts it pulls in on my machine, but presumably they call otest or some other test rig executable and the test results would be returned to that script. After a little time familiarizing yourself with those scripts you would likely be able to find a straightforward way to conditionally remove the output files based on the test results.