2017-04-04 23 views
2

我知道一个类是未来对象的蓝图,我试图更好地理解使用Swift的OOP体系结构。所以我的问题是,从类和实例角度运行测试时发生的过程是什么。我不认为我实际上已经创建了我的XCTestCase子类的实例,但Xcode似乎自动执行此操作。由于我正在构建更多激情项目应用程序,我通常必须创建一个实例才能使用它,但在测试中我没有那种感觉,并且它只是通过点击(Command + U)而发挥作用。我想了解一个实例是否甚至可以创建,如果是这样的话?XCTestCase子类如何实例化的过程是什么?

这里有一个蓝图XCTestCase子类的一些示例代码,但我从来没有真正实例化这个类:

import XCTest 
@testable import FirstDemo 

class FirstDemoTests: XCTestCase { 

    override func setUp() { 
     super.setUp() 
     // Put setup code here. This method is called before the invocation of each test method in the class. 
    } 

    override func tearDown() { 
     // Put teardown code here. This method is called after the invocation of each test method in the class. 
     super.tearDown() 
    } 

    func testExample() { 
     // This is an example of a functional test case. 
     // Use XCTAssert and related functions to verify your tests produce the correct results. 
    } 

    func testPerformanceExample() { 
     // This is an example of a performance test case. 
     self.measure { 
      // Put the code you want to measure the time of here. 
     } 
    } 

} 

回答

1

XCTestCase类以同样的方式为所有其他类实例化。

它只是它们在separate process中创建,一切都由XCTest框架管理,要么运行所有测试,并且与测试目标相关的所有类都将被实例化,要么选择单独的测试,并要实例化单独的类。

你可以调查XCTest源代码在这里:当你运行你的应用程序https://github.com/apple/swift-corelibs-xctest

1

,你的代码是不负责创建应用程序的委托:UIKit框架承担这个责任。

同样,当您运行测试时,测试运行器负责实例化您的测试用例。它通过搜索所有加载的类的列表来发现它们,这些类是一种XCTestCase。然后它要求每个类的测试调用。然后它可以为这些测试方法创建测试用例实例并运行测试。

这是如何工作的关键是Objective-C运行时提供的丰富元数据以及它提供的用于查询和处理该信息的元编程接口。

相关问题