如何在Xcode 8中按顺序调用XCTest测试用例函数? [英] How to call XCTest test case functions by order in Xcode 8?

查看:152
本文介绍了如何在Xcode 8中按顺序调用XCTest测试用例函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XCTest测试函数按字母顺序调用(在Xcode 8之前)。使用Xcode 8,我无法假定系统调用测试用例的顺序。



有人可以指出它吗?
在类中的测试在Xcode 8中以随机顺序运行。这鼓励测试独立于 可重复 / em>。




我假设您想按特定顺序运行测试,因为它们是链彼此关闭。例如, test_A 记录一个假用户,并且 test_B 将一个项目添加到购物车。应该避免这些类型的测试,因为它们相互依赖太多。如果你想单独运行 test_F 呢?您不应该通过 E 运行 A 来验证 F 仍然有效。此外,您可能会引入测试污染,影响其他测试的方式,你还没有意识到。

也就是说,测试之间有共同或共享的行为是好的,甚至不鼓励。您可以将此登录信息放入 setUp 方法或解压私人帮手方法来处理特定事件。例如,下面是一个 高级示例。

 类AppTests:XCTestCase {
)让subject = ViewController()

func setUp(){
super.setUp()
登录(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(){。 ..}
}


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 someone throw light on it?

解决方案

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() { ... }
}

这篇关于如何在Xcode 8中按顺序调用XCTest测试用例函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆