iOS(OC)中动态加载运行测试代码
// TestLoadder.m // WGTestFramework // // Created by liujc on 2017/11/27. // Copyright © 2017年 liujc. All rights reserved. // #import "TestLoadder.h" #import "TestDataLoader.h" #import "GlobalVariable.h" #import "Report.h" #import "Utils.h" #import <objc/runtime.h> #import <UIKit/UIDevice.h> @implementation TestLoadder static TestLoadder* _instance=nil; @synthesize TestProjectVersion=_testProjectVersion; +(instancetype) shareInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; } + (id)allocWithZone:(struct _NSZone *)zone { @synchronized (self) { if(_instance==nil){ _instance=[super allocWithZone:zone]; } } return _instance; } - (id) copyWithZone:(NSZone *)zone { return self; } - (void)runTest:(NSString *)Classes DefaultClass:(NSString*) defaultClass DefaultMethod:(NSString *) defaultMethod Category:(NSString *)category { if(Classes==nil || [Classes length]==0){ NSLog(@"@Classes don't nil"); return; } classesArray=Classes; defaultClassName=defaultClass; defaultMethodName=defaultMethod; testCategory=category; NSThread *thread=[[NSThread alloc] initWithTarget:self selector:@selector(ThreadRunTestCases) object:nil]; [thread start]; } - (void) collectTestSummaryAndSendResultToServer{ Report *report=[[Report shareInstance] CreateNewReport]; [report SetValue:@"subject" Value:[GlobalVariable shareInstance].report_title]; [report SetValue:@"sdk_version" Value:self.TestProjectVersion]; [report SetValue:@"platform" Value:[[UIDevice currentDevice] systemVersion]]; [report SetValue:@"startTime" Value:[self dateFormat:startTimeDate]]; [report SetValue:@"endTime" Value:[self dateFormat:endTimeDate]]; int duration=[[NSNumber numberWithDouble:[endTimeDate timeIntervalSince1970]] integerValue]-[[NSNumber numberWithDouble:[startTimeDate timeIntervalSince1970]] integerValue]; [report SetValue:@"duration" Value:[NSString stringWithFormat:@"%@", [self duration_string:duration]]]; [report SetValue:@"tolist" Value:[GlobalVariable shareInstance].tolist]; [report SaveToDisk]; NSData *result_data = [[NSData alloc] initWithContentsOfFile:[GlobalVariable shareInstance].result_path]; TestDataLoader *tdLoader = [[TestDataLoader alloc] init]; NSString *send_result = [tdLoader sendRequest_sync:@"" type:@"post" data:result_data tcfolder:@"report"]; NSLog(@"%@", send_result); } - (NSString *)duration_string:(int) duration { NSString *str_duration=@""; int mod=0; int remainder=0; int times=60*60*24; if(duration>times){ mod=duration/times; duration=duration%times; if(mod>0){ str_duration=[str_duration stringByAppendingFormat:@"%i days ", mod]; } } times=60*60; if(duration>times){ mod=duration/times; duration=duration%times; if(mod>0){ str_duration=[str_duration stringByAppendingFormat:@"%i hours ", mod]; } } times=60; if(duration>60){ mod=duration/times; duration=duration%times; if(mod>0){ str_duration=[str_duration stringByAppendingFormat:@"%i minutes ", mod]; } } str_duration=[str_duration stringByAppendingFormat:@"%i seconds", duration]; return str_duration; } - (NSString *)dateFormat:(NSDate *) date { NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSTimeZone *timeZone=[NSTimeZone timeZoneWithName:@"Asia/Beijing"]; [formatter setTimeZone:timeZone]; return [formatter stringFromDate:date]; } - (void)ThreadRunTestCases{ extern NSString* case_category; NSArray *classes=[classesArray componentsSeparatedByString:@","]; NSString *default_class_name=defaultClassName; NSString *method_name=defaultMethodName; NSString *m_name; Method meth=nil; Class tclass=nil; unsigned int count=0; objc_property_t *method=nil; @autoreleasepool { [[GlobalVariable shareInstance] create_path]; startTimeDate=[NSDate date]; for(NSString *class_name in classes){ if([default_class_name length]>0 && ![default_class_name isEqual:class_name]){ continue; } tclass=[[NSClassFromString(class_name) alloc]init]; count=0; method=class_copyMethodList([tclass class], &count); for(int i=0;i<count;i++){ meth=method[i]; SEL sel=method_getName(meth); m_name=[NSString stringWithUTF8String:sel_getName(sel)]; if([m_name hasPrefix:@"test_"]){ if([method_name length]>0){ if ([method_name isEqual:m_name]) { [tclass performSelector:NSSelectorFromString(m_name)]; } } else{ [tclass performSelector:NSSelectorFromString(m_name)]; } } } } endTimeDate=[NSDate date]; [self collectTestSummaryAndSendResultToServer]; } sleep(5); exit(0); } @end