本文介绍了Xcode:TEST与DEBUG预处理器宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用单元测试创​​建新项目时,Xcode将测试方案的构建配置设置为Debug(运行方案相同)。

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的新的Build Configuration,添加一个预处理器宏TEST = 1,并将其用作为测试方案构建配置?或者,我应该保持Run&测试两个作为调试?

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

但是,我真的只打算做现在的测试。所以,这就是为什么我认为我应该区分调试和测试,但是想知道为什么Xcode默认情况下不为你做这个?苹果认为你不应该区分他们吗?

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:


  1. 创建了一个 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
    


  • 这允许我使用 BUNDLE 在哪里我的意思是 [NSBundle mainBundle] ,并且在运行测试时也可以工作。

    This allows me to use BUNDLE where ever I mean [NSBundle mainBundle] and also have it work when I run Tests.

    导入SenTestingKit在 Tests-Prefix.pch 也加快了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:TEST与DEBUG预处理器宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-12 06:00