GitHunt
JS

Json031/SNUnitTestsOC

SNUnitTestsOC 是一个基于XCTest框架开发的Objective-C开源项目,集成了 UI 自动化测试、高并发 以及 覆盖 API与类方法等范围的单元测试,方便开发者对应用程序的 UI 、 API、类方法高并发等模块 进行全面的自动化单元测试。

Click here to go to the Swift version

SNUnitTestsOC

CocoaPods
License


🌍 Language / 语言选择

English | 中文


📖 English

Overview

SNUnitTestsOC is an Objective-C open-source project developed based on the XCTest framework. It integrates UI automation testing, high concurrency testing, and unit testing covering APIs and class methods, making it convenient for developers to conduct comprehensive automated unit testing on UI, API, and high-concurrency modules of their applications.

Installation

CocoaPods

The SNUnitTestsOC SDK for iOS is available through CocoaPods.

First, install CocoaPods if you haven't already:

brew install cocoapods
pod setup

Add to your Podfile:

$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 Installation

Drag the Classes folder into your project. For Objective-C projects, bridging may be required.

Unit Test Example

Unit Test Example

Example Class to Test

#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

Unit Test Code

#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];
        // Ensure snUnitTests is loaded
        XCTAssertNotNil(self.snUnitTests);
    }
    
    return true;
}

// UnityTool class unit test
-(void)testUnityToolClass {
    // Test instance method
    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);
    
    // Test class method
    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);
    
    // Test class method with assertion
    [self.snUnitTests testClassMethodCall:^id(id param) {
        return @([UnityTool isEmptyString:(NSString *)param]);
    }
                                    param:@"sds"
                                 expected:@(NO)
                              failMessage:nil];
    
    // Test instance method with assertion
    [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];
    
    // High concurrency unit test
    [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

Issues or Improvement Suggestions

If you find any issues or have improvement suggestions, please submit an issue or pull request on GitHub.

License

This project is licensed under the MIT License.


📖 中文

项目简介

SNUnitTestsOC 是一个基于 XCTest 框架开发的 Objective-C 开源项目,集成了 UI 自动化测试、高并发以及覆盖 API 与类方法等范围的单元测试,方便开发者对应用程序的 UI、API、类方法高并发等模块进行全面的自动化单元测试。

安装

CocoaPods

SNUnitTestsOC SDK for iOS 可通过 CocoaPods 安装。

首先,如果您还没有安装 CocoaPods,请先安装:

brew install cocoapods
pod setup

在 Podfile 中添加:

$iOSVersion = '11.0'

platform :ios, $iOSVersion
use_frameworks!

target 'YourProjectName' do

    target 'YourProjectNameTests' do
        inherit! :search_paths

        pod 'SNUnitTestsOC' # 包含所有功能的完整版本
    end
end

手动安装

Classes 文件夹拽入项目中,OC 项目还需要桥接。

单元测试示例

单元测试示例

待测试类示例

#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 上提交 issuepull request

许可证

本项目基于 MIT License 开源协议。


⬆ Back to Top / 返回顶部