简体   繁体   English

将Xcode 5中的各个XCTest单元测试用例定位到通用应用程序的特定iOS设备?

[英]Target individual XCTest unit test cases in Xcode 5 to a specific iOS device for a universal app?

I'm building a universal iPhone/iPad application, and the UI is significantly different in some cases (ie on iPhone we use a Master/Detail paradigm with TableViews, whereas on iPad we're using a CollectionView with cells that contain the details). 我正在构建一个通用的iPhone / iPad应用程序,并且在某些情况下,UI明显不同(即,在iPhone上,我们将Master / Detail范例与TableViews一起使用,而在iPad上,我们将CollectionView与包含详细信息的单元格一起使用) 。

Because the interfaces are so different, it changes how the unit tests for the view controllers are constructed. 因为接口是如此不同,所以它改变了构造视图控制器的单元测试的方式。 In the collection view, we'd have to test to make sure the cells get built correctly and contain all the detailed information, whereas on iPhone we'd need two sets of tests - one for the master list and one for the detail screen. 在集合视图中,我们必须进行测试以确保正确构建了单元并包含所有详细信息,而在iPhone上,我们需要进行两组测试-一组用于主列表,一组用于详细屏幕。

So my question is, in a universal application being built using Xcode 5 with XCTest framework, can you target individual unit test cases / classes to be run on specific physical devices? 所以我的问题是,在使用Xcode 5和XCTest框架构建的通用应用程序中,您可以针对要在特定物理设备上运行的单个单元测试用例/类吗?

I came across a similar question here - iOS Unit testing universal application - but the asker had different intentions and was using older technologies. 我在这里遇到了一个类似的问题-iOS单元测试通用应用程序 -但是问问者的意图不同,并且正在使用较旧的技术。

I've also seen recommendations to do a check in individual test functions for UserInterfaceIdiom but that seems really crude. 我还看到了建议对UserInterfaceIdiom的单个测试功能进行检查的建议,但这似乎很粗糙。 Ideally, there would be an annotation that would designate specific tests run on specific devices, or be able to designate an entire test class for a specific device. 理想情况下,应该有一个注释,用于指定在特定设备上运行的特定测试,或者能够为特定设备指定整个测试类别。

Thanks in advance for your suggestions. 预先感谢您的建议。

--EDIT-- - 编辑 -

@TommieC led me down what seems to be the appropriate path, if a smidge manual. @TommieC引导我走了一条合适的道路,如果是一些小手册。 I created two new testing specific schemes, one for phone-sized devices, and one for tablet-sized devices. 我创建了两种新的测试专用方案,一种用于手机大小的设备,另一种用于平板电脑的设备。 Inside those schemes, I edited the test portion and added in the core tests along with the device-specific tests. 在这些方案中,我编辑了测试部分,并将其与特定于设备的测试一起添加到了核心测试中。

I then have jenkins execute two build steps, targeting the appropriate devices to test for the particular scheme. 然后,我让詹金斯执行两个构建步骤,将适当的设备作为目标以测试特定的方案。 The downside to this approach is two build steps. 这种方法的缺点是两个构建步骤。 However, I plan to split up the jenkins job and run it on 2 slaves with the appropriate devices hooked to each to parallelize the testing. 但是,我计划拆分jenkins作业,并在2个从站上运行它,并在每个从站上连接适当的设备以并行化测试。

AFAIK this will also be Xcode Build Services friendly, as it works based off of schemes in a project as well. AFAIK还将对Xcode Build Services友好,因为它也基于项目中的方案工作。 It's really too bad there isn't something more elegant built into XCTest for this, considering this doesn't scale terribly well in the event a new device type comes into play (watch/tv/hair dryer/etc.) 实在太糟糕了,XCTest并没有内置更优雅的功能,考虑到在使用新设备类型(手表/电视/吹风机/等)的情况下,缩放效果不佳

To target device specific tests one would need to edit the schemes for a project. 要针对特定​​设备的测试,需要编辑项目的方案。 Under Product > Scheme > Edit Schemes one can choose to select device specific tests per device. 在产品>方案>编辑方案下,可以选择为每个设备选择特定于设备的测试。

方案编辑

Heres my proposed solution. 这是我提出的解决方案。

Split your tests up into iPhone specific and iPad specific tests. 将您的测试分为iPhone特定测试和iPad特定测试。

Now add a new Target (Cocoa Touch Unit Testing Bundle) specifically for iPhone or iPad. 现在添加专门用于iPhone或iPad的新Target(可可触摸单元测试套件)。 If you already have many common tests written, it may be more prudent to duplicate your current test target. 如果您已经编写了许多通用测试,则最好还是复制当前的测试目标。

Now ensure that your iPhone specific test classes are only included in your iPhone test target by clicking on the class in the Navigator, and then open up the Utilities panel. 现在,通过单击导航器中的类,然后打开“实用程序”面板,确保仅将iPhone特定的测试类包括在iPhone测试目标中。 You can set which target your class is a member of by using the Target Membership check boxes. 您可以使用“目标成员资格”复选框设置班级所属的目标。

目标会员面板

To expand on this you can add different schemes for your two targets to make running the tests quicker. 要对此进行扩展,可以为两个目标添加不同的方案,以更快地运行测试。

I was looking to do this same thing and came across this old post. 我一直想做同样的事情,并且遇到了这个旧帖子。 My solution involves naming specific device tests to end in either "Pad" or "Phone" and then filtering the list of tests to include the current device's tests: 我的解决方案涉及将特定的设备测试命名为“ Pad”或“ Phone”结尾,然后过滤测试列表以包括当前设备的测试:

class MyTests: XCTestCase {
    override class var defaultTestSuite: XCTestSuite {
        let suite = XCTestSuite(forTestCaseClass: MyTests.self)

        let newSuite = XCTestSuite(name: "MyTests")
        for test in suite.tests {
            // Name is of the form "-[MyTests test*]"
            if test.name.hasSuffix("Pad]") {
                // iPad only test
                if UIDevice.current.userInterfaceIdiom == .pad {
                    newSuite.addTest(test)
                }
            }
            else if test.name.hasSuffix("Phone]") {
                // iPhone only test
                if UIDevice.current.userInterfaceIdiom == .phone {
                    newSuite.addTest(test)
                }
            }
            else {
                // Can run on both devices
                newSuite.addTest(test)
            }
        }

        return newSuite
    }

    func testAll() { // All devices test }
    func testOnlyPad() { // iPad only test }
    func testOnlyPhone() { // iPhone only test }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM