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

bazel 提供了 unit test和ui test的rules
分別是:
ios_unit_test 和 ios_ui_test
但是當(dāng)我們嘗試用ios_unit_test 去寫https網(wǎng)絡(luò)請(qǐng)求的時(shí)候會(huì)出現(xiàn)請(qǐng)求失敗的現(xiàn)象

如圖

image.png

錯(cuò)誤日志:


2021-12-14 15:44:20.491 xctest[11320:31711385] 請(qǐng)求開(kāi)始
2021-12-14 15:44:20.801 xctest[11320:31711424] 請(qǐng)求完成...
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 <XCTest/XCTest.h>
#import <UIKit/UIKit.h>
@interface UserTests : XCTestCase

@end

@implementation UserTests

- (void)setUp {
}

- (void)testDown {
    NSLog(@"請(qǐng)求開(kāi)始");

    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(@"請(qǐng)求完成...");
            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
發(fā)現(xiàn)在unit的時(shí)候, 使用https報(bào)The certificate for this server is invalid錯(cuò)誤 ,查閱資料, 都沒(méi)有找到相關(guān)記載,于是翻看rules_apple 和xctestrunner 這兩個(gè)rules是直接和ios_unit_test相關(guān)的兩個(gè)rules

大致流程如下:

image.png
0.build_bazel_rules_apple 構(gòu)建xctest ,tmp目錄, workdir等,獲取我們定義的參數(shù), 替換模板,調(diào)用ios_test_runner.py
1.構(gòu)建xctest_session類 ,保存rules_apple 過(guò)來(lái)的參數(shù) ,bundle(xctest),app_path,work_dir然后處理傳過(guò)來(lái)的的參數(shù),拼裝APP目錄等, 用來(lái)構(gòu)建_xctestrun_obj
2.session_obj ,調(diào)用
['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']
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容