Xcode:测试与调试预处理器宏 [英] Xcode: TEST vs DEBUG preprocessor macros
问题描述
使用单元测试创建新项目时,Xcode 将构建配置设置为 Test 方案的 Debug(与 Run 方案相同).
When creating a new project with unit tests, Xcode sets the build configuration to Debug for the Test scheme (same for the Run scheme).
我应该区分 Run (Command-R) 和测试(Command-U)方案?
Should I differentiate between Run (Command-R) & Test (Command-U) schemes?
即,我是否应该创建一个名为 Test 的新构建配置,向其中添加一个预处理器宏 TEST=1,并将其用作测试方案的构建配置?或者,我应该保持 Run &将两者都作为 Debug 进行测试?
I.e., should I create a new Build Configuration called Test, add a preprocessor macro TEST=1 to it, and use it as the build configuration for the Test scheme instead? Or, should I just keep Run & Test both as Debug?
我有 Ruby/Rails 背景,您通常拥有测试、开发和生产环境.在我看来,Debug 就像开发,Release 就像生产,但我们缺少测试,这就是为什么我认为添加 Test 可能有意义.
I come from a Ruby/Rails background, where you usually have test, development, and production environments. It seems to me that Debug is like development and Release is like production, but we're missing a test, which is why I'm thinking it might make sense to add Test.
评论?意见?建议?
我特意问这个是因为我想编译一些用于测试的东西:
I'm specifically asking this because I want to compile something for Test with:
#ifdef TEST
// Do something when I test.
#endif
如果我也为 Debug 编译它,我认为这并不重要.所以,我真的可以这样做:
I don't think it matters if I also compile this for Debug. So, I really could just do:
#ifdef DEBUG
// Do something when I run or test.
#endif
但是,我现在真的只是打算做测试.所以,这就是为什么我认为我应该区分 debug 和测试但我想知道为什么 Xcode 默认不为你做这个?Apple 认为你不应该区分它们吗?
But, I'm really only intending to do it for tests for now. So, that's why I'm thinking I should differentiate between debug & test but am wondering why Xcode doesn't do that for you by default? Does Apple think you shouldn't differentiate between them?
推荐答案
我没有创建测试构建配置,而是:
Instead of creating a Test build configuration, I:
创建了一个
Tests-Prefix.pch
文件:
#define TEST 1
#import <SenTestingKit/SenTestingKit.h>
#import "CocoaPlant-Prefix.pch"
在测试目标的构建设置的前缀标题字段中输入其路径.
entered its path in the Prefix Header field of the Tests target's build settings.
将以下代码添加到我创建的名为 MyAppDefines.h
的文件的顶部,该文件是在 MyApp-Prefix.pch
中导入的:
added the following code to the top of a file I created called MyAppDefines.h
, imported in MyApp-Prefix.pch
:
#ifdef TEST
#define TEST_CLASS NSClassFromString(@"AppDelegateTests") // any test class
#define BUNDLE [NSBundle bundleForClass:TEST_CLASS]
#define APP_NAME @"Tests"
#else
#define BUNDLE [NSBundle mainBundle]
#define APP_NAME [[BUNDLE infoDictionary] objectForKey:(NSString *)kCFBundleNameKey]
#endif
这允许我在任何我指的是 [NSBundle mainBundle]
的地方使用 BUNDLE
并且在我运行测试时也可以使用它.
This allows me to use BUNDLE
where ever I mean [NSBundle mainBundle]
and also have it work when I run Tests.
在 Tests-Prefix.pch
中导入 SenTestingKit 也加快了 SenTestingKit 框架的编译,并允许我省略 #import <SenTestingKit/SenTestingKit.h>
从所有测试文件的顶部开始.
Importing SenTestingKit in Tests-Prefix.pch
also speeds up the compiling of the SenTestingKit Framework and allows me to leave out #import <SenTestingKit/SenTestingKit.h>
from the top of all the tests files.
这篇关于Xcode:测试与调试预处理器宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!