[Go] Assert that a slice contains a value with Testify

Created on November 29, 2026

You can use Contains from Testify's assert package to check whether a slice contains a value.

package main

import (
    "testing"

    "github.com/stretchr/testify/assert"
)

func TestContains(t *testing.T) {
    check := assert.New(t)

    fruits := []string{"apple", "banana", "cherry"}
    check.Contains(fruits, "banana")
}

assert.New(t) binds the assertions to the current test. The assertion passes because fruits contains "banana".

Reference

Go