Home

Awesome

ochamcrest

OCHamcrest

Build Status Coverage Status Swift Package Index Platform Compatibility Carthage compatible CocoaPods Version Mastodon Follow

OCHamcrest is an Objective-C module providing:

Matchers can be combined to create flexible expressions of intent in tests. They can also be used for other purposes, such as user input validation.

<!-- toc -->

Contents

My First OCHamcrest Test

We'll start by writing a very simple Xcode unit test, but instead of using XCTest's XCTAssertEqualObjects function, we'll use OCHamcrest's assertThat construct and a predefined matcher:

@import OCHamcrest;
@import XCTest;

@interface BiscuitTest : XCTestCase
@end

@implementation BiscuitTest

- (void)testEquals
{
    Biscuit* theBiscuit = [[Biscuit alloc] initWithName:@"Ginger"];
    Biscuit* myBiscuit = [[Biscuit alloc] initWithName:@"Ginger"];
    assertThat(theBiscuit, equalTo(myBiscuit));
}

@end

The assertThat function is a stylized sentence for making a test assertion. In this example, the subject of the assertion is the object theBiscuit, which is the first method parameter. The second method parameter is a matcher for Biscuit objects, here a matcher that checks one object is equal to another using the -isEqual: method. The test passes since the Biscuit class defines an -isEqual: method.

OCHamcrest's functions are actually declared with an "HC_" package prefix (such as HC_assertThat and HC_equalTo) to avoid name clashes. To make test writing faster and test code more legible, optional short syntax is provided by default. For example, instead of writing HC_assertThat, simply write assertThat.

Predefined Matchers

OCHamcrest comes with a library of useful matchers:

Object

Number

Text

Logical

Collection

Decorator

The arguments for many of these matchers accept not just a matching value, but another matcher, so matchers can be composed for greater flexibility. For example, only_contains(endsWith(@".")) will match any collection where every item is a string ending with period.

Syntactic Sugar

OCHamcrest strives to make your tests as readable as possible. For example, the is matcher is a wrapper that doesn't add any extra behavior to the underlying matcher. The following assertions are all equivalent:

assertThat(theBiscuit, equalTo(myBiscuit));
assertThat(theBiscuit, is(equalTo(myBiscuit)));
assertThat(theBiscuit, is(myBiscuit));

The last form is allowed since is wraps non-matcher arguments with equalTo. Other matchers that take matchers as arguments provide similar shortcuts, wrapping non-matcher arguments in equalTo.

Common Questions

How Can I Assert on an Asynchronous Call?

assertWithTimeout will keep evaluating an expression until the matcher is satisfied or a timeout is reached. For example,

assertWithTimeout(5, thatEventually(self.someString), is(@"expected"));

This repeatedly checks for this string to evaluate to "expected" before timing out after 5 seconds. thatEventually is a convenience macro to create a block.

Can I Add Custom Matchers?

OCHamcrest comes bundled with lots of useful matchers, but you'll probably find that you need to create your own from time to time to fit your testing needs. See the "Writing Custom Matchers" guide for more information.

What About Swift?

Try the native Swift implementation of Hamcrest.

How Do I Add OCHamcrest to My Project?

The Examples folder shows projects ready to use OCHamcrest via Swift Package Manager, CocoaPods, or through the prebuilt framework.

Swift Package Manager

Include an OCHamcrest package in your Package.swift manifest's array of dependencies:

<!-- snippet: swiftpm-declare-dependencies -->

<a id='snippet-swiftpm-declare-dependencies'></a>

dependencies: [
    .package(
        url: "https://github.com/hamcrest/OCHamcrest",
        .upToNextMajor(from: "9.0.0")
    ),
],

<sup><a href='/Examples/MacExample-SwiftPackageManager/Package.swift#L13-L20' title='Snippet source file'>snippet source</a> | <a href='#snippet-swiftpm-declare-dependencies' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

Then add OCHamcrest to the dependencies of your .testTarget:

<!-- snippet: swiftpm-use-dependencies -->

<a id='snippet-swiftpm-use-dependencies'></a>

.testTarget(
    name: "ExampleTests",
    dependencies: [
        "Example",
        "OCHamcrest",
    ]
),

<sup><a href='/Examples/MacExample-SwiftPackageManager/Package.swift#L27-L35' title='Snippet source file'>snippet source</a> | <a href='#snippet-swiftpm-use-dependencies' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet -->

CocoaPods

If you want to add OCHamcrest using Cocoapods then add the following dependency to your Podfile. Most people will want OCHamcrest in their test targets, and not include any pods from their main targets:

target 'MyTests' do
  inherit! :search_paths
  use_frameworks!
  pod 'OCHamcrest', '~> 9.0'
end

Carthage

Add the following to your Cartfile:

github "hamcrest/OCHamcrest" ~> 9.0

Then drag the the built framework from the appropriate Carthage/Build directory into your project, but with "Copy items into destination group's folder" disabled.

Prebuilt Framework

A prebuilt binary is available on GitHub. The binary is packaged as OCHamcrest.xcframework, containing these architectures:

Drag the XCFramework into your project.

Build Your Own

If you want to build OCHamcrest yourself, clone the repo, then

$ cd Source
$ ./MakeDistribution.sh

Author

Jon Reid is the author of iOS Unit Testing by Example. His website is Quality Coding.