iOS 使用 XCTest 和单独的文件进行测试

iOS testing with XCTest and separate file

目前,我在 1 个文件中编写测试(默认)。是否可以有多个文件并进行测试?

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (void)testExample {
    // This is an example of a functional test case.
    XCTAssert(YES, @"Pass");
}

您可以有很多文件,每个文件可以有很多测试。第一个只是 Xcode 自动生成的那个,但它的特别之处在于:

  1. 实现 XCTestCase 的子类
  2. 是测试目标的成员(不是您的应用目标)

制作另一个具有相同特征的文件很简单。

在 Xcode 中,文件 -> 新建 -> iOS -> 源 -> UI 测试用例 Class

惯例是将相关测试分组到一个源文件中,然后将该源文件命名为___Tests.swift(或.m)

例如,SharingTests.swift 可能包含方法 testSharingToFacebook()testSharingToTwitter()