ios - 如何动态添加 XCTestCase

标签 ios swift xcuitest xctestcase

我正在为一个白标项目编写 UI 测试,其中每个应用程序都有一组不同的菜单项。测试点击每个菜单项并截取屏幕截图(使用 fastlane snapshot )。

目前这一切都发生在一个名为 testScreenshotAllMenuItems()XCTestCase 中,如下所示:

func testScreenshotAllMenuItems() {
    // Take a screenshot of the menu
    openTheMenu()
    snapshot("Menu")
    var cells:[XCUIElement] = []

    // Store each menu item for use later
    for i in 0..<app.tables.cells.count {
        cells.append(app.tables.cells.element(boundBy: i))
    }

    // Loop through each menu item
    for menuItem in cells.enumerated() {
        let exists = menuItem.element.waitForExistence(timeout: 5)
        if exists && menuItem.element.isHittable {
            // Only tap on the menu item if it isn't an external link
            let externalLink = menuItem.element.children(matching: .image)["external link"]
            if !externalLink.exists {
                var name = "\(menuItem.offset)"
                let cellText = menuItem.element.children(matching: .staticText).firstMatch
                if cellText.label != "" {
                    name += "-\(cellText.label.replacingOccurrences(of: " ", with: "-"))"
                }
                print("opening \(name)")
                menuItem.element.tap()
                // Screenshot this view and then re-open the menu
                snapshot(name)
                openTheMenu()
            }
        }
    }
}

我希望能够动态生成每个屏幕截图,因为它是自己的测试用例,以便将这些屏幕截图正确地报告为单独的测试,可能是这样的:

[T] Screenshots
    [t] testFavouritesViewScreenShot()        ✓
    [t] testGiveFeedbackViewScreenShot()      ✓
    [t] testSettingsViewScreenShot()          ✓

我看过关于 creating tests programmatically 的文档但我不确定如何快速设置它。 - 理想情况下,我会使用闭包将现有的屏幕截图测试包装到它们自己的 XCTestCase 中 - 我想象如下,但似乎没有任何有用的初始化方法来实现这一点:

for menuItem in cells {
    let test = XCTestCase(closure: {
        menuItem.tap()
        snapshot("menuItemName")
    })
    test.run()
}

我不理解文档建议使用的调用和选择器的组合,我找不到任何好的示例,请为我指明正确的方向,或者分享您拥有的有关此工作的任何示例。

最佳答案

NSInvocation 以来,您可能无法在纯 swift 中完成此操作不再是 swift api 的一部分。

XCTest依赖+ (NSArray<NSInvocation *> *)testInvocations获取一个内部测试方法列表的函数 XCTestCase类(class)。您可以假设的默认实现只是找到所有以 test 开头的方法前缀并将它们包裹在 NSInvocation 中返回. (您可以阅读更多关于 NSInvocation here 的信息)
所以如果我们想在运行时声明测试,这就是我们感兴趣的地方。
遗憾NSInvocation不再是 swift api 的一部分,我们无法覆盖此方法。

如果您可以使用一点点 ObjC,那么我们可以创建隐藏 NSInvocation 细节的父类(super class),并为子类提供快速友好的 api。

/// Parent.h

/// SEL is just pointer on C struct so we cannot put it inside of NSArray.  
/// Instead we use this class as wrapper.
@interface _QuickSelectorWrapper : NSObject
- (instancetype)initWithSelector:(SEL)selector;
@end

@interface ParametrizedTestCase : XCTestCase
/// List of test methods to call. By default return nothing
+ (NSArray<_QuickSelectorWrapper *> *)_qck_testMethodSelectors;
@end
/// Parent.m

#include "Parent.h"
@interface _QuickSelectorWrapper ()
@property(nonatomic, assign) SEL selector;
@end

@implementation _QuickSelectorWrapper
- (instancetype)initWithSelector:(SEL)selector {
    self = [super init];
    _selector = selector;
    return self;
}
@end

@implementation ParametrizedTestCase
+ (NSArray<NSInvocation *> *)testInvocations {
    // here we take list of test selectors from subclass
    NSArray<_QuickSelectorWrapper *> *wrappers = [self _qck_testMethodSelectors];
    NSMutableArray<NSInvocation *> *invocations = [NSMutableArray arrayWithCapacity:wrappers.count];

    // And wrap them in NSInvocation as XCTest api require
    for (_QuickSelectorWrapper *wrapper in wrappers) {
        SEL selector = wrapper.selector;
        NSMethodSignature *signature = [self instanceMethodSignatureForSelector:selector];
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
        invocation.selector = selector;

        [invocations addObject:invocation];
    }

    /// If you want to mix parametrized test with normal `test_something` then you need to call super and append his invocations as well.
    /// Otherwise `test`-prefixed methods will be ignored
    return invocations;
}

+ (NSArray<_QuickSelectorWrapper *> *)_qck_testMethodSelectors {
    return @[];
}
@end

所以现在我们的 swift 测试类只需要从这个类继承并覆盖 _qck_testMethodSelectors :

/// RuntimeTests.swift

class RuntimeTests: ParametrizedTestCase {

    /// This is our parametrized method. For this example it just print out parameter value
    func p(_ s: String) {
        print("Magic: \(s)")
    }

    override class func _qck_testMethodSelectors() -> [_QuickSelectorWrapper] {
        /// For this example we create 3 runtime tests "test_a", "test_b" and "test_c" with corresponding parameter
        return ["a", "b", "c"].map { parameter in
            /// first we wrap our test method in block that takes TestCase instance
            let block: @convention(block) (RuntimeTests) -> Void = { $0.p(parameter) }
            /// with help of ObjC runtime we add new test method to class
            let implementation = imp_implementationWithBlock(block)
            let selectorName = "test_\(parameter)"
            let selector = NSSelectorFromString(selectorName)
            class_addMethod(self, selector, implementation, "v@:")
            /// and return wrapped selector on new created method
            return _QuickSelectorWrapper(selector: selector)
        }
    }
}

预期输出:

Test Suite 'RuntimeTests' started at 2019-03-17 06:09:24.150
Test Case '-[ProtocolUnitTests.RuntimeTests test_a]' started.
Magic: a
Test Case '-[ProtocolUnitTests.RuntimeTests test_a]' passed (0.006 seconds).
Test Case '-[ProtocolUnitTests.RuntimeTests test_b]' started.
Magic: b
Test Case '-[ProtocolUnitTests.RuntimeTests test_b]' passed (0.001 seconds).
Test Case '-[ProtocolUnitTests.RuntimeTests test_c]' started.
Magic: c
Test Case '-[ProtocolUnitTests.RuntimeTests test_c]' passed (0.001 seconds).
Test Suite 'RuntimeTests' passed at 2019-03-17 06:09:24.159.

感谢 Quick 团队的 super class implementation .

编辑:我用示例创建了 repo github

关于ios - 如何动态添加 XCTestCase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55142213/

相关文章:

ios - 是否可以将 UITests 目标中的文件复制到应用程序的文档目录中?

iphone - 如何在xcode中动态改变字体

ios - 想看看是否有人知道为什么在一个项目中与另一个项目中快速创建 "ambigious use of subscript"错误

ios - Swift Closure 参数,按位置操作

ios - 尽管覆盖了 Xcode 9.3 上状态栏的颜色没有改变

swift XCTest : Verify proper deallocation of weak variables

objective-c - 向应用程序添加自定义字体

ios - -[UITableView layoutSublayersOfLayer :] on iOS 7 断言失败

java - 我想通过滑动来查找/定位 IOS 应用程序中存在的元素

ios - 如何暂停 XCUITest 以进行手动模拟器操作?