SNUnitTestsOC 是一个基于XCTest框架开发的Objective-C开源项目,集成了 UI 自动化测试、高并发 以及 覆盖 API与类方法等范围的单元测试,方便开发者对应用程序的 UI 、 API、类方法高并发等模块 进行全面的自动化单元测试。
SNUnitTestsOC is a Objective-C open source project developed based on the XCTest framework, which integrates UI automation testing, high concurrency, and unit testing covering APIs and class methods, making it convenient for developers to understand the UI API、 Conduct comprehensive automated unit testing on high concurrency modules such as class methods.
- 最新版本 Latest Version:
- CocoaPods The SNUnitTestsOC SDK for iOS is available through CocoaPods. If CocoaPods is not installed, install it using the following command. Note that Ruby will also be installed, as it is a dependency of Cocoapods.
brew install cocoapods
pod setup
$iOSVersion = '11.0'
platform :ios, $iOSVersion
use_frameworks!
target 'YourProjectName' do
target 'YourProjectNameTests' do
inherit! :search_paths
pod 'SNUnitTestsOC' # Full version with all features
end
end
- 手动安装 manual install
将Classes文件夹拽入项目中,OC项目还需要桥接
Drag the Classes folder into the project, OC project still needs bridging
单元测试示例代码 Unit Test Example Code
#import "UnityTool.h"
@implementation UnityTool
- (BOOL)isEmptyString:(NSString *)sourceStr {
if ([sourceStr isEqual:@""]) {
return YES;
}
if (sourceStr == nil) {
return YES;
}
return NO;
}
+ (BOOL)isEmptyString:(NSString *)sourceStr {
if ([sourceStr isEqual:@""]) {
return YES;
}
if (sourceStr == nil) {
return YES;
}
return NO;
}
@end
#import <XCTest/XCTest.h>
#import "SNUnitTests.h"
#import "UnityTool.h"
@interface MyProjectDataToolTests : XCTestCase
@property (nonatomic, strong) SNUnitTests *snUnitTests;
@end
@implementation MyProjectDataToolTests
- (BOOL)setUpWithError:(NSError *__autoreleasing _Nullable *)error {
if (self.snUnitTests == nil) {
self.snUnitTests = [[SNUnitTests alloc] init];
// 确保 snUnitTests 加载完成
XCTAssertNotNil(self.snUnitTests);
}
return true;
}
//UnityTool类单元测试
-(void)testUnityToolClass {
id resultObj = [self.snUnitTests getMethodResultWithClass:[UnityTool class] method:^id(id instance) {
return ^id(id param) {
return @([(UnityTool *)instance isEmptyString:(NSString *)param]);
};
}
param:@"sds"];
Boolean result = [resultObj boolValue];
NSLog(@"UnityTool isEmptyString result: %hhu", result);
id classMethodResultObj = [self.snUnitTests getClassMethodResult:^id(id param) {
return @([UnityTool isEmptyString:(NSString *)param]);
}
param:@"sds"];
Boolean classMethodResult = [classMethodResultObj boolValue];
NSLog(@"UnityTool isEmptyString result:%hhu", classMethodResult);
[self.snUnitTests testClassMethodCall:^id(id param) {
return @([UnityTool isEmptyString:(NSString *)param]);
}
param:@"sds"
expected:@(NO)
failMessage:nil];
[self.snUnitTests testMethodCallWithClass:[UnityTool class]
method:^id(id instance) {
return ^id(id param) {
return @([(UnityTool *)instance isEmptyString:(NSString *)param]);
};
}
param:@"sds"
expected:@(NO)
failMessage:nil];
//高并发单元测试
[self.snUnitTests highConcurrencyUnitTestingForClassMethodWithIterations:1000 timeoutSeconds:1000 classType:[UnityTool class] method:^id(id instance) {
return ^id(id param) {
return @([(UnityTool *)instance isEmptyString:(NSString *)param]);
};
} param:@"dsad" expected:@(NO) verbose:NO];
}
@end
如果你发现任何问题或有改进建议,请在 GitHub 上提交 issue 或 pull request。
If you find any issues or have improvement suggestions, please submit issue Or [pull request]pull request on GitHub.
本项目采用 MIT 许可证,详情请参阅 MIT License 文件。
This project adopts the MIT license, please refer to the MIT License document for details.