如何在 Xcode 中运行单个测试用例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3402353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:35:44  来源:igfitidea点击:

How to run single test case in Xcode?

unit-testingxcode

提问by Morgan Cheng

I know it is best practice to run all unit test cases after any change to make sure not breaking anything. However, some times, e.g. debugging, I really want to run only one single test case. It seems Xcode doesn't provide any such feature in UI, while other testing framework such as JUnit has such features.

我知道最好的做法是在任何更改后运行所有单元测试用例,以确保不会破坏任何内容。但是,有时,例如调试,我真的只想运行一个测试用例。Xcode 在 UI 中似乎没有提供任何此类功能,而其他测试框架(例如 JUnit)具有此类功能。

Is there any workaround to have only one testcase run in Xcode?

是否有任何解决方法可以在 Xcode 中只运行一个测试用例?

P.S. most of my test cases are logic tests. So, they are not run in iPhone device.

PS我的大部分测试用例都是逻辑测试。因此,它们不在 iPhone 设备中运行。

回答by adib

Xcode 4 now have this feature. Simply create a "run scheme"? that has the test cases that you want to run.

Xcode 4 现在有这个功能。只需创建一个“运行方案”?包含您要运行的测试用例。

  1. Open menu "Product|Edit Scheme..."
  2. Click on "Edit..."
  3. In the left pane, expand the "Test" section.
  4. In the right pane, expand the test bundle and uncheck the test cases you don't need to run.
  1. 打开菜单“产品|编辑方案...”
  2. 点击“编辑...”
  3. 在左窗格中,展开“测试”部分。
  4. 在右侧窗格中,展开测试包并取消选中您不需要运行的测试用例。

回答by Stuart

?+?+?+U

?+ ?+ ?+U

You can also use the keyboard short cut of Control-Option-Command-U

您也可以使用 Control-Option-Command-U 的键盘快捷键

Expert taken from Apple Documentation

来自Apple 文档的专家

Product > Perform Action > Test . This dynamic menu item senses the current test method in which the editing insertion point is positioned when you're editing a test method and allows you to run that test with a keyboard shortcut. The command's name adapts to show the test it will run, for instance, Product > Perform Action > Test testAddition. The keyboard shortcut is Control-Option-Command-U.

产品 > 执行操作 > 测试。当您编辑测试方法时,此动态菜单项会检测编辑插入点所在的当前测试方法,并允许您使用键盘快捷键运行该测试。命令的名称适用于显示它将运行的测试,例如,产品 > 执行操作 > 测试 testAddition。键盘快捷键是 Control-Option-Command-U。

回答by Erik B

I'm sure that no one missed the release of Xcode 5 and its ability to easily run a single test case, but adding this answer just for completeness.

我敢肯定没有人会错过 Xcode 5 的发布及其轻松运行单个测试用例的能力,但添加此答案只是为了完整性。

In Xcode 5 you can run a single test case by clicking the little play button that you can find next to the test in the test navigator or next to the test method in the editor. In both places it shows up when you hover over it.

在 Xcode 5 中,您可以通过单击测试导航器中测试旁边或编辑器中测试方法旁边的小播放按钮来运行单个测试用例。当您将鼠标悬停在它上面时,它会在两个地方都显示出来。

回答by Will

You can also use xctoolfrom the commandline with the --onlyargument, which will only run the specified testcase(s).

您还可以从命令行xctool使用--only参数,它只会运行指定的测试用例。

回答by Philippe

As you have noted, the OCUnit test framework marks methods whose name start with 'test' as test cases. This is done at runtime

正如您所指出的,OCUnit 测试框架将名称以“test”开头的方法标记为测试用例。这是在运行时完成的

In practice, your test cases should run so fast that it should not matter how many are enabled; your debugger should be able to stop inside your test case very quickly after you press "Debug".

在实践中,您的测试用例应该运行得如此之快,以至于启用了多少都无关紧要;在您按下“调试”后,您的调试器应该能够很快地停止在您的测试用例中。

That being said, the quickest way to disable some tests is probably to use an #if 0 / #endif block. The feature to disable test cases dynamically does not exist in Xcode / OCUnit, since there is no GUI component.

话虽如此,禁用某些测试的最快方法可能是使用 #if 0 / #endif 块。Xcode / OCUnit 中不存在动态禁用测试用例的功能,因为没有 GUI 组件。

In theory it should be doable, because at runtime (and before all tests are run) there are ways to access the test list in OCUnit, but this requires modifications to the OCUnit source code, which is not desirable (it will be wiped out in the next Xcode update, for one).

理论上应该是可行的,因为在运行时(并且在所有测试运行之前)有办法访问 OCUnit 中的测试列表,但这需要修改 OCUnit 源代码,这是不可取的(它会在下一次 Xcode 更新,例如)。

Finally, if that feature is important to you, you can easily write your own test harness that mostly replicates what OCUnit does. Then you can tweak it to your heart's content, add UI, etc.

最后,如果该功能对您很重要,您可以轻松编写自己的测试工具,主要复制 OCUnit 的功能。然后你可以根据自己的喜好调整它,添加 UI 等。

It is not difficult, and a little educational. Here's a good link to get you started:

这并不难,有点教育意义。这是一个很好的链接,可以帮助您入门:

http://gusmueller.com/blog/archives/2009/10/how_to_write_your_own_automated_testing_framework.html

http://gusmueller.com/blog/archives/2009/10/how_to_write_your_own_automated_testing_framework.html