如何从测试目标下的生产代码获取测试目标包 Plist 文件?
How do I reach Test Target Bundle Plist-file from production code under the Test Target?
我需要从生产代码中标记某些特定于测试的情况,并且由于 swift 中不再存在预编译宏(并且由于其他 Swift 标记似乎在目标基础上工作,但不是在测试目标特定的基础上)我想我可以使用测试 plist 来访问仅在测试期间可用的属性。
如果此策略有效,我需要从 AppDelegates application:didFinishLaunchingWithOption: -方法访问我的测试目标 plist 文件。我在 plist 中有一个名为 TEST_TARGET 的 属性,在这个例子中它是一个字符串。
A related topic is discussed here。在我的 AppDelegates application:didFinishLaunchingWithOption: -方法中,我有以下代码行:
var testSetting = NSBundle(forClass: self.dynamicType).objectForInfoDictionaryKey("TEST_TARGET") as? String
println("Testtarget: \(testSetting)")
当运行这行代码打印Testtarget: nil。但是,在 TestClass 设置方法中添加完全相同的代码行会呈现 Testtarget: Optional("\"This is a test target\"")
的预期结果
在我看来,NSBundle(forClass: self.dynamicType) 在从 AppDelegate 调用时不会 return testBundle,这在某种程度上是合乎逻辑的,因为 AppDelegate class 在 mainBundle 中。但是,有什么方法可以让我从生产代码到达 testPlist 吗?能否在不破坏测试和代码架构的任何基本原则的情况下做到这一点。
编辑:只有拼写错误
在 objC 中我使用了这个:
获取测试包的 plist:
Class cls = NSClassFromString(@"YourOwnTestCaseClass");
if(cls) {
//the plist
id dict = [[NSBundle bundleForClass:cls] infoDictionary];
//the path to it (IF you need it)
id testPlistPath = [[NSBundle bundleForClass:cls] pathForResource:@"Info" ofType:@"plist"];
}
在 swift 我想我可以使用 allBundles
let bundles = NSBundle.allBundles()
for bundle in bundles {
let val = bundle.objectForInfoDictionaryKey("TESTING")
if val != nil {
println(bundle.infoDictionary)
}
}
但在 SWIFT
中都不起作用
不同的解决方案
所以我建议在 SCHEME 中设置环境变量进行测试
然后像
一样在你的代码中检查它
let val = getenv("TESTING") //Val is a C String
更好:
let dict = NSProcessInfo.processInfo().environment
let testing : AnyObject? = dict["TESTING"] //get a swift object
我需要从生产代码中标记某些特定于测试的情况,并且由于 swift 中不再存在预编译宏(并且由于其他 Swift 标记似乎在目标基础上工作,但不是在测试目标特定的基础上)我想我可以使用测试 plist 来访问仅在测试期间可用的属性。
如果此策略有效,我需要从 AppDelegates application:didFinishLaunchingWithOption: -方法访问我的测试目标 plist 文件。我在 plist 中有一个名为 TEST_TARGET 的 属性,在这个例子中它是一个字符串。
A related topic is discussed here。在我的 AppDelegates application:didFinishLaunchingWithOption: -方法中,我有以下代码行:
var testSetting = NSBundle(forClass: self.dynamicType).objectForInfoDictionaryKey("TEST_TARGET") as? String
println("Testtarget: \(testSetting)")
当运行这行代码打印Testtarget: nil。但是,在 TestClass 设置方法中添加完全相同的代码行会呈现 Testtarget: Optional("\"This is a test target\"")
的预期结果在我看来,NSBundle(forClass: self.dynamicType) 在从 AppDelegate 调用时不会 return testBundle,这在某种程度上是合乎逻辑的,因为 AppDelegate class 在 mainBundle 中。但是,有什么方法可以让我从生产代码到达 testPlist 吗?能否在不破坏测试和代码架构的任何基本原则的情况下做到这一点。
编辑:只有拼写错误
在 objC 中我使用了这个:
获取测试包的 plist:
Class cls = NSClassFromString(@"YourOwnTestCaseClass");
if(cls) {
//the plist
id dict = [[NSBundle bundleForClass:cls] infoDictionary];
//the path to it (IF you need it)
id testPlistPath = [[NSBundle bundleForClass:cls] pathForResource:@"Info" ofType:@"plist"];
}
在 swift 我想我可以使用 allBundles
let bundles = NSBundle.allBundles()
for bundle in bundles {
let val = bundle.objectForInfoDictionaryKey("TESTING")
if val != nil {
println(bundle.infoDictionary)
}
}
但在 SWIFT
中都不起作用不同的解决方案
所以我建议在 SCHEME 中设置环境变量进行测试
然后像
一样在你的代码中检查它let val = getenv("TESTING") //Val is a C String
更好:
let dict = NSProcessInfo.processInfo().environment
let testing : AnyObject? = dict["TESTING"] //get a swift object