Member-only story
Assertion messages in XCTest
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…