我根据"iPhone开发指南"创建了OCUnit测试.这是我要测试的类:
// myClass.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface myClass : NSObject {
UIImage *image;
}
@property (readonly) UIImage *image;
- (id)initWithIndex:(NSUInteger)aIndex;
@end
// myClass.m
#import "myClass.m"
@implementation myClass
@synthesize image;
- (id)init {
return [self initWithIndex:0];
}
- (id)initWithIndex:(NSUInteger)aIndex {
if ((self = [super init])) {
NSString *name = [[NSString alloc] initWithFormat:@"image_%i", aIndex];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
image = [[UIImage alloc] initWithContentsOfFile:path];
if (nil == image) {
@throw [NSException exceptionWithName:@"imageNotFound"
reason:[NSString stringWithFormat:@"Image (%@) with path \"%@\" for …Run Code Online (Sandbox Code Playgroud) 我发现Xcode 7(版本7.0(7A220))改变了+load在单元测试期间调用类和类别的方法的顺序.
如果属于测试目标的类别实现了一个+load方法,那么现在可以在最后调用它,此时类的实例可能已经被创建和使用.
我有一个AppDelegate实现+load方法.该AppDelegate.m文件还包含AppDelegate (MainModule)类别.此外,还有一个单元测试文件LoadMethodTestTests.m,其中包含另一个类别 - AppDelegate (UnitTest).
这两个类别也实现了+load方法.第一类属于主要目标,第二类属于测试目标.
我做了一个小测试项目来证明这个问题.它是一个空的默认Xcode一个视图项目,只更改了两个文件.
AppDelegate.m:
#import "AppDelegate.h"
@implementation AppDelegate
+(void)load {
NSLog(@"Class load");
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"didFinishLaunchingWithOptions");
return YES;
}
@end
@interface AppDelegate (MainModule)
@end
@implementation AppDelegate (MainModule)
+(void)load {
NSLog(@"Main Module +load");
}
@end
Run Code Online (Sandbox Code Playgroud)
和一个单元测试文件(LoadMethodTestTests.m):
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "AppDelegate.h"
@interface LoadMethodTestTests : XCTestCase
@end
@interface AppDelegate (UnitTest) …Run Code Online (Sandbox Code Playgroud) 我一直在尝试在iOS应用程序的单元测试中加载一个简单的文本文件.
NSString* resourcePath = [[NSBundle mainBundle] pathForResource: @"stopPointsInCircleData" ofType:@"txt"];
NSString* stopPointData = [NSData dataWithContentsOfFile: resourcePath];
Run Code Online (Sandbox Code Playgroud)
我的问题是pathForResource返回nil.
文件stopPointsInCircleData.txt位于测试代码的目录中,文件在测试目标的"构建阶段"中的"复制包资源"下正确列出.
我试图通过将type设置为nil来放松搜索,但是它也没有用.
任何帮助都非常有用.
我正在尝试通过快照当前屏幕元素(标签,图像,按钮)并将其可访问性信息保存到json文件来扩展Xcode 7中的新UI测试功能.
我们的想法是,在以后运行UI测试时,可以获取当前屏幕快照并与现有屏幕快照进行比较,如果找到其他元素或缺少元素,测试将失败.
遗憾的是,即使使用正确的目标,UI测试期间似乎也无法使用应用程序资源,因此无法加载json文件进行比较.以下标准代码无法加载资源:
guard let resourcePath = NSBundle.mainBundle ().pathForResource ("StartScreenShapshot", ofType:"json") else {
XCTFail ("can't load resource StartScreenShapshot")
return
}
Run Code Online (Sandbox Code Playgroud)
我可以理解为什么Apple采用这种沙盒方法,因为UI测试应该基于屏幕上发生的事情,并且不需要访问应用程序的工作,但是不能访问资源包是一件痛苦的事.
那么有没有办法在Xcode 7 UI测试期间从应用程序或本地其他方式加载本地资源?
在本地(自动)保存文件也是一个巨大的优势,可以节省手动创建它们.
unit-testing ×3
ios ×2
nsbundle ×2
objective-c ×2
xcode7 ×2
ocunit ×1
resources ×1
testing ×1
xcode ×1