Bazel系列-bazel test 使用https(一)

bazel 提供了 unit test和ui test的rules
分别是:
ios_unit_test 和 ios_ui_test
但是当我们尝试用ios_unit_test 去写https网络请求的时候会出现请求失败的现象

如图

image.png

错误日志:


2021-12-14 15:44:20.491 xctest[11320:31711385] 请求开始
2021-12-14 15:44:20.801 xctest[11320:31711424] 请求完成...
2021-12-14 15:44:20.802 xctest[11320:31711424] error------- : The certificate for this server is invalid. You might be connecting to a server that is pretending to be “baidu.com” which could put your confidential information at risk.
Test Case '-[UserTests testDown]' passed (0.325 seconds).
Test Suite 'UserTests' passed at 2021-12-14 15:44:20.805.
     Executed 1 test, with 0 failures (0 unexpected) in 0.325 (0.326) seconds
Test Suite 'User_test.xctest' passed at 2021-12-14 15:44:20.806.
     Executed 1 test, with 0 failures (0 unexpected) in 0.325 (0.327) seconds
Test Suite 'All tests' passed at 2021-12-14 15:44:20.806.
     Executed 1 test, with 0 failures (0 unexpected) in 0.325 (0.350) seconds

BUILD代码

objc_library(
    name = "User_test_library",
    testonly = 1,
    srcs = glob(["UserTests/*.m"]),
)

ios_unit_test(
    name = "User_test",
    minimum_os_version = "9.0",
    deps = [
        ":User_test_library",
    ]
)

UserTests代码:

#import 
#import 
@interface UserTests : XCTestCase

@end

@implementation UserTests

- (void)setUp {
}

- (void)testDown {
    NSLog(@"请求开始");

    XCTestExpectation *expectation = [self expectationWithDescription:@"异步加载 Person"];
    NSString *url = @"https://baidu.com";
    NSString *urlStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:
            ^(NSData *data, NSURLResponse *response, NSError *error) {
            NSLog(@"请求完成...");
            if (!error) {
                NSString * str  =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                NSLog(@"str-------------:%@",str);
                
                [expectation fulfill];
                
            } else {
                NSLog(@"error------- : %@", error.localizedDescription);
                [expectation fulfill];

            }

        }];
    [task resume];

    [self waitForExpectationsWithTimeout:5 handler:nil];

}
@end
发现在unit的时候, 使用https报The certificate for this server is invalid错误 ,查阅资料, 都没有找到相关记载,于是翻看rules_apple 和xctestrunner 这两个rules是直接和ios_unit_test相关的两个rules

大致流程如下:

image.png
0.build_bazel_rules_apple 构建xctest ,tmp目录, workdir等,获取我们定义的参数, 替换模板,调用ios_test_runner.py
1.构建xctest_session类 ,保存rules_apple 过来的参数 ,bundle(xctest),app_path,work_dir然后处理传过来的的参数,拼装APP目录等, 用来构建_xctestrun_obj
2.session_obj ,调用
['xcrun', 'simctl', 'spawn', '-s’,’-arch’,’x86, '4418A866-2C5B-4C36-BC88-6A4C6895C40C', '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents/xctest', '-XCTest', 'All', '/Users/username/Documents/bazeltest/tmp/test_runner_work_dir.XeXTe9/user_test/User_test.xctest']

你可能感兴趣的:(Bazel系列-bazel test 使用https(一))