为所有测试用例初始化一次对象

问题描述 投票:0回答:1

在下面的代码中,

appSettings
变量针对每个测试用例进行初始化。如何让它为整个测试类初始化一次?如果同时运行多个测试类,那么使用单例将会是一个问题,因为它将为所有测试类初始化一次

final class TestsmthTests: XCTestCase {
        
        var appSettings = AppSettings()
    
        override func setUpWithError() throws {
    
        }
    
        func testExample() throws {
            
        }
        
        func testMore() throws {
            
        }
    }
ios swift xctest xctestcase
1个回答
0
投票

将其设为

XCTestCase
类的静态变量。

final class TestsmthTests: XCTestCase {
        
        static var appSettings = AppSettings()
    
        override func setUpWithError() throws {
    
        }
    
        func testExample() throws {

            var foo = TestsmthTests.appSettings.someProperty()  
        }
        
        func testMore() throws {
            var foo = TestsmthTests.appSettings.someProperty()              
        }
    }

我不愿意这样做,因为测试是独立的,因此,如果您在测试中修改

appSettings
,您可能会开始看到问题。

© www.soinside.com 2019 - 2024. All rights reserved.