Testing completion handler APIs with Swift Testing – Donny Wals

Testing completion handler APIs with Swift Testing – Donny Wals


Revealed on: October 16, 2024

The Swift testing framework is an extremely great tool that permits us to write down extra expressive assessments with handy and fashionable APIs.

That is my first submit about Swift Testing, and I’m primarily writing it as a result of I wished to write down about one thing that I encountered not too way back once I tried to make use of Swift testing on a code base the place I had each async code in addition to older completion handler primarily based code.

The async code was very straightforward to check resulting from how Swift Testing is designed, and I shall be writing extra about that sooner or later.

The completion handler base code was somewhat bit tougher to check, primarily as a result of I used to be changing my code from XCTest with check expectations to regardless of the equal can be in Swift testing.

Understanding the issue

Once I began studying Swift testing, I truly checked out Apple’s migration doc and I discovered that there’s an one thing that’s purported to be analogous to the expectation object, which is the affirmation object. The examples from Apple have one little caveat in there.

The Swift Testing instance appears somewhat bit like this:

// After
struct FoodTruckTests {
  @Take a look at func truckEvents() async {
    await affirmation("…") { soldFood in
      FoodTruck.shared.eventHandler = { occasion in
        if case .soldFood = occasion {
          soldFood()
        }
      }
      await Buyer().purchase(.soup)
    }
    ...
  }
  ...
}

Now, as you’ll be able to see within the code above, the instance that Apple has exhibits that we now have a perform and a name to the affirmation perform in there, which is how we’re supposed to check our async code.

They name their previous completion handler primarily based API and within the occasion handler closure they name their affirmation closure (known as soldFood within the instance).

After calling setting the occasion handler they await Buyer().purchase(.soup).

And that is actually the place Apple desires us to pay shut consideration as a result of within the migration doc, they point out that we wish to catch an occasion that occurs throughout some asynchronous course of.

The await that they’ve as the ultimate line of that affirmation closure is de facto the important thing a part of how we must be utilizing affirmation.

Once I tried emigrate my completion handler primarily based code that I examined with XCTestExpectation, I did not have something to await. My authentic testing code regarded somewhat bit like this:

func test_valueChangedClosure() {
  let count on = expectation(description: "Anticipated synchronizer to finish")

  let synchronizer = Synchronizer()
  synchronizer.onComplete = {
    XCTAssert(synchronizer.newsItems.rely == 2)
    count on.fulfill()
  }

  synchronizer.synchronize()
  waitForExpectations(timeout: 1)
}

Based mostly on the migration information and skimming the examples I although that the next code can be the Swift Testing equal:

@Take a look at func valueChangedClosure() async {    
  await affirmation("Synchronizer completes") { @MainActor verify in
    synchronizer.onComplete = {
      #count on(synchronizer.newsItems.rely == 2)
      verify()
    }

    synchronizer.synchronize()
  }
}

My code ended up wanting fairly much like Apple’s code however the important thing distinction is the final line in my affirmation. I’m not awaiting something.

The consequence when working that is at all times a failing check. The check will not be ready for me to name the verify closure in any respect. That await proper on the finish in Apple’s pattern code is just about wanted for this API to be usable as a substitute of your expectations.

What Apple says within the migration information once you fastidiously learn is definitely that the entire confirmations should be known as earlier than your closure returns:

Confirmations perform equally to the expectations API of XCTest,
nonetheless, they don’t block or droop the caller whereas ready for a
situation to be fulfilled. As an alternative, the requirement is predicted to be confirmed (the equal of fulfilling an expectation) earlier than affirmation() returns

So every time that affirmation closure returns, Swift Testing expects that we now have confirmed all of our confirmations. In a standard completion handler-based setup, this may not be the case since you’re not awaiting something as a result of you do not have something to await.

This was fairly difficult to determine.

Write a check for completion handler code

The answer right here is to not use a affirmation object right here as a result of what I assumed would occur, is that the affirmation would act somewhat bit like a continuation within the sense that the Swift check would look forward to me to name that affirmation.

This isn’t the case.

So what I’ve actually discovered is that one of the best ways to check your completion handler-based APIs is to make use of continuations.

You need to use a continuation to wrap your name to the completion handler-based API after which within the completion handler, do all your assertions and resume your continuation. This can then resume your check and it’ll full your check.

Right here’s what that appears like for instance:

@Take a look at func valueChangedClosure() async {
    await withCheckedContinuation { continuation in
        synchronizer.onComplete = {
            #count on(synchronizer.newsItems.rely == 2)
            continuation.resume()
        }

        synchronizer.synchronize()
    }
}

This strategy works very effectively for what I wanted, and it permits me to droop the check whereas my callback primarily based code is working.

It is the best strategy I may give you, which is often a superb signal. However if in case you have some other approaches that you just favor, I’d love to listen to about them, particularly when it pertains to testing completion handler APIs. I do know this isn’t a full-on substitute for all the pieces that we will do with expectations, however for the completion handler case, I feel it is a fairly good substitute.

When to not use continuations for testing completion handler code

The strategy of testing outlined above assumes that our code is considerably freed from sure bugs the place the completion handler isn’t known as. Our continuation does not do something to forestall our check from hanging ceaselessly which may (let’s be trustworthy, will) be a problem for sure situations.

There are code snippets on the market that can get you the power to deal with timeouts, just like the one discovered on this gist that was shared with me by Alejandro Ramirez.

I have not accomplished intensive testing with this snippet but however a few preliminary assessments look good to me.

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.