cocoa - 使用多个 XCTestCase 类进行 UITesting

标签 cocoa compiler-errors xcode-ui-testing duplicate-symbol modularization

我正在为我的项目编写 UITest 案例。因此,我希望将多个文件子类化到 XCTestCase 或子类化到我的其他测试类。每当我创建这样的文件时,我都会收到以下错误。

duplicate symbol _lastUsedSaveDirectory in:
/Users/UserName/Library/Developer/Xcode/DerivedData/ProjectName/Build/Intermediates/CodeCoverage/Intermediates/Project.build/Debug/Project_UITests.build/Objects-normal/x86_64/SecondaryFile.o
/Users/UserName/Library/Developer/Xcode/DerivedData/ProjectName/Build/Intermediates/CodeCoverage/Intermediates/Project.build/Debug/Project_UITests.build/Objects-normal/x86_64/MainFile.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我尝试了以下方法,但没有任何效果:
1. Created "SecondaryFile" by subclassing it from "MainFile" which is a subclass of "XCTestCase".
2. Created "SecondaryFile" by subclassing it directly from "XCTestCase".
3. Created both .h and .m file for both the "MainFile" and "SecondaryFile"

主文件.m
#import <XCTest/XCTest.h>

@interface MainFile_UITests : XCTestCase
@end

@implementation MainFile_UITests

- (void)setUp {
        [super setUp];

        // Put setup code here. This method is called before the invocation of each test method in the class.

        // In UI tests it is usually best to stop immediately when a failure occurs.
        self.continueAfterFailure = NO;
        // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
        [[[XCUIApplication alloc] init] launch];
        // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}

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

- (void)testExample {
    // Use recording to get started writing UI tests.
        // Use XCTAssert and related functions to verify your tests produce the correct results.    
}

@end

次要文件.m
#import <XCTest/XCTest.h>

@interface SecondaryFile_UITests : XCTestCase
@end

@implementation SecondaryFile_UITests

- (void)setUp {
        [super setUp];

        // Put setup code here. This method is called before the invocation of each test method in the class.

        // In UI tests it is usually best to stop immediately when a failure occurs.
        self.continueAfterFailure = NO;
        // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
        [[[XCUIApplication alloc] init] launch];
        // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}

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

@end

有人可以告诉我如何模块化/创建多个文件 UITesting 类。

最佳答案

为了使用多个 UI 测试文件,您应该首先为 UITests.m 创建一个头文件。 ,然后在您单独的测试文件中,继承自 UITest.h .这将为您提供 setUptearDown来自 UITests.m 的方法在你的子类中。然后,您所要做的就是将新的测试方法添加到您的子类中。

例如,下面是 Objective-C 中的子类 UITests 文件的示例:
https://gist.github.com/OffensivelyBad/012043f8dab50b3b024238e985462926

关于cocoa - 使用多个 XCTestCase 类进行 UITesting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41235346/

相关文章:

macos - cocoa 界面生成器中的发送操作和接收操作

ios - Appcelerator - 将新 UDID 添加到配置文件后的 CodeSign 问题

swift - NSTableView 不会调用 viewForTableColumn

cocoa - 是否有未弃用的方法来访问 OS X 10.11 的字体集合?

c# - 由于<,>和$符号而导致无法编译代码

java - 无法从 teaminfo 类型对非静态方法acceptadd(String) 进行静态引用

ios - 使用 UIPickerView 进行 UI 测试时出现 "Failed to get refreshed snapshot"错误

ios - 使用 pod 中的测试代码在物理设备上运行 Xcode ui 测试

ios - 让 XCUIElement 为自动化 UITest 执行 3D 触摸?

objective-c - 使用 "unsigned char"指向原始数据缓冲区时需要了解内存管理