testify icon indicating copy to clipboard operation
testify copied to clipboard

`assert.Subset` doesn't fail tests

Open joshprzybyszewski-wf opened this issue 3 years ago • 2 comments

Story

We were writing some unit tests, and I was surprised that they passed. Here is a re-creation:

func TestSubsetSlices(t *testing.T) {
	a := []string{`a`}
	ab := []string{`a`, `b`}

	assert.Subset(t, ab, a)
	assert.NotSubset(t, a, ab)

	// The following asserts should fail this test
	assert.Subset(t, a, ab)
	assert.NotSubset(t, ab, a)
}

func TestSubsetMapsWithValues(t *testing.T) {
	a := map[string]int{
		`a`: 1,
	}
	ab := map[string]int{
		`a`: 1,
		`b`: 2,
	}

	assert.Subset(t, ab, a)
	assert.NotSubset(t, a, ab)

	// The following asserts should fail this test
	assert.Subset(t, a, ab)
	assert.NotSubset(t, ab, a)
}

func TestSubsetMapsWithEmptyStructs(t *testing.T) {
	a := map[string]struct{}{
		`a`: {},
	}
	ab := map[string]struct{}{
		`a`: {},
		`b`: {},
	}

	assert.Subset(t, ab, a)
	assert.NotSubset(t, a, ab)

	// The following asserts should fail this test
	assert.Subset(t, a, ab)
	assert.NotSubset(t, ab, a)
}

Problem

I think the problem is here:

https://github.com/stretchr/testify/blob/181cea6eab8b2de7071383eca4be32a424db38dd/assert/assertions.go#L821-L825

https://github.com/stretchr/testify/blob/181cea6eab8b2de7071383eca4be32a424db38dd/assert/assertions.go#L882-L886

It returns a false value, but it doesn't fail the *testing.T.

joshprzybyszewski-wf avatar Sep 22 '22 18:09 joshprzybyszewski-wf

Hey Josh, how's stuff?! This looks pretty straightforward, if you can put up a PR I'll merge it.

glesica avatar Sep 23 '22 23:09 glesica

Hey, @glesica, mind giving #1290 a glance to see if it is decent enough to resolve this?

antichris avatar Nov 28 '22 04:11 antichris

Solved by https://github.com/stretchr/testify/pull/1261

st3penta avatar Feb 16 '24 23:02 st3penta

Dup of #1261

brackendawson avatar Feb 17 '24 17:02 brackendawson