UITesting自动化测试log日志
2017-05-10 本文已影响422人
沈宥
1、XCTestSuite
官方解释:
@interface XCTestSuite : XCTest {
#ifndef __OBJC2__
@private
id _internalImplementation;
#endif
}
+ (instancetype)defaultTestSuite;
+ (instancetype)testSuiteForBundlePath:(NSString *)bundlePath;
+ (instancetype)testSuiteForTestCaseWithName:(NSString *)name;
+ (instancetype)testSuiteForTestCaseClass:(Class)testCaseClass;
+ (instancetype)testSuiteWithName:(NSString *)name;
- (instancetype)initWithName:(NSString *)name NS_DESIGNATED_INITIALIZER;
- (void)addTest:(XCTest *)test;
@property (readonly, copy) NSArray <__kindof XCTest *> *tests;
@end
综上可知,该类可在任意地方获取已经建立的XCTestCase用例个数,可通过用例名、类名来查找具体的用例;也可通过属性获取用例数组后,遍历获取用例;
举个栗子:
XCTestSuite *suite = [XCTestSuite defaultTestSuite];
NSArray *array = suite.tests;
for (XCTestCase *cases in array) {
NSLog(@"%@",cases.description);
}
运行结果:

2、获取完整log日志:
日志存储路径:
/Users/username
/Library/Developer/Xcode/DerivedData/project
/Logs/Test

(1)每个case执行结果截图在Attachments
文件夹;
(2)case执行正确、错误数量:.plist
文件
(3)log输出:.log
文件
(4)已经点击成功的控件元素本地缓存:Cache.db
备注:当某个用例执行失败时,无日志保存。根据这些成功后的信息,后续想完成一个log显示系统
3、解析并简单显示测试结果
(1)通过.plist
文件找出所有的执行case详细信息:

其中,ActivitySummaries中包含了所有的case信息,但是我们只需要拿到:
Title:case信息;
UUID:截图保存的名称;
备注:关于UUID有一些又多层叠加,所以需要在子层中找到存在的截图名称,如下图中的第三层才是正确的:

(2)Python解析文件,并编写HTML给出简单的日志分析表:
首先安装PyH,使用方法点击此处。
安装:
sudo python setup.py install
关于源码,见请GitHub
(3)在解析的过程中过滤了大量没有截图的信息:
def function_dictionary(rootPath,dictionary,title_array):
title = str(dictionary['Title'])
imagePath = append_image_path(rootPath,dictionary['UUID'])
global isWait
if "Synthesize event" in title:
return title_array
elif "Snapshot accessibility hierarchy for com.meelive.ingkee" in title:
return title_array
elif "Use cached accessibility hierarchy for com.meelive.ingkee" in title:
return title_array
elif "Unable to" in title:
return title_array
elif "Set Up" in title:
return title_array
elif "Terminate" in title:
return title_array
elif "Get" in title:
return title_array
elif "Find" in title:
return title_array
elif "Launch com.meelive.ingkee" in title:
return title_array
elif "Start Test" in title:
return title_array
else:
title_array.append({title:imagePath})
return title_array

结果展示页:
