测试运行器在完成 运行 测试前以代码 9 退出

The test runner exited with code 9 before finishing running tests

当 Xcode 在执行 WatchOS 测试期间崩溃时,代码 9 表示什么(以及如何解决该问题)?

场景:

我有一个 XCTestCase 可以读取大约 100 个 CSV 测试资源文件。这些文件以逗号分隔,大约有 6,000 行,平均大小为 64K。在我的测试用例中,我将这些文件读入内存,我的一个,我验证输入文件(经过一些处理)与输出文件匹配。下面是一些演示流程的代码:

import XCTest
@testable import MyWatch_Extension

class MyTest: XCTestCase {
    

    func testMyAlgo() throws {
        let testingData = discoverAvailableTestFiles(filter: "dataset_1");
        XCTAssertGreaterThan(testingData.count, 0, "have data to process")

        for (_, testingEntry) in testingData.enumerated() {
            var input : [Double] = [];
            var expectations : [Double] = [];
            readInputOutputTestData(entries: &input, fileName: testingEntry +  "_input");
            readInputOutputTestData(entries: &expectations, fileName: testingEntry + "_expected_output");
            
            // do something with the input, and store it into results
            let results = MyAglo().doSomething();

            compareResultsAndExpectations(testingEntry: testingEntry, results: results, expectations: expectations);
        }
    }

    func discoverAvailableTestFiles(filter: String) -> Set<String> {
        let bundle = Bundle(for: type(of: self))
        let paths = bundle.paths(forResourcesOfType: "csv", inDirectory: nil);
        var results = Set<String>()
        for path in paths {
            if (path.contains(filter)) {
                let fileNameSubstring = path[path.index(path.lastIndex(of: "/")!, offsetBy: 1)...]
                let qaFileName = fileNameSubstring[...fileNameSubstring.index(fileNameSubstring.lastIndex(of: "_")!, offsetBy: -1)]
                results.insert(String(qaFileName))
            }
        }
        return results;
    }

    func readInputOutputTestData(entries : inout [Double], fileName : String) {
        let bundle = Bundle(for: type(of: self))
        let path = bundle.path(forResource: fileName, ofType: "csv")!
        do {
            let data = try String(contentsOfFile: path, encoding: .utf8)
            let myStrings = data.components(separatedBy: .newlines);
            for idx in 0..<myStrings.count {
                let parts = myStrings[idx].split(separator: ",");
                if (parts.count > 0) {
                    for part in parts {
                        entries.append((part as NSString).doubleValue);
                    }
                }
            }
        } catch {
            print(error)
        }
    }
    
    func compareResultsAndExpectations(testingEntry: String, results: [Double], expectations: [Double]) {
        print("## testing \(testingEntry)");
        XCTAssertEqual(results.count, expectations.count / 3, "mismatch in data count \(testingEntry)")
        var counter = 0;
        for idx in stride(from: 0, to: expectations.count, by: 3) {
            XCTAssertEqual(results[counter], expectations[idx], accuracy: 0.5, "\(idx + 1) value mismatch")
            counter += 1;
        }
    }

}

当我执行 testMyAlgo 测试用例时,我可能会读取前 20 个文件,然后收到错误消息:

The test runner exited with code 9 before finishing running tests.

如果我 运行 每个文件单独或以较小的批次(可能只有 20 个,而不是整个循环 100 个),一切都很好。这让我相信我正在耗尽手表的内存 space 或者应该以不同的方式执行测试用例。知道问题是什么,或者我应该如何重新构建测试用例以摆脱这个错误? (可能在每个测试用例之前提供免费资源或类似的东西)

您可以创建一个愚蠢的 Xcode 项目,该项目 运行 在 iPhone 上 copy/paste 只有这个测试。如果 运行 没问题,则表示您超出了手表的限制。如果是这种情况,您可以使用您的算法和 运行 框架上的测试创建一个框架,然后将框架导入您的手表扩展