XCTest test functions was called in alphabetical order (before Xcode 8). With Xcode 8, I can not assume in which order the test cases are called by the system.
Can someo
Tests within a class are run in a random order in Xcode 8. This encourages tests to be independent and repeatable.
I'm assuming that you want to run your tests in a specific order because they "chain" off of each other. For example, test_A
logs a fake user in and test_B
adds an item to the shopping cart. These type of tests should be avoided because they depend too much on each other. What if you want to run test_F
alone? You shouldn't have to run A
through E
just to validate that F
still works. Also, you could be introducing test pollution that is affecting other tests in ways you aren't yet aware of.
That said, having common or shared behavior between tests is fine, even ecouraged. You can put this login in the setUp
method or extract private helper methods to handle specific events. For example, here's a very high-level example.
class AppTests: XCTestCase {
let subject = ViewController()
func setUp() {
super.setUp()
login(email: "user@example.com", password: "password")
}
func test_AddingItemsToCart() {
addItemToCart(21)
XCTAssertEqual(subject.itemsInCartLabel.text, "1")
}
func test_Checkout() {
addItemToCart(15)
checkout()
XCTAssertEqual(subject.totalPriceLabel.text, "$21")
}
private func login(email: String, password: String) { ... }
private func addItemToCart(item: Int) { ... }
private func checkout() { ... }
}