Alexander N.
Alexander N.

Reputation: 1592

Run iterative tests in XCTestCase on the same test

I am attempting to run a UI Test in XCTestCase and when I am running it, I would like to run it multiple times with multiple different inputs. Is there a straightforward way to run a test multiple times with different inputs?

I want to run a test with different inputs but only write the test once. An example of this would be that I am trying to pass different user names in to verify behavior.

Upvotes: 2

Views: 1019

Answers (2)

Soumya Mahunt
Soumya Mahunt

Reputation: 2762

Instead of directly running your assertions for each input in the same test I will advise to use XCTContext.runActivity to specify activity name for each parameter input:

func testToBeLooped(parameter: Int) {
    print(parameter)
    XCTAssertEqual(0, parameter % 2)
}

func testLoop() {
    let myParameters = [1, 2, 3]
    for parameter in myParameters {
        XCTContext.runActivity(named: "Testing for parameter \(parameter)") { activity in
            setUp()
            testToBeLooped(parameter: parameter)
            tearDown()
        }
    }
}

This allows you to visualize exactly which input parameters didn't pass assertions and which of the assertions failed for each in the test report:

Testing assertion with activity

Upvotes: 1

atgrubb
atgrubb

Reputation: 1203

It's kind of sloppy because it executes setUp() once at the beginning unnecessarily but otherwise it gets the job done...

func testLoop() { 
    for parameter in myParameters {
       setUp()
       testToBeLooped(parameter: parameter)
       tearDown()
    }
}

Upvotes: 1

Related Questions