【问题标题】:The test runner exited with code 9 before finishing running tests测试运行程序在完成运行测试之前以代码 9 退出
【发布时间】:2021-11-18 08:15:00
【问题描述】:

当 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 的整个循环),一切都很好。这让我相信我正在耗尽手表的内存空间,或者应该以不同的方式执行测试用例。知道问题是什么,或者我应该如何重新构建测试用例以摆脱这个错误? (在每个测试用例之前可能有免费资源或类似的东西)

【问题讨论】:

    标签: swift watchkit xctest


    【解决方案1】:

    您可以创建一个在 iPhone 上运行的哑 Xcode 项目,然后只复制/粘贴这个测试。如果运行正常,则意味着您超出了手表的限制。如果是这种情况,您可以使用您的算法创建一个框架并在该框架上运行测试,然后在您的手表扩展中导入该框架

    【讨论】:

    • 这可能是最好的方法,即使它没有超过实际的错误。感谢您分享您的想法。
    • 我不再确定它是否可行:stackoverflow.com/questions/45498554/…
    • 引用该链接答案的评论“您可以做的是在不同目标之间共享您的源代码,并使用 TargetConditionals 以正确的平台编译这些源代码”。不过,我不确定这是如何阻止愚蠢项目的想法,因为它只是为了找出问题所在
    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2019-05-19
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多