Skip to content
an equality assertion for swift tests similar to XCTAssertEqual but using reflection rather than an equals function.
Swift Shell Ruby Objective-C
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


CI Status Version License Platform

MCAssertReflectiveEqual is a function that can be used to write swift test assertions. It works very similarly to XCTest's XCTAssertEqual but doesn't require Equatable items and uses reflection instead. Therefore you don't need to write neither equals functions that your production code does not require, nor assert multiple individual fields making tests a chore.

MCAssertReflectiveEquals works on primitives, structs, classes and enums. It gives you a nice error message when items do not match. It deeply compares items and handles recursive loops (A -> B -> A, where A & B are objects and -> is a references). It just makes tests easier.

The development of MCAssertReflectiveEqual is described here.


import XCTest
import MCAssertReflectiveEqual

private class ClassWithVal {
    var val: Int
    init(_ val: Int) {
        self.val = val

class ClassWithValTest : XCTestCase {

    func testAreEqual() {
        let expected = ClassWithVal(1)
        let actual = ClassWithVal(1)
        MCAssertReflectiveEqual(expected, actual) 
        MCAssertReflectiveEqual([expected], [actual])
        MCAssertReflectiveEqual(expected, ClassWithVal(5)) //fails


Custom Matchers

Sometimes simple reflective matching is not good enough. Imagine a complex data structure that contains a geographical coordinate, a CLLocation. We may not be interested that our expected value is identical to the value produced by the system under test, only that they are close enough. Here's how we go about that:

class FavouriteLocation {
    let user: String
    let location: CLLocation
    init(user: String, location: CLLocation) {
        self.user = user
        self.location = location
let camdenTown = CLLocation(latitude: 51.5390, longitude: 0.1426)
let closeToCamdenTown = CLLocation(latitude: 51.5391, longitude: 0.1427)
let matcher = matcherFor(CLLocation.self, { (expected, actual) in
                    return expected.distance(from: actual) < 100 //close enough
MCAssertReflectiveEqual(FavouriteLocation(user: "bob", location: closeToCamdenTown), 
                        FavouriteLocation(user: "bob", location: camdenTown), 
                        matchers: [matcher])        

Provided matchers

For convenience, a matcher for doubles with defined accuracy is provided. Instantiate it with

let accuracy = 0.001

let matcher = matchDoubles(withAccuracy: accuracy)

let expected = 0.01
let actual = 0.0101

MCAssertReflectiveEqual(expected, actual) //fails

MCAssertReflectiveEqual(expected, actual, matchers: [matcher]) //passes

More examples in MCAssertReflectiveEqualTest.swift


MCAssertReflectiveEqual is available through CocoaPods. To install it, simply add the following line to your Podfile. It really only makes sense to reference it in your test target.


pod "MCAssertReflectiveEqual"

It's implemented in a single file so if you do not want to use CocoaPods it should be easy to integrate with your project.


Stefanos Zachariadis, motocode ltd, first name at last name dot net,


MCAssertReflectiveEqual is available under the MIT license. See the LICENSE file for more info.

You can’t perform that action at this time.