bazel 提供了 unit test和ui test的rules
分别是:
ios_unit_test 和 ios_ui_test
但是当我们尝试用ios_unit_test 去写https网络请求的时候会出现请求失败的现象
如图
错误日志:
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 <XCTest/XCTest.h>
#import <UIKit/UIKit.h>
@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