Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIScene.body(Xcode 12.5): “整个应用程序必须运行并启动其故事板和根视图控制器”是否正确?我还没有测试过,但我觉得不太对劲。嗯……是的,应用程序确实完成了启动,并且正在运行根视图控制器的viewdidload,刚刚测试过。我不认为是_Ios_Swift_Xcode_Xctest - Fatal编程技术网

Ios UIScene.body(Xcode 12.5): “整个应用程序必须运行并启动其故事板和根视图控制器”是否正确?我还没有测试过,但我觉得不太对劲。嗯……是的,应用程序确实完成了启动,并且正在运行根视图控制器的viewdidload,刚刚测试过。我不认为是

Ios UIScene.body(Xcode 12.5): “整个应用程序必须运行并启动其故事板和根视图控制器”是否正确?我还没有测试过,但我觉得不太对劲。嗯……是的,应用程序确实完成了启动,并且正在运行根视图控制器的viewdidload,刚刚测试过。我不认为是,ios,swift,xcode,xctest,Ios,Swift,Xcode,Xctest,UIScene.body(Xcode 12.5): “整个应用程序必须运行并启动其故事板和根视图控制器”是否正确?我还没有测试过,但我觉得不太对劲。嗯……是的,应用程序确实完成了启动,并且正在运行根视图控制器的viewdidload,刚刚测试过。我不认为是这样的lol。是什么原因导致了你的测试出现问题?也许还有其他方法可以解决这个问题?我需要让应用程序知道它在运行时考虑了测试,所以像旧的预处理器宏这样的标志可以工作,但是swift不支持它们。是的,但是为什么你“需要”这样做呢?是什么让你认为你需

UI
Scene.body
(Xcode 12.5):



“整个应用程序必须运行并启动其故事板和根视图控制器”是否正确?我还没有测试过,但我觉得不太对劲。嗯……是的,应用程序确实完成了启动,并且正在运行根视图控制器的viewdidload,刚刚测试过。我不认为是这样的lol。是什么原因导致了你的测试出现问题?也许还有其他方法可以解决这个问题?我需要让应用程序知道它在运行时考虑了测试,所以像旧的预处理器宏这样的标志可以工作,但是swift不支持它们。是的,但是为什么你“需要”这样做呢?是什么让你认为你需要这样做?这只适用于单元测试,而不适用于新的Xcode 7 UI测试。在最新的Xcode中不起作用-环境变量名称似乎已更改不确定何时停止工作,但是对于Xcode 7.3,我现在使用的是
XCTestConfigurationFilePath
环境键,而不是
XCInjectBundle
。谢谢@ospr,我已经编辑了使用Xcode 7的答案。3@tkuichooseyou
po ProcessInfo.ProcessInfo.environment
没有键
XCTestConfigurationFilePath
。你能分享你的代码吗?在单元测试的Xcode 9.2和Xcode 9.3 beta版中,这是一个UITest targetWorks的检查。如果我们在所有方案的“测试”中添加它,这不是更有帮助吗?这样,isRunningTests将在所有计划中都起作用。@VishalSingh是的,我认为这样更干净。你试过那种方法吗?让我们知道它是否对您同样有效。安全警报:我会将其包装在一个大的#if DEBUG子句中,否则您会让“测试”后门进入产品中如何在删除主机应用程序后为目标包含桥接头?尝试了这个,它打破了我的测试,直到我得到了一个相反的答案:如果我这样做,我可以测试cloudKit(例如),所以我的解决方案是在ApplicationIDFinishLaunching中检测我是否正在测试,如果是,然后返回而不分配应用程序的主要类。这似乎是我迄今为止发现的最干净的方法。我们不必将所有应用程序文件添加到测试目标,也不必依赖于某种奇怪的解决方案来检查
“XCTestConfigurationFilePath”
NSClassFromString(“XCTest”)
。我在Swift中用一个全局函数实现了这个解决方案
func isRunningTests()->Bool{return UserDefaults.standard.Bool(forKey:isRunningTests”)}
在我看来,如果用合理的努力删除主机应用程序为时已晚,这是解决这个问题的最佳答案。与其他一些答案不同,这个答案是有效的。谢谢。在2021年Xcode 12.4上仍然可以工作。这比现在好多了!更短,框架兼容!正在处理Swift包,这对meThat for loop有效,可以像这样进行Swiftize:
返回threadDictionary.allKeys.anyMatch{($0 as?String)?.split(分隔符:“.”)。contains(“xTest”)==true}
anyMatch
应该是
contains(其中:)
,您也不需要返回,因为它现在是一行:
threadDictionary.allKeys.contains{($0 as?String)?.split(分隔符:“.”)。contains(“xctest”)==true}
还将指出,如果您想检测标记为情节提要入口点的视图控制器的生命周期功能中是否存在单元测试,则此操作不起作用。线程字典尚未包含这些键。在初始视图控制器上的断点上,我可以看到
NSClassFromString
变量工作,并且
ProcessInfo
变量也工作。
// Appdelegate.swift
if runningTests() {
   return
} else {
   // do ordinary api calls
}
if NSProcessInfo.processInfo().environment["XCTestConfigurationFilePath"] != nil {
     // Code only executes when tests are running
}
#if DEBUG
    if NSProcessInfo.processInfo().environment["XCTestConfigurationFilePath"] != nil {
        // Code only executes when tests are running
    }
#endif
if ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil {
    // Code only executes when tests are running
}
static NSNumber *__isRunningTests;

+ (BOOL)isRunningTests;
{
    if (!__isRunningTests) {
        NSDictionary *environment = [[NSProcessInfo processInfo] environment];
        NSString *isRunningTestsValue = environment[@"APPS_IS_RUNNING_TEST"];
        __isRunningTests = @([isRunningTestsValue isEqualToString:@"YES"]);
    }

    return [__isRunningTests boolValue];
}
// Return if this is a unit test
if let _ = NSClassFromString("XCTest") {
    return true
}
#if DEBUG
        if (NSClassFromString(@"XCTest") == nil) {
            // Your code that shouldn't run under tests
        }
#else
        // unconditional Release version
#endif
BOOL test = [[NSUserDefaults standardUserDefaults] boolForKey:@"isTest"];
var isRunningTests: Bool {
    return ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil
}
if isRunningTests {
    return "lena.bmp"
}
return "facebook_profile_photo.bmp"
func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions:
                 [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    #if DEBUG
    if let _ = NSClassFromString("XCTest") {
        // If we're running tests, don't launch the main storyboard as
        // it's confusing if that is running fetching content whilst the
        // tests are also doing so.
        let viewController = UIViewController()
        let label = UILabel()
        label.text = "Running tests..."
        label.frame = viewController.view.frame
        label.textAlignment = .center
        label.textColor = .white
        viewController.view.addSubview(label)
        self.window!.rootViewController = viewController
        return true
    }
    #endif
XCUIApplication *testApp = [[XCUIApplication alloc] init];

// set launch environment variables
NSDictionary *customEnv = [[NSMutableDictionary alloc] init];
[customEnv setValue:@"YES" forKey:@"APPS_IS_RUNNING_TEST"];
testApp.launchEnvironment = customEnv;
[testApp launch];
BOOL testing = false;
...
if (! testing) {
    NSDictionary *environment = [[NSProcessInfo processInfo] environment];
    NSString *isRunningTestsValue = environment[@"APPS_IS_RUNNING_TEST"];
    testing = [isRunningTestsValue isEqualToString:@"YES"];
}
let app = XCUIApplication()
app.launchEnvironment.updateValue("YES", forKey: "UITesting")
app.launch()
let isTesting: Bool = (ProcessInfo.processInfo.environment["UITesting"] == "YES")
    if isTesting {
        // Only if testing
    } else {
        // Only if not testing
    }
 if ProcessInfo.processInfo.environment["IS_UNIT_TESTING"] == "1" {
                 // Code only executes when tests are running
 } 
if UserDefaults.standard.value(forKey: "XCTIDEConnectionTimeout") == nil {
  // not unit testing
} else {
  // unit testing
}