0

So Apple has an example of how the setUp and TearDown methods should be used that looks like this:

    class SetUpAndTearDownExampleTestCase: XCTestCase {
    
    override class func setUp() { // 1.
        // This is the setUp() class method.
        // It is called before the first test method begins.
        // Set up any overall initial state here.
    }
    
    override func setUpWithError() throws { // 2.
        // This is the setUpWithError() instance method.
        // It is called before each test method begins.
        // Set up any per-test state here.
    }
    
    override func setUp() { // 3.
        // This is the setUp() instance method.
        // It is called before each test method begins.
        // Use setUpWithError() to set up any per-test state,
        // unless you have legacy tests using setUp().
    }
    
    func testMethod1() throws { // 4.
        // This is the first test method.
        // Your testing code goes here.
        addTeardownBlock { // 5.
            // Called when testMethod1() ends.
        }
    }
    
    func testMethod2() throws { // 6.
        // This is the second test method.
        // Your testing code goes here.
        addTeardownBlock { // 7.
            // Called when testMethod2() ends.
        }
        addTeardownBlock { // 8.
            // Called when testMethod2() ends.
        }
    }
    
    override func tearDown() { // 9.
        // This is the tearDown() instance method.
        // It is called after each test method completes.
        // Use tearDownWithError() for any per-test cleanup,
        // unless you have legacy tests using tearDown().
    }
    
    override func tearDownWithError() throws { // 10.
        // This is the tearDownWithError() instance method.
        // It is called after each test method completes.
        // Perform any per-test cleanup here.
    }
    
    override class func tearDown() { // 11.
        // This is the tearDown() class method.
        // It is called after all test methods complete.
        // Perform any overall cleanup here.
    }
    
}

As you can see in the first method override class func setUp() overall inital state is suppost to be set. I have a simple setup phase for the application. But if this fails in some way. Then the test just get ignored like it never ran instead of marked as failure.

This is what the log says:

:0: error: simpleUITest : Failed to get matching snapshot: No matches found for first query match sequence: Descendants matching type Cell -> Elements matching predicate '"login_button" IN identifiers', given input App element pid: 9849 (no attribute values faulted in) Test Suite 'simpleUITest' failed at 2021-01-20 10:49:42.684. Executed 0 tests, with 1 failure (0 unexpected) in 0.000 (13.204) seconds

How can I get this to report as a test failure rather then just having XCTest ignore it?

sago92
  • 51
  • 6

1 Answers1

0

Based on the docs, you have at least two options to fail a test from setUp:

Before each test begins, XCTest calls setUpWithError(), followed by setUp(). If state preparation might throw errors, override setUpWithError(). XCTest marks the test failed when it catches errors, or skipped when it catches XCTSkip.

and

Tip You can include test assertions in a test case's setUp(), setUpWithError(), tearDown(), and tearDownWithError() instance methods. Any such assertions will be evaluated as part of every test method's run.

It isn't clear what you tried from your question. Do you have an assertion in setUp as suggested in the second option? Note that this only applies to the non-class setUp

N Brown
  • 507
  • 3
  • 9