Search code examples
xcodeunit-testingtddxctest

How to mark a XCTest as pending?


I have a subclass of XCTestCase and I would like to mark some test as pending, in the same way that is possible with RSpec

https://www.relishapp.com/rspec/rspec-core/v/2-0/docs/pending/pending-examples


Solution

  • There is no simple way to do this. The most pragmatic solution is to comment out or rename your test:

    @implementation MyTestCase
    
    - (void)testSomething { /* ... */ }
    
    // Option 1: Comment out the test.
    // - (void)testSomething { /* ... */ }
    
    // Option 2: Rename the test such that XCTest does not consider 
    // it a valid test method. In order to be "valid", it needs to:
    // 
    // - Have a return type of `void`
    // - Take no parameters
    // - Begin with "test"
    //
    // Here, we rename the test so that it does not begin with "test".
    - (void)PENDING_testSomething { /* ... */ }
    
    @end
    

    Of course, one of the nice things about RSpec is that it prints out a warning of some kind. You could add these yourself using #warning:

    #pragma Pending!
    - (void)PENDING_testSomething { /* ... */ }
    

    This displays a warning in Xcode. Keep in mind that #warning is only available in Objective-C, not Swift.

    Testing frameworks like Quick and Specta provide pending examples:

    pending("tests something") { /* ... */ }
    xit("tests something") { /* ... */ }
    xdescribe("describes something") { /* ... */ }
    xcontext("provides context for something") { /* ... */ }
    

    These test frameworks prevent "pending" tests from being run by overriding -[XCTestCase testInvocations]. Doing so is a lot of work, but if you wanted to implement "pending tests" yourself, you could look into doing so as well.