Member-only story

Assertion messages in XCTest

Alonso Del Arte
4 min readDec 16, 2023

--

Photo by Scott Van Hoy on Unsplash

You can get a lot of testing work done for your Swift programming in Xcode just with XCTest’s Assert Equals. With Assert Equals, you can count on Xcode to report what was expected and was actually received. For example, in a blackjack or poker program, if this test fails,

    func testShorthand() {
for suit in Suit.allCases {
for rank in Rank.allCases {
let card: PlayingCard = PlayingCard(rank: rank, suit: suit)
let expected = "\(rank.symbol())\(suit.rawValue)"
let actual = card.shorthand()
XCTAssertEqual(expected, actual)
}
}
}

Xcode will let us know exactly why the test failed.

In this example, the test is expecting the shorthand() function to give “10♠” for the Ten of Spades, “10♣” for the Ten of Clubs, etc., but the function is returning “SORRY” for all of them. It’s quite clear what we need to do to get this test to pass.

Likewise the numeric comparison assertions are reasonably informative. For example, a test asserts that a particular number is at most 30 (I believe that’s the maximum bust value in blackjack, such as if a player is…

--

--

Alonso Del Arte
Alonso Del Arte

Written by Alonso Del Arte

is a Java and Scala developer from Detroit, Michigan. AWS Cloud Practitioner Foundational certified

No responses yet