DEV.co
Open-Source Testing · hamcrest

OCHamcrest

OCHamcrest is a mature Objective-C testing library that provides composable matcher objects for flexible assertions in unit tests. It reduces boilerplate compared to standard XCTest assertions and enables more readable, expressive test code.

Source: GitHub — github.com/hamcrest/OCHamcrest
713
GitHub stars
98
Forks
Objective-C
Primary language
BSD-2-Clause
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositoryhamcrest/OCHamcrest
Ownerhamcrest
Primary languageObjective-C
LicenseBSD-2-Clause — OSI-approved
Stars713
Forks98
Open issues0
Latest releasev9.1.1 (2025-02-24)
Last updated2026-06-18
Sourcehttps://github.com/hamcrest/OCHamcrest

What OCHamcrest is

OCHamcrest implements the Hamcrest pattern for Objective-C, offering a fluent DSL for building assertion expressions via matcher composition. Supports XCTest integration, async assertions via assertWithTimeout, and custom matcher extension. Distributed via CocoaPods, Carthage, and Swift Package Manager.

Quickstart

Get the OCHamcrest source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/hamcrest/OCHamcrest.gitcd OCHamcrest# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Readable XCTest-based assertions

Replace repetitive XCTAssertEqual/XCTAssertNotNil with fluent matcher chains (e.g., assertThat(obj, allOf(hasProperty(@"name", equalTo(@"Foo")), instanceOf([MyClass class])))). Especially valuable in assertion-heavy test suites.

Complex collection and object verification

Leverage built-in matchers for collections (containsInAnyOrder, everyItem, hasCountOf) and nested property matching (hasProperty, hasEntries) without writing custom assertion loops.

Async assertion patterns

Use assertWithTimeout(timeout, thatEventually(expression), matcher) for polling-based assertions on network calls, background tasks, or delayed state updates without manual retry logic.

Implementation considerations

  • Integrate via CocoaPods, Carthage, or SPM; examples provided for each. Ensure build target is testTarget, not main app target.
  • Learn the matcher DSL and common syntax shortcuts (is, anyOf, allOf, everyItem). Custom matchers require subclassing HCMatcher or using convenience macros; see wiki for patterns.
  • Async assertions require wrapping expressions in thatEventually() blocks; understand timeout behavior to avoid flaky tests.
  • OCHamcrest uses HC_ namespace prefix (e.g., HC_assertThat); optional short syntax (assertThat) is enabled by default but can collide with other frameworks.
  • Test coverage and CI/CD integration rely on standard Xcode/SPM toolchain; no special runner or configuration needed.

When to avoid it — and what to weigh

  • Swift-first codebases — OCHamcrest is Objective-C only. The README explicitly directs Swift users to the separate SwiftHamcrest project. Mixing Objective-C matchers into Swift test files is not recommended.
  • Minimal testing infrastructure — For projects with few assertions or simple equality checks, the overhead of learning matcher syntax and dependency management may outweigh readability gains. Plain XCTest may suffice.
  • Projects requiring matcher introspection or IDE autocomplete for custom matchers — Objective-C's runtime capabilities are limited compared to Swift/modern languages. Building and discovering custom matchers can be friction-heavy; validation occurs at test runtime.
  • Strict lock-in to stable, well-known third-party testing libraries — While active, OCHamcrest is niche compared to industry-standard testing frameworks in newer ecosystems. Migration cost and community size are considerations for long-term projects.

License & commercial use

BSD-2-Clause (Simplified BSD License) permits use, modification, and distribution in source and binary forms with preservation of copyright notice, license terms, and disclaimer. No patent grant; minimal restrictions on commercial use.

BSD-2-Clause is an OSI-approved permissive license. Commercial use (including closed-source products) is explicitly permitted, provided license text and copyright notice accompany distribution. No royalties or attribution beyond license compliance required. Suitable for proprietary internal testing infrastructure.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceActive
DocumentationStrong
License clarityClear
Deployment complexityLow
DEV.co fitGood
Assessment confidenceHigh
Security considerations

OCHamcrest is a testing library with no network, auth, or data persistence responsibilities. Security review should focus on: (1) Dependency supply chain (GitHub/CocoaPods integrity); (2) Use in sensitive test environments (matchers capture values; ensure logs/artifacts are protected); (3) Custom matchers or extensions that may inadvertently log sensitive state. No known CVEs reported.

Alternatives to consider

Native XCTest assertions (XCTAssertEqual, XCTAssertTrue, etc.)

Zero dependencies; built-in to Xcode. Trade-off: repetitive, less composable, less readable for complex conditions. Suitable for simple test suites.

SwiftHamcrest (Swift native)

If moving to or already in Swift, SwiftHamcrest provides equivalent matcher composition with modern Swift syntax. Requires rewrite of ObjC test code but better IDE support and language integration.

OCMock (Objective-C mocking framework)

Orthogonal but often paired with OCHamcrest. OCMock focuses on mocking/stubbing; OCHamcrest on assertions. Can be used together or separately depending on test isolation needs.

Software development agency

Build on OCHamcrest with DEV.co software developers

If you're maintaining a mature Objective-C codebase and want more readable, maintainable assertions without switching frameworks, OCHamcrest is a proven, low-risk addition. Start with a single test target and evaluate syntax fit.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

OCHamcrest FAQ

Can I use OCHamcrest in Swift tests?
Not directly. OCHamcrest is Objective-C only. Use SwiftHamcrest for native Swift tests, or use Objective-C bridging headers if testing Objective-C APIs from Swift and want matcher syntax.
How do I test asynchronous code?
Use assertWithTimeout(timeout, thatEventually(expression), matcher). This polls the expression until the matcher succeeds or timeout expires. Avoid hard sleeps; let the matcher drive polling.
Does OCHamcrest work with mocking libraries like OCMock?
Yes. OCHamcrest matchers are assertions; OCMock provides mocking/stubbing. Both can be used in the same test. No known conflicts.
Can I write custom matchers?
Yes. Subclass HCMatcher or use convenience macros defined in the wiki (Writing Custom Matchers guide). Custom matchers must implement -matches: and -describeTo: methods.

Custom software development services

Need help beyond evaluating OCHamcrest? DEV.co is a software development agency offering software development services and web development for teams of every size. Our software developers and web developers build custom software, web applications, APIs, and open-source testing integrations — and maintain them long-term.

Evaluate OCHamcrest for Your Objective-C Test Suite

If you're maintaining a mature Objective-C codebase and want more readable, maintainable assertions without switching frameworks, OCHamcrest is a proven, low-risk addition. Start with a single test target and evaluate syntax fit.