我有以下问题。我想在执行所有测试类之前执行一段代码。例如:我不希望我的游戏在执行期间使用 SoundEngine 单例,而是使用 SilentSoundEngine。我想一次激活 SilentSoundEngine 不是在所有测试中。我所有的测试都是这样的:
class TestBasketExcercise : XCTestCase {
override func setUp() {
SilentSoundEngine.activate () // SoundEngine is a singleton
}
// The tests
}
-编辑-
大多数答案都针对为 TestCase 提供自定义父类(super class)。我正在寻找一种更通用、更简洁的方法来提供所有测试都需要执行的环境。某处没有用于测试的“主要”功能/Appdelegate 之类的功能吗?
最佳答案
来自 Writing Test Classes and Methods :
You can optionally add customized methods for class setup
(+ (void)setUp)
and teardown(+ (void)tearDown)
as well, which run before and after all of the test methods in the class.
在 Swift 中,这将是
class
方法:override class func setUp() {
super.setUp()
// Called once before all tests are run
}
override class func tearDown() {
// Called once after all tests are run
super.tearDown()
}
关于ios - 如何在执行任何 XCTest 之前运行一次性设置代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29822457/