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

Kha*_*inn 4 ios xctest

目前,我在 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");
}
Run Code Online (Sandbox Code Playgroud)

div*_*gio 7

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

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

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

在Xcode中,File -> New -> iOS -> Source -> UI Test Case Class

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

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