简体   繁体   English

从命令行为 iOS 应用程序运行单个 XCTest(UI、单元)测试用例

[英]Running individual XCTest (UI, Unit) test cases for iOS apps from the command line

Is it possible to run individual test cases, or individual test suites, from an iOS app test target, instead of all the test cases, from a command line interface?是否可以从命令行界面从 iOS 应用程序测试目标而不是所有测试用例运行单个测试用例或单个测试套件?

You can run tests from command line with xcodebuild , out of the box.您可以使用xcodebuild从命令行运行测试,开箱即用。 When you do so, you run all of the test cases contained in the test target you've selected.当您这样做时,您将运行您选择的测试目标中包含的所有测试用例。

You can also do so with scan from Fastlane, though I believe you're restricted to running all of the tests of the build scheme you select (as above), so it's not different from xcodebuild.您也可以使用 Fastlane 的扫描来执行此操作,但我相信您只能运行 select(如上)的构建方案的所有测试,因此它与 xcodebuild 没有什么不同。

You can run specific tests with xctool from Facebook, but it doesn't use xcodebuild, and is restricted to running on simulators only, not actual iOS test devices.您可以使用来自 Facebook 的xctool运行特定测试,但它不使用 xcodebuild,并且仅限于在模拟器上运行,而不是在实际的 iOS 测试设备上运行。

I found a reference to running the xctest command line utility directly, but it seems to be an undocumented feature and targets DerivedData.我找到了直接运行xctest命令行实用程序的参考,但它似乎是一个未记录的功能并且以 DerivedData 为目标。 This is complicated by the fact that UI Tests, have their *xctest files in a separate XCTRunner bundle .由于 UI 测试将其 *xctest 文件放在单独的 XCTRunner 包中,这使情况变得复杂。

It is now possible with Xcode 8 using the -only-testing parameter with xcodebuild :现在可以在 Xcode 8 中使用xcodebuild-only-testing参数:

xcodebuild test -workspace <path>
                -scheme <name>
                -destination <specifier>
                -only-testing:TestBundle/TestSuite/TestCase

在此处输入图片说明

You can edit the scheme to run only specific tests.您可以编辑方案以仅运行特定测试。 Select the scheme, then edit scheme.选择方案,然后编辑方案。 In the appearing window, select the Test phase and disable/enable individual tests.在出现的窗口中,选择测试阶段并禁用/启用单个测试。

在此处输入图片说明

You can also add schemes to run subsets of tests.您还可以添加方案来运行测试的子集。 When running the tests from command line you can specify the scheme to use for the test (at least in fastlane).从命令行运行测试时,您可以指定用于测试的方案(至少在 fastlane 中)。

Run an individual test运行单独的测试

To run an individual test case you can use -only-testing要运行单个测试用例,您可以使用-only-testing

-only-testing Pattern -only-testing模式

-only-testing:<target>/<class_name>/<test_name>

xcodebuild Pattern xcodebuild模式

xcodebuild test 
-workspace "<name>.xcworkspace"  
-scheme "<name>" 
-destination '<options>' 
-only-testing "<test_case>"

Example例子

//for example(several test cases)
xcodebuild test 
-workspace "MyApp.xcworkspace"
-scheme "MyAppTest" 
-destination 'platform=iOS Simulator,name=iPhone 11 Pro Max,OS=13.5' 
-only-testing "MyAppTest/TestClass/foo"
-only-testing "MyAppTest/TestClass/foo2"

For example if Test Navigator looks like例如,如果Test Navigator看起来像

the parameter will have the following type参数将具有以下类型

-only-testing:F49088168M_OBDIITests/HexUtilityTests/testHexToBinStringFormat

If you want to add an additional test case you can add one more -only-testing如果你想添加一个额外的测试用例,你可以再添加一个-only-testing

Also you can skip a test using: -skip-testing您也可以使用以下-skip-testing跳过测试: -skip-testing

Test results you can find in Derived Data [About]您可以在Derived Data找到测试结果[关于]

<derived_data>/<project_name>-dzqvyqfphypgrrdauxiyuhxkfxmg/Logs/Test/Test-<target_name>-<date>.xcresult

[Xcode screenshot] [Xcode截图]

I was in similar situation as you and have built a python script that triggers the set of test case/s that i want.我和你的情况类似,并且已经构建了一个 python 脚本来触发我想要的一组测试用例。 Its a little bit elaborate process but works for me and has been very useful over time in implementing DataProvider methods, Rerunning of failed test cases and other customizations I required.它的过程有点复杂,但对我有用,并且随着时间的推移在实现 DataProvider 方法、重新运行失败的测试用例和我需要的其他自定义方面非常有用。

Some relevant steps for what you want to achieve.您想要实现的一些相关步骤。

  1. Override testInvocations method present in XCTestCase to do below steps覆盖 XCTestCase 中存在的 testInvocations 方法以执行以下步骤
    • In this method I read Environment Variable XXXX.在这种方法中,我读取了环境变量 XXXX。
    • This environment variable is basically a comma separated test case method names.这个环境变量基本上是一个逗号分隔的测试用例方法名称。
    • Now create NSInvocations for each test method that you want to trigger.现在为要触发的每个测试方法创建 NSInvocations。
    • Return array of Invocations.返回调用数组。
  2. How to pass Environment Variable?如何传递环境变量?
    • In scheme add an environment variable named XXXX.在方案中添加一个名为 XXXX 的环境变量。
    • Scheme files are standard xml files, write a script that modifies the scheme file to contain the Comma separated values in environment variable. Scheme文件是标准的xml文件,编写脚本修改scheme文件,使其在环境变量中包含逗号分隔的值。

If you require more info add a comment I will reply to it.如果您需要更多信息,请添加评论,我会回复。

To run an individual test or test class you can click the diamond next to it in the gutter.要运行单个测试或测试类,您可以单击装订线中它旁边的菱形。 This is right next to where line numbers appear if you have them turned on.如果您打开它们,它就在行号出现的位置旁边。

In this screenshot my mouse is hovering over the diamond.在此屏幕截图中,我的鼠标悬停在钻石上方。 Notice how it has changed to a little play arrow indicating it will be run.注意它是如何变成一个小播放箭头的,表示它将被运行。

悬停在测试钻石上

You can then re-execute the most recently run test(s) with ⌃⌥⌘ G .然后,您可以使用⌃⌥⌘ G重新执行最近运行的测试。

As far as I know this cannot be done via the xcodebuild .据我所知,这不能通过xcodebuild完成。

May be this helps.可能这有帮助。 I was ignorant because I didn't know that you can disable some particular tests in Test navigator by right-clicking them.我很无知,因为我不知道您可以通过右键单击测试导航器中的某些特定测试来禁用它们。

Then you run xcodebuild test -scheme <name> as usual, and all the disabled tests will be ignored.然后像往常一样运行xcodebuild test -scheme <name> ,所有禁用的测试都将被忽略。 Worked with Fastlane for me.为我与 Fastlane 合作。

Xcode 测试导航器

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

相关问题 将Xcode 5中的各个XCTest单元测试用例定位到通用应用程序的特定iOS设备? - Target individual XCTest unit test cases in Xcode 5 to a specific iOS device for a universal app? 如何在 azure 管道中运行 iOS 移动应用程序的 XCTest(UI 测试用例)? - How to run XCTest (UI test cases) of iOS mobile application in the azure pipeline? iOS:从命令行运行单个单元测试 - iOS: Run a single unit test from the command line 单击确定可以从XCTest中的其他测试用例中调用测试用例 - OK to call test cases from within other test cases in XCTest 运行测试用例时 CocoaLumberJack XCTest 链接器错误 - CocoaLumberJack XCTest Linker Error when running test cases iOS应用程序中的单元测试用例 - Unit test cases in iOS application 在XCTest中的UI测试中从TextView获取文本 - Getting text from TextView in UI test in XCTest 从命令行在项目中查找 XCTest Target - Find XCTest Target in a project from command line 如何在iOS XCTest单元测试中测试从右到左的语言? - How to test right-to-left language in iOS XCTest unit tests? 是否可以在IOS的XCTest单元测试用例中模拟方法(带有参数)调用 - Is it possible to mock a method(with parameters) call in XCTest unit test case in IOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM