How to call XCTest test case functions by order in Xcode 8?

孤人 提交于 2019-12-02 02:10:58

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() { ... }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!