3

我是 Xcode(和一般的 Mac)的新手,我正在尝试将我的一些代码库移植到 OS X 和 iOS 上运行。我有大量针对 Google C++ 测试框架(Google 测试)编写的单元测试。我成功编译了框架并且可以运行一些测试,但我不确定如何从 Xcode 中查看彩色输出。

我习惯于在 Visual Studio 中点击“运行”并立即看到一个控制台窗口(带有颜色),让我一眼就知道测试是通过还是失败。

我设法设置了一个“运行脚本”“构建阶段”,但这似乎只输出到日志导航器,它消除了颜色,甚至是固定宽度的输出,如果测试通过,很难一目了然. 运行测试后,我也找不到显示日志的方法。当我这样做时,“所有输出”窗口中不会出现任何内容。

我玩过 XcodeColors,但这似乎不适用于使用 ANSI 颜色代码的脚本。

在这一点上,如果这根本无法在 Xcode 中完成,我不会感到惊讶。那将是理想的,但如果不是,是否可以创建一个“运行脚本”来在独立的终端窗口中运行测试?颜色在那里工作得很好。

谢谢你的帮助!

4

2 回答 2

3

Here are links to a tool that colorizes the text in the Log window. It's free and the source is in github so you can figure out how it works. The first link says that it just uses simple ANSI codes to do the job.

http://deepitpro.com/en/articles/XcodeColors/info
https://github.com/robbiehanson/XcodeColors#readme

To kick off the execution from within Xcode, you will probably need to add a new target to your project. To add a Target, click on your project and then there is an Add Target button on the bottom of the screen. I don't know exactly what you're executing but here are my best guesses based on your question:

  • MacOSX/Application/Cocoa-AppleScript or Command Line Tool - Create a simple script or program that will execute your units tests.
  • MacOSX/Other/External Build System - Allows for execution of an external "make" program with args.

Once you have a way to execute your unit tests, you just need to figure out how to route the output from the unit tests to the Log window. If you can edit the Google Test project and make it use NSLog(), that would seem to be the easiest solution. You could create your own logging method, perform the ANSI colorization, and then send the final text to NSLog().

ADDED: OK. Interesting findings... Read all before starting. Here's what to do:

Start AppleScript Editor. This is in LaunchPad. Paste the following script into it:

tell application "Terminal"
    activate
    do script "<your commands>" in window 1
end tell

You can repeat the "do script" line as needed. Use this to execute your unit tests. In Script Editor, do Save As.../File Format=Script and save it to a safe location for now like your Documents directory. This will create a file like "UnitTests.scpt".

Now go to your iOS project. Select the project at the top-left. Select the Build Phases tab top-middle. Click the Add Build Phase button on the bottom right. Here's the interesting part.

Leave Shell as is ("/bin/sh"). Add one line:

osascript ~/Documents/UnitTests.scpt

That will execute the script after every build.

But here's the interesting part I found. Click on Build Settings (top-middle). Make sure All is selected (not Basic). Scroll down the list to find Unit Testing. Open Test Host. Hit the + next to Debug. You can also put the above osascript command here. You might be able to execute your unit tests here and if you can, the output will likely show up in the Log! Let me know what happens.

于 2012-08-22T01:29:28.050 回答
0

我熟悉 Java:JUnit + JCodecoverage,在移动应用程序:Android 和 iPhone 我懒得用 TDD 开发,但如果我想开始的话:

我将创建一个 Hello Word 应用程序,并打开 JUnitTesting 选项:检查包括单元测试

在此处输入图像描述

这将创建一个测试应用程序/目标,您将能够运行它。在 Android 上也是如此:你必须创建一个“测试项目”一旦我做了并且忘记了它是如何工作的,但是,还有其他的东西: - 长按 Xcode 上的播放按钮( 4.4 ),你将拥有一个下拉菜单:运行、测试、配置文件、分析。

我无法呈现这些,因为如果我按 Shift+ Cmd + 4 截屏,它会发生变化,但这里看起来像是更改后的菜单: 在此处输入图像描述

恕我直言:对于银行、外汇、其他金融或军事(高安全性软件),我会使用测试驱动开发,代码覆盖率超过 99%,但那些简单的 3-4 网络服务调用移动应用程序,它们显示浏览器中可用的公共数据只是浪费时间开发测试和维护它!

很多时候我需要在没有互联网连接的情况下进行测试。更糟糕的情况是 WI-FI 连接,但是路由器不给 IP 也不放开手机,但是如果我问手机状态:它已连接...

GUI 流很难通过单元测试进行测试,其中 / 对我有用:从 Web 服务获取的数据以及与内部缓存的同步。正如我所见,通过手动测试来做这件事仍然更便宜。

于 2012-08-21T23:05:07.993 回答