【问题标题】:Unable to use Alamofire in an xcode UI Test with Swift无法在使用 Swift 的 xcode UI 测试中使用 Alamofire
【发布时间】:2018-07-07 18:21:29
【问题描述】:

我遇到的问题是让 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,标头{ “访问控制允许凭据”=( 真的 等等……

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下工作)

任何帮助将不胜感激。

【问题讨论】:

    标签: swift xcode cocoapods alamofire xcode-ui-testing


    【解决方案1】:

    Alamofire 中的网络(与所有其他 iOS 网络库一样)是异步的。您的测试甚至在服务器处理请求之前就结束了。这就是为什么你的 responseJSON 方法的闭包永远不会被触发。 你必须使用期望来让它发挥作用: https://developer.apple.com/documentation/xctest/asynchronous_tests_and_expectations/testing_asynchronous_operations_with_expectations

    不过我很好奇,为什么你需要在 UI 测试中建立网络?

    【讨论】:

    • 我会试试这个并报告,谢谢。我正在为应用程序的用户帐户创建构建 UI 测试。因此,我计划删除在拆卸中创建的用户帐户,以便可以使用相同的用户帐户详细信息再次运行测试。
    • 根据您关于所有 iOS 网络异步的提示以及此 SO 帖子:stackoverflow.com/questions/29852431,我能够在 UI 测试方面获得 Alamofire 响应。谢谢
    猜你喜欢
    • 2018-08-02
    • 2015-11-16
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 2014-08-31
    • 1970-01-01
    相关资源
    最近更新 更多