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

こ雲淡風輕ζ 提交于 2019-11-28 05:19:54
emoleumassi

It is now possible with Xcode 8 using the -only-testing parameter with xcodebuild:

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

Check this video: https://developer.apple.com/videos/play/wwdc2016/409/

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).

I was in similar situation as you and have built a python script that triggers the set of test case/s that i want. 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.

Some relevant steps for what you want to achieve.

  1. Override testInvocations method present in XCTestCase to do below steps
    • In this method I read Environment Variable 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.
    • Return array of Invocations.
  2. How to pass Environment Variable?
    • In scheme add an environment variable named XXXX.
    • Scheme files are standard xml files, write a script that modifies the scheme file to contain the Comma separated values in environment variable.

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.

As far as I know this cannot be done via the xcodebuild.

-only-testing:<MyXCTargetForTest>/<MyTestSuite>/<MyTestMethod>
  • MyXCTargetForTest - target
  • MyTestSuite - class_name
  • MyTestMethod - test_name

For example if Test Navigator looks like

the parameter will have the following type

-only-testing:SampleAppTest/SampleAppTest/testExample

Also you can skip a test using: -skip-testing

Read more here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!