Search code examples
swiftxcodeunit-testingswift-package-managerxctestcase

Write unit test in swift package manager for API call


I am completely new to writing unit test cases. This is my first one to write and I am confused. I am creating one swift package manager where I have written one method which will accept

  1. URL

  2. HTTPMethod

  3. Parameter

and I am using Alamofire as package dependancy over there, which will call API from passed URL and then response will be catch.

Following is the code, Framework.swift file

public let APIFramework = APICore.default
let version = "1.0.0"

FrameworkClass.swift file code

import Foundation
import Alamofire

public protocol APICoreDelegate: AnyObject {
    func didReceiveData(data: Data)
}

open class APICore {
    public static let `default` = APICore()
    public var delegate: APICoreDelegate?
    open func processApiCall( url : String, method: HTTPMethod, parameters: Dictionary<String, Any>)
    {
        AF.request(url, method: method, parameters: parameters).responseJSON { (response) in
        switch response.result {
            case .success:
                guard let jsonData = response.value else {
                    return
                }
                self.delegate?.didReceiveData(data: jsonData as! Data)
            case .failure: break
            }
        }
    }
}

Now I have to write unit test case for this method in CDHFrameworkTests class.

import XCTest
@testable import CDHFramework

final class CDHFrameworkTests: XCTestCase {
    override func setUp() {

    }
    func testExample() {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct
        // results.
    }

    static var allTests = [
        ("testExample", testExample),
    ]
}

Can any one help me with one test case at least, so that I can study that and try to write the rest of them. I have read a lot, but all the material is quite confusing to me. Any help to write test cases for this will be appreciated.

Thanks in advance.


Solution

  • It's really good you're getting to know unit tests (they'll make your life SOOOO much easier, trust me).

    A few things I must mention about your snippet of code above, since you're calling Alamofire directly in your API core class (AF.request) you won't be able to mock your network and hence not being able to perform a unit test per se. Sure, you could either:

    • Use an HTTP proxy for your project to interact with and hence make your test reliable but that would introduce a whole lot of boilerplate and set up.
    • Interact directly with a test/mock endpoint but this also comes with a caveat since you'd be relying on your network connection and having the need to introduce a bunch of waiting time in order to be able to sync your tests with the response times. Not recommendable.

    You should check these resources out:

    I need to improve the documentation on my library but the network layer is fully covered in tests + it's SPM (after all that was your original question).

    I wish you all the best of lucks with automated testing in general, cheers!

    PS: You should consider dismiss network layer dependencies for simple tasks. Libraries such as Alamofire and Moya are great choices if you need to do some really heavy lifting on the network side, otherwise they are unnecessary boilerplate. Check out these other great resources if you need a starting point: