Skip to content

Commit

Permalink
test: fix test flakiness due to reusing "for" loop variable in sub-te…
Browse files Browse the repository at this point in the history
…sts (#6)

This change fixes test flakiness caused to due "for" loops reusing the
same loop variable in parallel tests.

Refs: #6
  • Loading branch information
arikkfir committed May 4, 2024
1 parent bcccb4b commit a6741d1
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions asserter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestAssertionOrFail(t *testing.T) {
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
defer VerifyTestOutcome(t, tc.expectedOutcome, tc.expectedOutcomePattern)
Expand Down Expand Up @@ -200,6 +201,7 @@ func TestAssertionFor(t *testing.T) {
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
defer VerifyTestOutcome(t, tc.expectedOutcome, tc.expectedOutcomePattern)
Expand Down Expand Up @@ -320,6 +322,7 @@ func TestAssertionWithin(t *testing.T) {
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
defer VerifyTestOutcome(t, tc.expectedOutcome, tc.expectedOutcomePattern)
Expand Down
3 changes: 3 additions & 0 deletions matchers_be_between_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ func TestBeBetween(t *testing.T) {
},
}
for kind, kindTestCases := range testCases {
kind := kind
kindTestCases := kindTestCases
t.Run(kind.String(), func(t *testing.T) {
for name, tc := range kindTestCases {
tc := tc
t.Run(name, func(t *testing.T) {
if tc.expectFailurePattern != nil {
defer VerifyTestOutcome(t, ExpectFailure, *tc.expectFailurePattern)
Expand Down
1 change: 1 addition & 0 deletions matchers_be_empty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestBeEmpty(t *testing.T) {
"Non-empty string fails": {actual: "abc", expectedOutcome: ExpectFailure, expectFailurePattern: regexp.QuoteMeta(`Expected 'abc' to be empty, but it is not (has a length of 3)`)},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
defer VerifyTestOutcome(t, tc.expectedOutcome, tc.expectFailurePattern)
Expand Down
3 changes: 3 additions & 0 deletions matchers_be_greater_than_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ func TestBeGreaterThan(t *testing.T) {
},
}
for kind, kindTestCases := range testCases {
kind := kind
kindTestCases := kindTestCases
t.Run(kind.String(), func(t *testing.T) {
t.Parallel()
for name, tc := range kindTestCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if tc.expectFailurePattern != nil {
Expand Down
3 changes: 3 additions & 0 deletions matchers_be_less_than_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ func TestBeLessThan(t *testing.T) {
},
}
for kind, kindTestCases := range testCases {
kind := kind
kindTestCases := kindTestCases
t.Run(kind.String(), func(t *testing.T) {
t.Parallel()
for name, tc := range kindTestCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
if tc.expectFailurePattern != nil {
Expand Down
1 change: 1 addition & 0 deletions matchers_equal_to_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func TestEqualTo(t *testing.T) {
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
defer VerifyTestOutcome(t, tc.expectedOutcome, tc.expectedFailurePattern)
Expand Down
1 change: 1 addition & 0 deletions matchers_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestFail(t *testing.T) {
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
defer VerifyTestOutcome(t, tc.expectedOutcome, tc.expectFailurePattern)
Expand Down
1 change: 1 addition & 0 deletions matchers_not_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestNot(t *testing.T) {
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
defer VerifyTestOutcome(t, tc.expectedOutcome, tc.expectFailurePattern)
Expand Down
1 change: 1 addition & 0 deletions matchers_say_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestSay(t *testing.T) {
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
defer VerifyTestOutcome(t, tc.expectedOutcome, tc.expectFailurePattern)
Expand Down
1 change: 1 addition & 0 deletions matchers_succeed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestSucceed(t *testing.T) {
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
defer VerifyTestOutcome(t, tc.expectedOutcome, tc.expectFailurePattern)
Expand Down
4 changes: 4 additions & 0 deletions value_extractor_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestValueExtractor(t *testing.T) {
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
mt := NewMockT(t)
Expand Down Expand Up @@ -172,6 +173,7 @@ func TestNewChannelExtractor(t *testing.T) {
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
mt := NewMockT(t)
Expand Down Expand Up @@ -235,6 +237,7 @@ func TestNewPointerExtractor(t *testing.T) {
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
mt := NewMockT(t)
Expand Down Expand Up @@ -382,6 +385,7 @@ func TestNewFuncExtractor(t *testing.T) {
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
mt := NewMockT(t)
Expand Down
1 change: 1 addition & 0 deletions value_extractor_numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestNumericValueExtractor(t *testing.T) {
"uint64": {actual: uint64(1), expectedOutcome: ExpectSuccess, expected: uint64(1)},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
defer VerifyTestOutcome(t, tc.expectedOutcome, tc.expectedOutcomePattern)
Expand Down

0 comments on commit a6741d1

Please sign in to comment.