# How to Use Matchers with Logical Operators (And, Or, Not) in GoogleTest

> Master GoogleTest matchers with logical operators. Learn to use AllOf for AND, AnyOf for OR, and Not for negation to build powerful, complex assertions. Enhance your unit tests today.

- Repository: [Google/googletest](https://github.com/google/googletest)
- Tags: how-to-guide
- Published: 2026-08-30

---

**Use `AllOf` for AND, `AnyOf` for OR, and `Not` for negation to compose complex assertions from simple matchers in gMock.**

The google/googletest repository provides a flexible matcher composition system through its gMock library. Learning how to use matchers with logical operators in GoogleTest enables you to validate complex conditions without implementing custom matcher classes for every scenario.

## Understanding Logical Matchers in gMock

GoogleMock implements three fundamental logical operators as variadic matcher wrappers in [`googlemock/include/gmock/gmock-matchers.h`](https://github.com/google/googletest/blob/main/googlemock/include/gmock/gmock-matchers.h). These combine existing matchers using boolean logic:

- **`AllOf`** – Succeeds only when all supplied matchers succeed (logical AND).
- **`AnyOf`** – Succeeds when at least one supplied matcher succeeds (logical OR).
- **`Not`** – Succeeds when the inner matcher fails (logical NOT).

According to the google/googletest source code, these are thin wrappers around the core `VariadicMatcher` and `MatcherInterface` infrastructure. Each operates on any type `T` that satisfies the `Matcher<T>` concept and integrates seamlessly with the `EXPECT_THAT` macro.

## Composing AND Conditions with AllOf

The `AllOf` matcher implements logical conjunction. As defined at line 1333 of [`gmock-matchers.h`](https://github.com/google/googletest/blob/main/gmock-matchers.h), it constructs a `vector<Matcher<T>>` from its arguments and validates that every matcher in the collection matches the input value.

```cpp
#include <gmock/gmock.h>
using ::testing::AllOf;
using ::testing::Gt;
using ::testing::Lt;

TEST(LogicalAndDemo, RangeValidation) {
  // Passes because 5 > 2 AND 5 < 10
  EXPECT_THAT(5, AllOf(Gt(2), Lt(10)));
  
  // Fails because 5 < 4 is false
  EXPECT_THAT(5, AllOf(Gt(2), Lt(4)));
}

```

You can pass any number of matchers to `AllOf`, making it ideal for validating that a value satisfies multiple constraints simultaneously.

## Combining OR Conditions with AnyOf

The `AnyOf` matcher provides logical disjunction. Implemented at line 1469 of [`gmock-matchers.h`](https://github.com/google/googletest/blob/main/gmock-matchers.h), it returns success on the first inner matcher that matches, short-circuiting evaluation of subsequent matchers.

```cpp
#include <gmock/gmock.h>
using ::testing::AnyOf;
using ::testing::Gt;
using ::testing::Lt;

TEST(LogicalOrDemo, AlternativeValidation) {
  // Passes because 5 > 2 (first condition true)
  EXPECT_THAT(5, AnyOf(Gt(2), Lt(3)));
  
  // Fails because neither 5 > 10 nor 5 < 0
  EXPECT_THAT(5, AnyOf(Gt(10), Lt(0)));
}

```

This is useful when a value can satisfy any one of several valid conditions.

## Negating Matchers with Not

The `Not` matcher inverts the result of its inner matcher. Found at line 1289 of [`gmock-matchers.h`](https://github.com/google/googletest/blob/main/gmock-matchers.h), it simply returns the logical negation of the wrapped matcher's evaluation.

```cpp
#include <gmock/gmock.h>
using ::testing::Not;
using ::testing::Lt;
using ::testing::Eq;

TEST(LogicalNotDemo, Negation) {
  // Passes because 5 is NOT less than 3
  EXPECT_THAT(5, Not(Lt(3)));
  
  // Fails because 5 IS equal to 5
  EXPECT_THAT(5, Not(Eq(5)));
}

```

## Nesting Logical Matchers

Because `AllOf`, `AnyOf`, and `Not` return valid matchers themselves, you can nest them to express complex boolean logic. This composition happens entirely at the matcher level without requiring temporary variables or custom classes.

```cpp
#include <gmock/gmock.h>
using ::testing::AllOf;
using ::testing::AnyOf;
using ::testing::Not;
using ::testing::Gt;
using ::testing::Lt;

TEST(CombinedLogicalDemo, ComplexValidation) {
  // Validates: NOT (>10 OR <0) AND >2 AND <8
  // Effectively checks: value in [0,10] AND (2,8)
  EXPECT_THAT(5, AllOf(
    Not(AnyOf(Gt(10), Lt(0))),
    Gt(2),
    Lt(8)
  ));
}

```

You can also combine logical operators with container matchers like `Contains`:

```cpp
#include <gmock/gmock.h>
#include <vector>
using ::testing::Contains;
using ::testing::Not;

TEST(ContainerLogicalDemo, ElementExclusion) {
  std::vector<int> v = {1, 2, 3};
  
  // Passes because vector does not contain 4
  EXPECT_THAT(v, Not(Contains(4)));
  
  // Fails because vector contains 2
  EXPECT_THAT(v, Not(Contains(2)));
}

```

## Summary

- **Use `AllOf`** (line 1333 in [`gmock-matchers.h`](https://github.com/google/googletest/blob/main/gmock-matchers.h)) to require that all matchers succeed (AND logic).
- **Use `AnyOf`** (line 1469 in [`gmock-matchers.h`](https://github.com/google/googletest/blob/main/gmock-matchers.h)) to allow any matcher to succeed (OR logic).
- **Use `Not`** (line 1289 in [`gmock-matchers.h`](https://github.com/google/googletest/blob/main/gmock-matchers.h)) to invert a matcher's result.
- **Nest freely**: These matchers compose with each other and any other gMock matchers via the `VariadicMatcher` infrastructure.
- **Include path**: All definitions reside in [`googlemock/include/gmock/gmock-matchers.h`](https://github.com/google/googletest/blob/main/googlemock/include/gmock/gmock-matchers.h).

## Frequently Asked Questions

### Where are logical matchers defined in the GoogleTest source code?

The `AllOf`, `AnyOf`, and `Not` matchers are defined in [`googlemock/include/gmock/gmock-matchers.h`](https://github.com/google/googletest/blob/main/googlemock/include/gmock/gmock-matchers.h). Specifically, `Not` appears at line 1289, `AllOf` at line 1333, and `AnyOf` at line 1469. Additional unit tests verifying their linkage exist in [`googlemock/test/gmock_link_test.h`](https://github.com/google/googletest/blob/main/googlemock/test/gmock_link_test.h).

### Can I nest multiple logical matchers in GoogleTest?

Yes. Because `AllOf`, `AnyOf`, and `Not` return `Matcher<T>` objects, they can be passed as arguments to other logical matchers. This allows you to build arbitrary boolean expressions without writing custom matcher classes, as shown when combining `Not(AnyOf(...))` inside `AllOf(...)`.

### What is the difference between AllOf and AnyOf in GoogleTest matchers?

`AllOf` requires every inner matcher to succeed for the assertion to pass, implementing logical AND. `AnyOf` succeeds if at least one inner matcher succeeds, implementing logical OR. According to the source code, `AnyOf` also short-circuits evaluation, returning immediately upon the first successful match.

### How do I check that a container does not contain a specific element?

Use the `Not` matcher to wrap the `Contains` matcher: `EXPECT_THAT(container, Not(Contains(value)))`. This pattern works with any STL-compatible container and is documented in the gMock cook book alongside other logical composition patterns.