mokacoding

unit and acceptance testing, automation, productivity

Posts tagged "xctest"

XCTNSPredicateExpectation is slow, and what to do about it

Each XCTNSPredicateExpectation requires a timeout of at least 1.1 seconds. That's will unnecessarily slow down your test suite. You can use Nimble's toEventually instead and make your tests as fast as possible.

When to test a @Published property using sink

Swift @Published properties come with an associated Combine Publisher that emits values over time. This free XCTest tutorial explains when to write a unit test that accesses the property directly and when it's instead necessary to subscribe to it using the sink operator.

Test-Driven Development in Swift is now available

My book, Test-Driven Development in Swift, is now available in online bookstores everywhere. You'll learn Test-Driven Development writing a real-world SwiftUI application, including events-flow management with Combine, networking, local storage, and third-party libraries.

WWDC21: What's New in Testing

A roundup of the testing-related new features announced at WWDC 2021. Including Xcode Cloud, how to test code using async/await, the new XCTExpectFailure and addTearDownBlock APIs, and the new Test Repetition configuration in Test Plans.

How to test Swift async/await code with XCTest

Swift 5.5 and Xcode 13 introduce the async/await pattern for concurrent code. This tutorial post shows how to write unit tests for asynchronous code in Swift using the XCTest framework.

How to write better tests for Swift date comparisons

Testing Swift date comparison code with XCTest can result in indeterministic tests because of the passage of time. To make tests robust and deterministic, decouple them from the system clock by injecting the reference date.

Unit Testing Combine Publisher Cheatsheet

Snippets to test the behavior of Combine Publishers in XCTest ready to copy and paste into Xcode

Write better Swift unit tests with custom XCTest assertions

The XCTest Swift testing framework has a limited offer of assertions. There's only so much you can do with XCTAssertTrue and XCTAssertEqual. This XCTest tutorial shows how to create custom assertions to make your unit tests and UI tests shorter and clearer.

How to write unit test assertions for Swift Result values

Result is one of the most useful types in the Swift language. Learn how to write better unit tests using Result in this XCTest tutorial.

How to decouple unit tests from values that change frequently

When the output value of a function changes often but the logic to pick it doesn't, adding a separation layer will make unit tests easier to maintain.

How to remove duplication from Swift tests with helper functions

Some code ends up requiring a lot of duplication to be tested. You can remove it by using helper functions encapsulating the shared assertion logic.

XCTest closure based expectations

Testing async code is not simple, but XCTest provides us with all the required tool. This post shows how to wait for an expectation to be fulfilled based on a Swift closure.

Testing Delegates in Swift with XCTest

In this second post of the Practical Testing in Swift we a look at strategies to test how objects call their delegate methods or set property on them.

Testing callbacks in Swift with XCTest

Better tests with Specta

Writing unit tests for our iOS and OS X projects not only is important, but should be always part of the development cycle. As such the way we write the tests is as important, and having the option to write tests that easily explain their purpose can drastically increase the quality of the suite. Specta and Expecta are two libraries that provide a different way to writing tests than XCTest, let's see what we can gain by using such approach.