使用 Xcode5、iOS7 模拟器和 XCTest 生成 gcda 文件 [英] Generate gcda-files with Xcode5, iOS7 simulator and XCTest

查看:16
本文介绍了使用 Xcode5、iOS7 模拟器和 XCTest 生成 gcda 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

受到这个问题的解决方案的启发 我尝试对 XCTest 使用相同的方法.

我已经设置了生成测试覆盖率文件=YES"和仪器程序流程=YES".

XCode 仍然不生成任何 gcda 文件.有人对如何解决这个问题有任何想法吗?

代码:

#import @interface VATestObserver : XCTestLog@结尾静态 ID mainSuite = nil;@implementation VATestObserver+(无效)初始化{[[NSUserDefaults standardUserDefaults] setValue:@"VATestObserver"forKey:XCTestObserverClassKey];[超级初始化];}- (void)testSuiteDidStart:(XCTestRun *)testRun {[超级 testSuiteDidStart:testRun];XCTestSuiteRun *suite = [[XCTestSuiteRun alloc] init];[套件 addTestRun:testRun];如果(mainSuite == nil){mainSuite = 套件;}}- (void)testSuiteDidStop:(XCTestRun *)testRun {[超级 testSuiteDidStop:testRun];XCTestSuiteRun *suite = [[XCTestSuiteRun alloc] init];[套件 addTestRun:testRun];如果(mainSuite == 套件){UIApplication* application = [UIApplication sharedApplication];[application.delegate applicationWillTerminate:application];}}@结尾

在 AppDelegate.m 中,我有:

extern void __gcov_flush(void);- (void)applicationWillTerminate:(UIApplication *)application {__gcov_flush();}

编辑:我编辑了问题以反映当前状态(没有红鲱鱼).

编辑为了使它工作,我必须将所有被测文件添加到测试目标,包括VATestObserver.

AppDelegate.m

#ifdef DEBUG+(无效)初始化{if([self class] == [AppDelegate class]) {[[NSUserDefaults standardUserDefaults] setValue:@"VATestObserver"forKey:@"XCTestObserverClass"];}}#万一

VATestObserver.m

#import #import #import //在设置测试覆盖率标志时未正确调用 __gcov_flush 的 XCode 5 错误的解决方法@interface VATestObserver : XCTestLog@结尾#ifdef 调试外部无效 __gcov_flush(void);#万一静态 NSUInteger sTestCounter = 0;静态 ID mainSuite = nil;@implementation VATestObserver+(无效)初始化{[[NSUserDefaults standardUserDefaults] setValue:@"VATestObserver"forKey:XCTestObserverClassKey];[超级初始化];}- (void)testSuiteDidStart:(XCTestRun *)testRun {[超级 testSuiteDidStart:testRun];XCTestSuiteRun *suite = [[XCTestSuiteRun alloc] init];[套件 addTestRun:testRun];sTestCounter++;如果(mainSuite == nil){mainSuite = 套件;}}- (void)testSuiteDidStop:(XCTestRun *)testRun {sTestCounter--;[超级 testSuiteDidStop:testRun];XCTestSuiteRun *suite = [[XCTestSuiteRun alloc] init];[套件 addTestRun:testRun];如果(sTestCounter == 0){__gcov_flush();}}

解决方案

更新 1:

在阅读更多关于此的内容后,我现在清楚了两件事(添加了重点):

<块引用>

测试和被测试的应用程序是分开编译的.测试实际上是注入到正在运行的应用程序中,因此 __gcov_flush() 必须在应用程序内部而不是在测试内部调用.

Xcode5 代码覆盖率(来自 CI 构建的 cmd-line) - 堆栈溢出

和,

<块引用>

再次强调:注入是复杂的.您的收获应该是:不要将 .m 文件从您的应用添加到您的测试目标.您会遇到意外行为.

测试视图控制器 – #1 – 更轻的视图控制器

下面的代码已更改以反映这两个见解......

<小时>

更新 2:

根据 @MdaG 在评论中的要求,添加了有关如何使其适用于静态库的信息.图书馆的主要变化是:

  • 我们可以直接从 -stopObserving 方法刷新,因为没有单独的应用程序可以在其中注入测试.

  • 我们必须在 +load 方法中注册观察者,因为当 +initialize 被调用时(当这个类第一次从测试套件中访问时) XCTest 拿它已经太晚了.

<小时>

解决方案

这里的其他答案极大地帮助了我在我的项目中设置代码覆盖率.在探索它们的过程中,我相信我已经设法大大简化了修复代码.

考虑以下任一情况:

  • ExampleApp.xcodeproj 作为空应用程序"从头开始创建
  • ExampleLibrary.xcodeproj 作为独立的Cocoa Touch 静态库"创建

这些是我在 Xcode 5 中启用代码覆盖率生成的步骤:

  1. 使用以下代码在 ExampleAppTests 组内创建 GcovTestObserver.m 文件:

    #import @interface GcovTestObserver : XCTestObserver@结尾@implementation GcovTestObserver- (void)stopObserving{【超级停止观察】;UIApplication* application = [UIApplication sharedApplication];[application.delegate applicationWillTerminate:application];}@结尾

    做库的时候,由于没有app可以调用,flush可以直接从observer调用.在这种情况下,请使用以下代码将该文件添加到 ExampleLibraryTests 组:

    #import @interface GcovTestObserver : XCTestObserver@结尾@implementation GcovTestObserver- (void)stopObserving{【超级停止观察】;外部无效 __gcov_flush(void);__gcov_flush();}@结尾

  2. 要注册测试观察者类,请将以下代码添加到以下任一者的@implementation 部分:

    • ExampleAppDelegate.m 文件,位于 ExampleApp 组内
    • ExampleLibrary.m 文件,位于 ExampleLibrary 组内

     

    #ifdef DEBUG+(无效)负载{[[NSUserDefaults standardUserDefaults] setValue:@"XCTestLog,GcovTestObserver"forKey:@"XCTestObserverClass"];}#万一

    以前,这个答案建议使用 +initialize 方法(并且您仍然可以在应用程序的情况下这样做)但它不适用于图书馆......

    在库的情况下,+initialize 可能只有在测试第一次调用库代码时才会执行,到那时注册观察者已经太晚了.使用+load方法,无论哪种场景,观察者注册总是及时完成.

  3. 对于应用程序,将以下代码添加到 ExampleAppDelegate.m 文件的 @implementation 部分,在 ExampleAppstrong> 组,在退出应用程序时刷新覆盖文件:

    - (void)applicationWillTerminate:(UIApplication *)application{#ifdef 调试外部无效 __gcov_flush(void);__gcov_flush();#万一}

  4. 启用 Generate Test Coverage FilesInstrument Program Flow 通过在项目构建设置中将它们设置为 YES(对于示例"和示例测试"目标).

    为了以简单且一致的方式执行此操作,我添加了一个 Debug.xcconfig 文件 与项目的调试"配置相关,具有以下声明:

    GCC_GENERATE_TEST_COVERAGE_FILES = YESGCC_INSTRUMENT_PROGRAM_FLOW_ARCS = 是

  5. 确保项目的所有 .m 文件也包含在示例测试"目标的编译源"构建阶段. 不要这样做:应用代码属于应用目标,测试代码属于测试目标!

在为您的项目运行测试后,您将能够在此处找到为 Example.xcodeproj 生成的覆盖文件:

cd ~/Library/Developer/Xcode/DerivedData/找到 ./Example-* -name *.gcda

注意事项

步骤 1

XCTestObserver.h 中的方法声明表明:

/*!在运行测试后立即发送以通知观察者是时候了停止观察测试进度.子类可以覆盖这个方法,但是他们必须调用 super 的实现.*/-(无效)停止观察;

步骤 2

2.a)

通过创建和注册一个单独的 XCTestObserver 子类,我们可以避免直接干扰默认的 XCTestLog 类.

XCTestObserver.h 中的常量键声明表明:

/*!将 XCTestObserverClass 用户默认设置为XCTestObserver 指示 XCTest 应该使用该子类进行报告测试结果而不是默认值 XCTestLog.您可以指定多个XCTestObserver 的子类通过在每个子类之间指定一个逗号,对于例如@"XCTestLog,FooObserver".*/XCT_EXPORT NSString * const XCTestObserverClassKey;

2.b)

尽管通常的做法是在 +initialize 内的代码周围使用 if(self == [ExampleAppDelegate class]) [注意:现在使用 +load],我发现在这种特殊情况下更容易忽略它:在执行复制和操作时无需调整到正确的类名.粘贴.

此外,这里并不是真的需要防止两次运行代码:这不包含在发布版本中,即使我们子类化 ExampleAppDelegate 运行此代码也没有问题不止一个.

2.c)

在库的情况下,问题的第一个提示来自 适用于 Mac 的 Google 工具箱 项目:GTMCodeCovereageApp.m

+ (void)load {//使用定义和字符串,这样我们就不必在这里链接 XCTest.//必须在这里设置默认值.如果我们在 XCTest 中设置它们,我们就太迟了//用于观察者注册.//(...)

并且作为 NSObject 类引用表示:

<块引用>

initialize — 在类收到第一条消息之前初始化该类

<块引用>

load — 每当将类或类别添加到 Objective-C 运行时时调用

EmptyLibrary"项目

如果有人试图通过创建自己的EmptyLibrary"项目来复制此过程,请记住,您需要以某种方式从默认的 emtpy 测试中调用库代码.

如果没有从测试中调用主库类,编译器将尝试 smart 并且不会将它添加到运行时(因为它没有在任何地方被调用),所以+load 方法不会被调用.

您可以简单地调用一些无害的方法(正如 Apple 在其 Cocoa # 类初始化的编码指南).例如:

- (void)testExample{[ExampleLibrary self];}

Being inspired by the solution to this question I tried using the same approach with XCTest.

I've set 'Generate Test Coverage Files=YES' and 'Instrument Program Flow=YES'.

XCode still doesn't produce any gcda files. Anyone have any ideas of how to solve this?

Code:

#import <XCTest/XCTestLog.h>

@interface VATestObserver : XCTestLog

@end

static id mainSuite = nil;

@implementation VATestObserver

+ (void)initialize {
    [[NSUserDefaults standardUserDefaults] setValue:@"VATestObserver"
                                             forKey:XCTestObserverClassKey];
    [super initialize];
}

- (void)testSuiteDidStart:(XCTestRun *)testRun {
    [super testSuiteDidStart:testRun];

    XCTestSuiteRun *suite = [[XCTestSuiteRun alloc] init];
    [suite addTestRun:testRun];

    if (mainSuite == nil) {
        mainSuite = suite;
    }
}

- (void)testSuiteDidStop:(XCTestRun *)testRun {
    [super testSuiteDidStop:testRun];

    XCTestSuiteRun *suite = [[XCTestSuiteRun alloc] init];
    [suite addTestRun:testRun];

    if (mainSuite == suite) {
        UIApplication* application = [UIApplication sharedApplication];
        [application.delegate applicationWillTerminate:application];
    }
}

@end

In AppDelegate.m I have:

extern void __gcov_flush(void);
- (void)applicationWillTerminate:(UIApplication *)application {
    __gcov_flush();
}

EDIT: I edited the question to reflect the current status (without the red herrings).

EDIT To make it work I had to add the all the files under test to the test target including VATestObserver.

AppDelegate.m

#ifdef DEBUG
+ (void)initialize {
    if([self class] == [AppDelegate class]) {
        [[NSUserDefaults standardUserDefaults] setValue:@"VATestObserver"
                                                 forKey:@"XCTestObserverClass"];
    }
}
#endif

VATestObserver.m

#import <XCTest/XCTestLog.h>
#import <XCTest/XCTestSuiteRun.h>
#import <XCTest/XCTest.h>

// Workaround for XCode 5 bug where __gcov_flush is not called properly when Test Coverage flags are set

@interface VATestObserver : XCTestLog
@end

#ifdef DEBUG
extern void __gcov_flush(void);
#endif

static NSUInteger sTestCounter = 0;
static id mainSuite = nil;

@implementation VATestObserver

+ (void)initialize {
    [[NSUserDefaults standardUserDefaults] setValue:@"VATestObserver"
                                             forKey:XCTestObserverClassKey];
    [super initialize];
}

- (void)testSuiteDidStart:(XCTestRun *)testRun {
    [super testSuiteDidStart:testRun];

    XCTestSuiteRun *suite = [[XCTestSuiteRun alloc] init];
    [suite addTestRun:testRun];

    sTestCounter++;

    if (mainSuite == nil) {
        mainSuite = suite;
    }
}

- (void)testSuiteDidStop:(XCTestRun *)testRun {

    sTestCounter--;

    [super testSuiteDidStop:testRun];

    XCTestSuiteRun *suite = [[XCTestSuiteRun alloc] init];
    [suite addTestRun:testRun];

    if (sTestCounter == 0) {
        __gcov_flush();
    }
}

解决方案

Update 1:

After reading a bit more about this, 2 things have now become clear to me (emphasis added):

Tests and the tested application are compiled separately. Tests are actually injected into the running application, so the __gcov_flush() must be called inside the application not inside the tests.

Xcode5 Code Coverage (from cmd-line for CI builds) - Stack Overflow

and,

Again: Injection is complex. Your take away should be: Don’t add .m files from your app to your test target. You’ll get unexpected behavior.

Testing View Controllers – #1 – Lighter View Controllers

The code below was changed to reflect these two insights…


Update 2:

Added information on how to make this work for static libraries, as requested by @MdaG in the comments. The main changes for libraries is that:

  • We can flush directly from the -stopObserving method because there isn't a separate app where to inject the tests.

  • We must register the observer in the +load method because by the time the +initialize is called (when the class is first accessed from the test suite) it's already too late for XCTest to pick it up.


Solution

The other answers here have helped me immensely in setting up code coverage in my project. While exploring them, I believe I've managed to simplify the code for the fix quite a bit.

Considering either one of:

  • ExampleApp.xcodeproj created from scratch as an "Empty Application"
  • ExampleLibrary.xcodeproj created as an independent "Cocoa Touch Static Library"

These were the steps I took to enable Code Coverage generation in Xcode 5:

  1. Create the GcovTestObserver.m file with the following code, inside the ExampleAppTests group:

    #import <XCTest/XCTestObserver.h>
    
    @interface GcovTestObserver : XCTestObserver
    @end
    
    @implementation GcovTestObserver
    
    - (void)stopObserving
    {
        [super stopObserving];
        UIApplication* application = [UIApplication sharedApplication];
        [application.delegate applicationWillTerminate:application];
    }
    
    @end
    

    When doing a library, since there is no app to call, the flush can be invoked directly from the observer. In that case, add the file to the ExampleLibraryTests group with this code instead:

    #import <XCTest/XCTestObserver.h>
    
    @interface GcovTestObserver : XCTestObserver
    @end
    
    @implementation GcovTestObserver
    
    - (void)stopObserving
    {
        [super stopObserving];
        extern void __gcov_flush(void);
        __gcov_flush();
    }
    
    @end
    

  2. To register the test observer class, add the following code to the @implementation section of either one of:

    • ExampleAppDelegate.m file, inside the ExampleApp group
    • ExampleLibrary.m file, inside the ExampleLibrary group

     

    #ifdef DEBUG
    + (void)load {
        [[NSUserDefaults standardUserDefaults] setValue:@"XCTestLog,GcovTestObserver"
                                                 forKey:@"XCTestObserverClass"];
    }
    #endif
    

    Previously, this answer suggested to use the +initialize method (and you can still do that in case of Apps) but it doesn't work for libraries…

    In the case of a library, the +initialize will probably be executed only when the tests invoke the library code for the first time, and by then it's already too late to register the observer. Using the +load method, the observer registration in always done in time, regardless of which scenario.

  3. In the case of Apps, add the following code to the @implementation section of the ExampleAppDelegate.m file, inside the ExampleApp group, to flush the coverage files on exiting the app:

    - (void)applicationWillTerminate:(UIApplication *)application
    {
    #ifdef DEBUG
        extern void __gcov_flush(void);
        __gcov_flush();
    #endif
    }
    

  4. Enable Generate Test Coverage Files and Instrument Program Flow by setting them to YES in the project build settings (for both the "Example" and "Example Tests" targets).

    To do this in an easy and consistent way, I've added a Debug.xcconfig file associated with the project's "Debug" configuration, with the following declarations:

    GCC_GENERATE_TEST_COVERAGE_FILES = YES
    GCC_INSTRUMENT_PROGRAM_FLOW_ARCS = YES
    

  5. Make sure all the project's .m files are also included in the "Compile Sources" build phase of the "Example Tests" target. Don't do this: app code belongs to the app target, test code belongs to the test target!

After running the tests for your project, you'l be able to find the generated coverage files for the Example.xcodeproj in here:

cd ~/Library/Developer/Xcode/DerivedData/
find ./Example-* -name *.gcda

Notes

Step 1

The method declaration inside XCTestObserver.h indicates:

/*! Sent immediately after running tests to inform the observer that it's time 
    to stop observing test progress. Subclasses can override this method, but 
    they must invoke super's implementation. */
- (void) stopObserving;

Step 2

2.a)

By creating and registering a separate XCTestObserver subclass, we avoid having to interfere directly with the default XCTestLog class.

The constant key declaration inside XCTestObserver.h suggests just that:

/*! Setting the XCTestObserverClass user default to the name of a subclass of 
    XCTestObserver indicates that XCTest should use that subclass for reporting 
    test results rather than the default, XCTestLog. You can specify multiple 
    subclasses of XCTestObserver by specifying a comma between each one, for 
    example @"XCTestLog,FooObserver". */
XCT_EXPORT NSString * const XCTestObserverClassKey;

2.b)

Even though it's common practice to use if(self == [ExampleAppDelegate class]) around the code inside +initialize [Note: it's now using +load], I find it easier to omit it in this particular case: no need to adjust to the correct class name when doing copy & paste.

Also, the protection against running the code twice isn't really necessary here: this is not included in the release builds, and even if we subclass ExampleAppDelegate there is no problem in running this code more than one.

2.c)

In the case of libraries, the first hint of the problem came from this code comment in the Google Toolbox for Mac project: GTMCodeCovereageApp.m

+ (void)load {
  // Using defines and strings so that we don't have to link in XCTest here.
  // Must set defaults here. If we set them in XCTest we are too late
  // for the observer registration.
  // (...)

And as the NSObject Class Reference indicates:

initialize — Initializes the class before it receives its first message

load — Invoked whenever a class or category is added to the Objective-C runtime

The "EmptyLibrary" project

In case someone tries to replicate this process by creating their own "EmptyLibrary" project, please bear in mind that you need to invoke the library code from the default emtpy tests somehow.

If the main library class is not invoked from the tests, the compiler will try to be smart and it won't add it to the runtime (since it's not being called anywhere), so the +load method doesn't get called.

You can simply invoke some harmless method (as Apple suggests in their Coding Guidelines for Cocoa # Class Initialization). For example:

- (void)testExample
{
    [ExampleLibrary self];
}

这篇关于使用 Xcode5、iOS7 模拟器和 XCTest 生成 gcda 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆