ozd
ozd

Reputation: 1284

Skip multi tests in Xcode

I'm writing unit tests in my Swift app.

I have tests that i want to skip over, using the flag shouldSkip, something like -

if shouldSkip {
     throw XCTSkip()
}

The problem is - I don't want all the tests to have to write this line over and over + all the skipped test needs to be marked as "skipped" (the gray arrow, NOT pass or fail).

So I thought about override invokeTest or using the setup to skip all the tests when the flag in true, something like -

override func setUp() {
        
        if shouldSkip {
          throw XCTSkip()
        } 
        super.setUp()
}

but i couldn't make the tests to be marked as skipped (only pass or fail)

Any ideas on how to skip (and mark as skipped) a bulk of tests (in the same file or not)?

I know about test plans and schemes but i need the tests to run and be skipped in oppose to not running the tests.

Upvotes: 1

Views: 2433

Answers (1)

David Pasztor
David Pasztor

Reputation: 54706

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). Or if you want to avoid having to write it in all tests, you can do it in setUpWithError, which is the throwing variant of setup.

Instead of doing if shouldSkip { XCTSkip() }, the correct approach is either XCTSkipIf(shouldSkip) or if shouldSkip { throw XCTSkip() }.

Using one of these 2 solutions will correctly mark the tests as skipped.

For more info, see the Methods for Skipping Tests doc page.

Upvotes: 2

Related Questions