What is OCHamcrest?

OCHamcrest is:

Matchers are useful for a variety of purposes, such as UI validation. But they're most commonly used for writing unit tests that are expressive and flexible.

OCHamcrest is used for both Mac and iOS development, and is compatible with:

How do I add OCHamcrest to my project?


If you want to build OCHamcrest yourself, cd to the Source folder, then

$ ./MakeDistribution.sh

Or just use the pre-built release available in Downloads.

Mac Project Setup:

Add OCHamcrest.framework to your project.

Add a Copy Files build phase to copy OCHamcrest.framework to your Products Directory. For unit test bundles, make sure this Copy Files phase comes before the Run Script phase that executes tests.


#import <OCHamcrest/OCHamcrest.h>

Note: If your Console shows

otest[57510:203] *** NSTask: Task create for path '...' failed: 22, "Invalid argument".  Terminating temporary process.

double-check your Copy Files phase.

iOS Project Setup:

Add OCHamcrestIOS.framework to your project.


#import <OCHamcrestIOS/OCHamcrestIOS.h>

My first OCHamcrest test

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

#import <SenTestingKit/SenTestingKit.h>

#import <OCHamcrest/OCHamcrest.h>

@interface BiscuitTest : SenTestCase

@implementation BiscuitTest

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


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, shorthand macros are provided if HC_SHORTHAND is defined before including the OCHamcrest header. For example, instead of writing HC_assertThat, simply write assertThat.

Predefined matchers

OCHamcrest comes with a library of useful matchers:

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));

Writing custom matchers

A key feature of OCHamcrest is its extensibility. See Writing custom matchers for an example of how to write your own matchers.

More resources