【问题标题】:xctestcase run tests when a pretest is passedxctestcase 在预测试通过时运行测试
【发布时间】:2015-12-19 07:45:42
【问题描述】:

我在 XCTestCase 类中有几个测试用例,例如测试1,测试2等 仅当通过 testPrecondition 时,我才想运行 test1, test2。我怎样才能做到这一点?

【问题讨论】:

    标签: ios objective-c xcode macos xctest


    【解决方案1】:

    你必须重写 XCtest 的 testInvocations 类方法。以下示例来自Github 代码一目了然。

    + (NSArray *)testInvocations
    {
        BOOL onlineTests = [[[[NSProcessInfo processInfo] environment] objectForKey:@"ONLINE_TESTS"] boolValue];
        if (!onlineTests)
            return [super testInvocations];
    
        NSMutableArray *testInvocations = [NSMutableArray new];
        for (NSInvocation *invocation in [super testInvocations])
        {
            if (![NSStringFromSelector(invocation.selector) hasSuffix:offlineSuffix])
                [testInvocations addObject:invocation];
        }
        return [testInvocations copy];
    }
    

    如果您想决定在运行时运行哪个测试 => 您的测试中有代码异味(依赖性),这意味着您做错了测试。

    【讨论】:

    • 依赖?例如该应用程序使用智能卡读卡器。可以连接两个读卡器。只有当系统发现连接了两个阅读器时,才应运行使用两个阅读器的测试。我不希望两个因此而未能通过整个自动测试。
    • 如果您将其作为集成测试进行,则完全可以。如果您使用真正的读卡器进行标准测试,那么您的测试是错误且缓慢的(读卡器是依赖项)。
    猜你喜欢
    • 2013-04-02
    • 2020-06-14
    • 2017-10-12
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    相关资源
    最近更新 更多