Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

failing a XCTestCase with assert without the test continuing to run but without stopping other tests

Tags:

I'm trying to test my application using the XCTest framework.

I want my single test case to fail if some logical condition holds (using an assertion). I don't want the rest of the code in the test case to run, because this might lead to problems (access to null pointers, for example) I also want the rest of the test case to run normally, and just the failed test to be marked as failed.

I've noticed XCTestCase has a property called continueAfterFailure. However, setting it to YES caused the failed test to continue executing lines after the assertion, and setting it to NO caused the rest of the tests not to run at all.

Is there a solution to this issue?

like image 236
Yoav Schwartz Avatar asked Jan 08 '14 14:01

Yoav Schwartz


People also ask

What will happen if assert fails?

The assert Statement If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError. AssertionError exceptions can be caught and handled like any other exception using the try-except statement, but if not handled, they will terminate the program and produce a traceback.

What expected failure?

An object that represents an expected test failure. Options that determine how the test matches the expected failure to an actual test failure, and whether an unfulfilled expected failure results in a test failure.

What is the test method to test an asynchronous operation in XCTest?

XCTest provides two approaches for testing asynchronous code. For Swift code that uses async and await for concurrency, you mark your test methods async or async throws to test asynchronously.

How do I use XCTSkip?

You need to throw the XCTSkip , since it is an Error , simply creating it has no effect on your test outcome. Or you can use the XCTSkipIf function, which skips the test in case the passed in condition is met. So in the tests you want to skip, just call XCTSkipIf(shouldSkip) .


1 Answers

Pascal's answer gave me the idea to achieve this properly. XCTool now behaves like OCUnit when an assertion fails: the execution of the test case is aborted immediately, tearDown invoked and the next test case is run.

Simply override the method invokeTest in your base class (the one that inherits from the XCTestCase class):

- (void)invokeTest
{
    self.continueAfterFailure = NO;

    @try
    {
        [super invokeTest];
    }
    @finally
    {
        self.continueAfterFailure = YES;
    }
}

That's it!

like image 54
Oscar Hierro Avatar answered Oct 16 '22 04:10

Oscar Hierro