XCode 5单元测试:启动我的应用程序

时间:2023-01-22 21:04:01

When I run my tests in XCode 5, the main window of my OS X app appears on the screen for a couple of seconds while running the tests. Why? Even if I uncomment all my tests it still opens my main window.

当我在XCode 5中运行测试时,我的OS X应用程序的主窗口在运行测试时会在屏幕上显示几秒钟。为什么?即使我取消注释我的所有测试,它仍会打开我的主窗口。

7 个解决方案

#1


37  

You are running application test, not logic test. This means an instance of your app will be started and then run the unit tests. This allow you to perform some integration test that require your app is running.

您正在运行应用程序测试,而不是逻辑测试。这意味着您的应用程序实例将启动,然后运行单元测试。这允许您执行一些需要您的应用程序运行的集成测试。

Here is the guide to setup application test and logic test.

以下是设置应用程序测试和逻辑测试的指南。

If you want to change it to logic test (so it run faster and don't need to start your app first):

如果你想将它改为逻辑测试(所以它运行得更快,不需要先启动你的应用程序):

  1. go to build settings for your unit test target
  2. 去构建单元测试目标的设置
  3. search Bundle
  4. 搜索捆绑
  5. remove Bundle Loader and Test Host
  6. 删除Bundle Loader和Test Host

XCode 5单元测试:启动我的应用程序

#2


10  

Thats right, you have to delete the "Bundle Loader" and "Test Host" from your build settings.

多数民众赞成在右边,您必须从构建设置中删除“Bundle Loader”和“Test Host”。

But you have to add the necessary implementation files to your unit test target. The necessary files are what you want to use in your unit test cases. You need to do this because in logic tests XCode wont compile the whole application. So some of your files will be missing.

但是您必须将必要的实现文件添加到单元测试目标中。必要的文件是您要在单元测试用例中使用的文件。你需要这样做,因为在逻辑测试中,XCode不会编译整个应用程序。所以你的一些文件将丢失。

This is en error message if you have left out a file:

如果您遗漏了文件,则会显示错误消息:

Undefined symbols for architecture i386:
  "_OBJC_CLASS_$_Module", referenced from:
      objc-class-ref in Lobic Network.o
      objc-class-ref in Logic_Unit.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

You can add the missing files by selecting the implementation file and bringing up the file inspector. There will be a section named "Target Membership" and there you can set the files target membership to your unit test also.

您可以通过选择实施文件并调出文件检查器来添加缺少的文件。将有一个名为“目标成员资格”的部分,您也可以将文件目标成员资格设置为单元测试。

#3


9  

With XCTest, application files DO NOT need to be included within XCTest targets. The XCTest bundle is linked against the application which makes those files available during runtime.

使用XCTest,应用程序文件不需要包含在XCTest目标中。 XCTest包与应用程序链接,后者在运行时使这些文件可用。

To make this work, ensure the compiler option "Symbols hidden by default" is set to NO Within the Application target.

要使其工作,请确保编译器选项“默认情况下隐藏的符号”在应用程序目标中设置为NO。

Here is a blog post with screenshots for clarity: http://zmcartor.github.io/code/2014/02/24/slim-xctest-targets

这是一篇博客文章,屏幕截图清晰:http://zmcartor.github.io/code/2014/02/24/slim-xctest-targets

The advantage of this approach is test target builds much much faster.

这种方法的优点是测试目标的构建速度要快得多。

#4


4  

In XCode 7, removing Host Application does not work for me. Indeed I use the following to avoid app runs.

在XCode 7中,删除主机应用程序对我不起作用。确实,我使用以下内容来避免应用程序运行。

  1. Setup Test Scheme Arguments XCode 5单元测试:启动我的应用程序

    设置测试方案参数

  2. in main.m

    在main.m

    static bool isRunningTests()
    {
        NSDictionary* environment = [[NSProcessInfo processInfo] environment];
        NSString* testEnabled = environment[@"TEST_ENABLED"];
        return [testEnabled isEqualToString:@"YES"];
    }
    
  3. modify main()

    修改main()

    int main(int argc, char * argv[]) {
        @autoreleasepool {
            if (isRunningTests()) {
                return UIApplicationMain(argc, argv, nil, nil);
            } else {
                return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
            }
        }
    }
    

#5


2  

If the tests are for code that can run on desktop and mobile, you can run them without a simulator or hosting them within your app.

如果测试适用于可在桌面和移动设备上运行的代码,则可以在没有模拟器的情况下运行它们,也可以在应用程序中托管它们。

The trouble is that you cannot use the scheme chooser for your normal target (desktop or iOS) to run the test.

问题是您不能将方案选择器用于正常目标(桌面或iOS)来运行测试。

The following worked for me in Xcode6.

以下在Xcode6中为我工作。

File > New Target...

Select Cocoa Testing Bundle from the OS X category.

从OS X类别中选择Cocoa Testing Bundle。

XCode 5单元测试:启动我的应用程序

Take care to select None from the target drop-down.

注意从目标下拉列表中选择“无”。

XCode 5单元测试:启动我的应用程序

Click Finish. Add the relevant files to the new target as described above.

单击完成。如上所述,将相关文件添加到新目标。

Now create a scheme to run the test.

现在创建一个运行测试的方案。

Click the schemes chooser top-right and choose New Scheme..., click the drop-down and navigate down the list to the new target. Now you can choose the scheme from the schemes chooser, and use ⌘U to run the tests.

单击右上角的方案选择器并选择New Scheme ...,单击下拉列表并向下导航到新目标。现在,您可以从方案选择器中选择方案,并使用⌘U运行测试。

XCode 5单元测试:启动我的应用程序

#6


1  

I just wasted a morning on this.

我只是浪费了一个早上。

Project was created in XCode 4 and used SenTesting.

项目是在XCode 4中创建的,并使用了SenTesting。

Tried migrating tests on XCode 5/XCTTest

尝试在XCode 5 / XCTTest上进行迁移测试

Had same issue - app ran in simulator and test never started after trying everything (change from app to logic tests, change to XCTest, remove SenTesting)

有同样的问题 - 应用程序在模拟器中运行,测试从未尝试过所有内容(从应用程序更改为逻辑测试,更改为XCTest,删除SenTesting)

gave up created a clean XCode 5 project.

放弃了创建一个干净的XCode 5项目。

Added all my files in and tests ran ok.

添加了我的所有文件,测试运行正常。

May still have issues with Storyboard as these were built with XCode 4.

可能仍然存在Storyboard的问题,因为这些是使用XCode 4构建的。

Drastic but it works so keep it as last resort.

很有吸引力,但它的工作原理,所以保持最后的手段。

#7


1  

On XCode5, the app does start. This answer shows how to change its delegate when running unit tests so that it exits right away: https://*.com/a/20588035/239408

在XCode5上,应用程序确实启动了。这个答案显示了如何在运行单元测试时更改其委托,以便它立即退出:https://*.com/a/20588035/239408

#1


37  

You are running application test, not logic test. This means an instance of your app will be started and then run the unit tests. This allow you to perform some integration test that require your app is running.

您正在运行应用程序测试,而不是逻辑测试。这意味着您的应用程序实例将启动,然后运行单元测试。这允许您执行一些需要您的应用程序运行的集成测试。

Here is the guide to setup application test and logic test.

以下是设置应用程序测试和逻辑测试的指南。

If you want to change it to logic test (so it run faster and don't need to start your app first):

如果你想将它改为逻辑测试(所以它运行得更快,不需要先启动你的应用程序):

  1. go to build settings for your unit test target
  2. 去构建单元测试目标的设置
  3. search Bundle
  4. 搜索捆绑
  5. remove Bundle Loader and Test Host
  6. 删除Bundle Loader和Test Host

XCode 5单元测试:启动我的应用程序

#2


10  

Thats right, you have to delete the "Bundle Loader" and "Test Host" from your build settings.

多数民众赞成在右边,您必须从构建设置中删除“Bundle Loader”和“Test Host”。

But you have to add the necessary implementation files to your unit test target. The necessary files are what you want to use in your unit test cases. You need to do this because in logic tests XCode wont compile the whole application. So some of your files will be missing.

但是您必须将必要的实现文件添加到单元测试目标中。必要的文件是您要在单元测试用例中使用的文件。你需要这样做,因为在逻辑测试中,XCode不会编译整个应用程序。所以你的一些文件将丢失。

This is en error message if you have left out a file:

如果您遗漏了文件,则会显示错误消息:

Undefined symbols for architecture i386:
  "_OBJC_CLASS_$_Module", referenced from:
      objc-class-ref in Lobic Network.o
      objc-class-ref in Logic_Unit.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

You can add the missing files by selecting the implementation file and bringing up the file inspector. There will be a section named "Target Membership" and there you can set the files target membership to your unit test also.

您可以通过选择实施文件并调出文件检查器来添加缺少的文件。将有一个名为“目标成员资格”的部分,您也可以将文件目标成员资格设置为单元测试。

#3


9  

With XCTest, application files DO NOT need to be included within XCTest targets. The XCTest bundle is linked against the application which makes those files available during runtime.

使用XCTest,应用程序文件不需要包含在XCTest目标中。 XCTest包与应用程序链接,后者在运行时使这些文件可用。

To make this work, ensure the compiler option "Symbols hidden by default" is set to NO Within the Application target.

要使其工作,请确保编译器选项“默认情况下隐藏的符号”在应用程序目标中设置为NO。

Here is a blog post with screenshots for clarity: http://zmcartor.github.io/code/2014/02/24/slim-xctest-targets

这是一篇博客文章,屏幕截图清晰:http://zmcartor.github.io/code/2014/02/24/slim-xctest-targets

The advantage of this approach is test target builds much much faster.

这种方法的优点是测试目标的构建速度要快得多。

#4


4  

In XCode 7, removing Host Application does not work for me. Indeed I use the following to avoid app runs.

在XCode 7中,删除主机应用程序对我不起作用。确实,我使用以下内容来避免应用程序运行。

  1. Setup Test Scheme Arguments XCode 5单元测试:启动我的应用程序

    设置测试方案参数

  2. in main.m

    在main.m

    static bool isRunningTests()
    {
        NSDictionary* environment = [[NSProcessInfo processInfo] environment];
        NSString* testEnabled = environment[@"TEST_ENABLED"];
        return [testEnabled isEqualToString:@"YES"];
    }
    
  3. modify main()

    修改main()

    int main(int argc, char * argv[]) {
        @autoreleasepool {
            if (isRunningTests()) {
                return UIApplicationMain(argc, argv, nil, nil);
            } else {
                return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
            }
        }
    }
    

#5


2  

If the tests are for code that can run on desktop and mobile, you can run them without a simulator or hosting them within your app.

如果测试适用于可在桌面和移动设备上运行的代码,则可以在没有模拟器的情况下运行它们,也可以在应用程序中托管它们。

The trouble is that you cannot use the scheme chooser for your normal target (desktop or iOS) to run the test.

问题是您不能将方案选择器用于正常目标(桌面或iOS)来运行测试。

The following worked for me in Xcode6.

以下在Xcode6中为我工作。

File > New Target...

Select Cocoa Testing Bundle from the OS X category.

从OS X类别中选择Cocoa Testing Bundle。

XCode 5单元测试:启动我的应用程序

Take care to select None from the target drop-down.

注意从目标下拉列表中选择“无”。

XCode 5单元测试:启动我的应用程序

Click Finish. Add the relevant files to the new target as described above.

单击完成。如上所述,将相关文件添加到新目标。

Now create a scheme to run the test.

现在创建一个运行测试的方案。

Click the schemes chooser top-right and choose New Scheme..., click the drop-down and navigate down the list to the new target. Now you can choose the scheme from the schemes chooser, and use ⌘U to run the tests.

单击右上角的方案选择器并选择New Scheme ...,单击下拉列表并向下导航到新目标。现在,您可以从方案选择器中选择方案,并使用⌘U运行测试。

XCode 5单元测试:启动我的应用程序

#6


1  

I just wasted a morning on this.

我只是浪费了一个早上。

Project was created in XCode 4 and used SenTesting.

项目是在XCode 4中创建的,并使用了SenTesting。

Tried migrating tests on XCode 5/XCTTest

尝试在XCode 5 / XCTTest上进行迁移测试

Had same issue - app ran in simulator and test never started after trying everything (change from app to logic tests, change to XCTest, remove SenTesting)

有同样的问题 - 应用程序在模拟器中运行,测试从未尝试过所有内容(从应用程序更改为逻辑测试,更改为XCTest,删除SenTesting)

gave up created a clean XCode 5 project.

放弃了创建一个干净的XCode 5项目。

Added all my files in and tests ran ok.

添加了我的所有文件,测试运行正常。

May still have issues with Storyboard as these were built with XCode 4.

可能仍然存在Storyboard的问题,因为这些是使用XCode 4构建的。

Drastic but it works so keep it as last resort.

很有吸引力,但它的工作原理,所以保持最后的手段。

#7


1  

On XCode5, the app does start. This answer shows how to change its delegate when running unit tests so that it exits right away: https://*.com/a/20588035/239408

在XCode5上,应用程序确实启动了。这个答案显示了如何在运行单元测试时更改其委托,以便它立即退出:https://*.com/a/20588035/239408