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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19219706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:02:14  来源:igfitidea点击:

XCode 5 unit testing: starts my app

objective-cxcodexcode5xctest

提问by dhrm

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 应用程序的主窗口在运行测试时出现在屏幕上几秒钟。为什么?即使我取消所有测试的注释,它仍然会打开我的主窗口。

回答by Bryan Chen

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.

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

Hereis 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. search Bundle
  3. remove Bundle Loader and Test Host
  1. 去为你的单元测试目标构建设置
  2. 搜索 Bundle
  3. 删除捆绑加载器和测试主机

enter image description here

在此处输入图片说明

回答by Szasza

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.

您可以通过选择实现文件并打开文件检查器来添加丢失的文件。将有一个名为“目标成员资格”的部分,您也可以在那里将文件目标成员资格设置为您的单元测试。

回答by HackaZach

With XCTest, application files DO NOTneed 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 NOWithin the Application target.

要使此工作正常进行,请确保在应用程序目标中将编译器选项“默认隐藏的符号”设置为“” 。

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.

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

回答by Nicholas Ng

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

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

  1. Setup Test Scheme Arguments enter image description here

  2. in main.m

    static bool isRunningTests()
    {
        NSDictionary* environment = [[NSProcessInfo processInfo] environment];
        NSString* testEnabled = environment[@"TEST_ENABLED"];
        return [testEnabled isEqualToString:@"YES"];
    }
    
  3. modify 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]));
            }
        }
    }
    
  1. 设置测试方案参数 在此处输入图片说明

  2. main.m

    static bool isRunningTests()
    {
        NSDictionary* environment = [[NSProcessInfo processInfo] environment];
        NSString* testEnabled = environment[@"TEST_ENABLED"];
        return [testEnabled isEqualToString:@"YES"];
    }
    
  3. 修改 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]));
            }
        }
    }
    

回答by Sez

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。

enter image description here

在此处输入图片说明

Take care to select Nonefrom the target drop-down.

请注意None从目标下拉列表中进行选择。

Specify new test target

指定新的测试目标

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 运行测试。

enter image description here

在此处输入图片说明

回答by xverges

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://stackoverflow.com/a/20588035/239408

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

回答by brian.clear

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.

激烈,但它有效,所以把它作为最后的手段。