Joe*_*Joe 5 xcode cocoapods swift alamofire xcode-ui-testing
我遇到的问题是使用Alamofirexcode UI 测试。
我已尽可能地将各个部分隔离开来,试图找出问题可能出在哪里,但仍然无法解决这个问题。通过使用 swift 的新 xcode 项目,我可以Alamofire按预期使用 mainViewController.swift和func viewDidLoad()函数,但是在创建 UI 测试时,我无法在函数中使用相同的代码func testExample()。
使用的工具版本:
\n\nXcode = 9.2(9C40b) \nSwift = 4.0\ncocoapods = 1.4.0 \nmacOS = High Sierra Version 10.13.2 (17C205) \nAlamofire = 4.5 \nRun Code Online (Sandbox Code Playgroud)\n\nViewController.swift - 工作正常
\n\nimport UIKit\nimport Alamofire\n\nclass ViewController: UIViewController {\n\n override func viewDidLoad() {\n super.viewDidLoad()\n // Do any additional setup after loading the view, typically from a nib.\n\n Alamofire.request("https://httpbin.org/get").responseJSON { response in\n print("Request: \\(String(describing: response.request))") // original url request\n print("Response: \\(String(describing: response.response))") // http url response\n print("Result: \\(response.result)") // response serialization result\n\n if let json = response.result.value {\n print("JSON: \\(json)") // serialized json response\n }\n\n if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {\n print("Data: \\(utf8Text)") // original server data as UTF8 string\n }\n\n }\n\n\n }\n\n override func didReceiveMemoryWarning() {\n super.didReceiveMemoryWarning()\n // Dispose of any resources that can be recreated.\n }\n\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n结果
\n\n\n\n\n请求: 可选( https://httpbin.org/get )\n 响应: 可选( { URL: https://httpbin.org/get } { 状态代码: 200, headers {\n "Access-Control-Allow-凭证” = (\n true\n 等等...
\n
UITestExample() - 不工作
\n\n//\n// exalamUITests.swift\n// exalamUITests\n//\n\n\nimport XCTest\nimport Alamofire\n\nclass exalamUITests: XCTestCase {\n\n override func setUp() {\n super.setUp()\n\n // Put setup code here. This method is called before the invocation of each test method in the class.\n\n // In UI tests it is usually best to stop immediately when a failure occurs.\n continueAfterFailure = false\n // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.\n XCUIApplication().launch()\n\n // In UI tests it\xe2\x80\x99s 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.\n }\n\n override func tearDown() {\n // Put teardown code here. This method is called after the invocation of each test method in the class.\n super.tearDown()\n }\n\n func testExample() {\n Alamofire.request("https://httpbin.org/get").responseJSON { response in\n print("Request: \\(String(describing: response.request))") // original url request\n print("Response: \\(String(describing: response.response))") // http url response\n print("Result: \\(response.result)") // response serialization result\n\n if let json = response.result.value {\n print("JSON: \\(json)") // serialized json response\n }\n\n if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {\n print("Data: \\(utf8Text)") // original server data as UTF8 string\n }\n\n }\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n结果
\n\n2018-01-29 14:55:25.974425+1000 exalamUITests-Runner[12450:454722] +[CATransaction synchronize] called within transaction\n2018-01-29 14:55:26.022366+1000 exalamUITests-Runner[12450:454722] Running tests...\n2018-01-29 14:55:29.229531+1000 exalamUITests-Runner[12450:454722] Continuing to run tests in the background with task ID 1\nTest Suite \'Selected tests\' started at 2018-01-29 14:55:29.896\nTest Suite \'exalamUITests.xctest\' started at 2018-01-29 14:55:29.900\nTest Suite \'exalamUITests\' started at 2018-01-29 14:55:29.902\nTest Case \'-[exalamUITests.exalamUITests testExample]\' started.\n t = 0.00s Start Test at 2018-01-29 14:55:29.908\n t = 0.17s Set Up\n t = 0.18s Open Company.exalam\n t = 0.28s Launch Company.exalam\n t = 6.39s Wait for Company.exalam to idle\n t = 9.13s Tear Down\nTest Case \'-[exalamUITests.exalamUITests testExample]\' passed (9.339 seconds).\nTest Suite \'exalamUITests\' passed at 2018-01-29 14:55:39.244.\n Executed 1 test, with 0 failures (0 unexpected) in 9.339 (9.342) seconds\nTest Suite \'exalamUITests.xctest\' passed at 2018-01-29 14:55:39.245.\n Executed 1 test, with 0 failures (0 unexpected) in 9.339 (9.346) seconds\nTest Suite \'Selected tests\' passed at 2018-01-29 14:55:39.249.\n Executed 1 test, with 0 failures (0 unexpected) in 9.339 (9.353) seconds\n\n\nTest session log:\n/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\nRun Code Online (Sandbox Code Playgroud)\n\nPodfile
\n\n# Uncomment the next line to define a global platform for your project\nplatform :ios, \'9.0\'\n\ntarget \'exalam\' do\n # Comment the next line if you\'re not using Swift and don\'t want to use dynamic frameworks\n use_frameworks!\n pod \'Alamofire\', \'~> 4.5\'\n\n # Pods for exalam\n\n target \'exalamTests\' do\n inherit! :search_paths\n # Pods for testing\n end\n\n target \'exalamUITests\' do\n inherit! :search_paths\n use_frameworks!\n pod \'Alamofire\', \'~> 4.5\'\n # Pods for testing\n end\n\nend\nRun Code Online (Sandbox Code Playgroud)\n\n这个问题很相似,但已经一年多了,我想我可以添加更多细节。
\n\n我是 Mac、swift、xcode 和 cocoapods 的新手,但我已经尝试过所有类似的 SO 相关答案,并尝试了许多不同的事情。\n(通常在 python 和 linux 下工作)
\n\n任何帮助将非常感激。
\nAlamofire 中的网络(与所有其他 iOS 网络库一样)是异步的。您的测试在服务器处理请求之前就已经结束了。这就是为什么你的responseJSON方法的闭包永远不会被触发的原因。您必须使用期望才能使其工作: https ://developer.apple.com/documentation/xctest/asynchronous_tests_and_expectations/testing_asynchronous_operations_with_expectations
不过我很好奇,为什么 UI 测试中需要联网?
| 归档时间: |
|
| 查看次数: |
1223 次 |
| 最近记录: |