我遇到的问题是让Alamofire
使用xcode UI测试。
我已经尽可能地隔离了部件以尝试识别问题的位置,但仍然无法解决这个问题。使用swift的新xcode项目,我可以使用Alamofire
按预期使用主ViewController.swift
和func viewDidLoad()
函数,但是在创建UI测试时,我无法使用相同的func testExample()
函数中的代码。
使用的工具版本:
Xcode = 9.2(9C40b)
Swift = 4.0
cocoapods = 1.4.0
macOS = High Sierra Version 10.13.2 (17C205)
Alamofire = 4.5
ViewController.swift - 正常工作
import UIKit
import Alamofire
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Alamofire.request("https://httpbin.org/get").responseJSON { response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // response serialization result
if let json = response.result.value {
print("JSON: \(json)") // serialized json response
}
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)") // original server data as UTF8 string
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
结果
请求:可选(https://httpbin.org/get) 回复:可选({URL:https://httpbin.org/get} {状态代码:200,标题{ “Access-Control-Allow-Credentials”=( 真正 等...
UITestExample() - 无法正常工作
//
// exalamUITests.swift
// exalamUITests
//
import XCTest
import Alamofire
class exalamUITests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
XCUIApplication().launch()
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
Alamofire.request("https://httpbin.org/get").responseJSON { response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // response serialization result
if let json = response.result.value {
print("JSON: \(json)") // serialized json response
}
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)") // original server data as UTF8 string
}
}
}
}
结果
2018-01-29 14:55:25.974425+1000 exalamUITests-Runner[12450:454722] +[CATransaction synchronize] called within transaction
2018-01-29 14:55:26.022366+1000 exalamUITests-Runner[12450:454722] Running tests...
2018-01-29 14:55:29.229531+1000 exalamUITests-Runner[12450:454722] Continuing to run tests in the background with task ID 1
Test Suite 'Selected tests' started at 2018-01-29 14:55:29.896
Test Suite 'exalamUITests.xctest' started at 2018-01-29 14:55:29.900
Test Suite 'exalamUITests' started at 2018-01-29 14:55:29.902
Test Case '-[exalamUITests.exalamUITests testExample]' started.
t = 0.00s Start Test at 2018-01-29 14:55:29.908
t = 0.17s Set Up
t = 0.18s Open Company.exalam
t = 0.28s Launch Company.exalam
t = 6.39s Wait for Company.exalam to idle
t = 9.13s Tear Down
Test Case '-[exalamUITests.exalamUITests testExample]' passed (9.339 seconds).
Test Suite 'exalamUITests' passed at 2018-01-29 14:55:39.244.
Executed 1 test, with 0 failures (0 unexpected) in 9.339 (9.342) seconds
Test Suite 'exalamUITests.xctest' passed at 2018-01-29 14:55:39.245.
Executed 1 test, with 0 failures (0 unexpected) in 9.339 (9.346) seconds
Test Suite 'Selected tests' passed at 2018-01-29 14:55:39.249.
Executed 1 test, with 0 failures (0 unexpected) in 9.339 (9.353) seconds
Test session log:
/var/folders/ys/9lh0sqdd62s03g_d10zs46z00000gn/T/com.apple.dt.XCTest/IDETestRunSession-8C0D5A43-AB22-47EA-88C7-6AB878853EBF/ exalamUITests-9DA87773-7D75-4A9A-8127-BAE3CAB18354/Session-exalamUITests-2018-01-29_145508-bLBnaC.log
Podfile
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
target 'exalam' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
pod 'Alamofire', '~> 4.5'
# Pods for exalam
target 'exalamTests' do
inherit! :search_paths
# Pods for testing
end
target 'exalamUITests' do
inherit! :search_paths
use_frameworks!
pod 'Alamofire', '~> 4.5'
# Pods for testing
end
end
This Question类似,但它已超过一年了,我想我可以添加更多细节。
我是Mac,swift,xcode和cocoapods的新手,但我尝试了所有类似的S.O.相关答案已经并尝试了许多不同的事情。 (通常在python和linux下工作)
非常感谢任何帮助。
答案 0 :(得分:1)
Alamofire中的网络(与所有其他iOS网络库一样)是异步的。在服务器甚至处理请求之前,您的测试已经结束。这就是为什么你的responseJSON方法的闭包永远不会被触发的原因。 你必须使用期望来使它工作: https://developer.apple.com/documentation/xctest/asynchronous_tests_and_expectations/testing_asynchronous_operations_with_expectations
我很好奇,为什么你需要在UI测试中建立网络?