Skip to content

Conversation

@SVilgelm
Copy link
Contributor

In case of using t.Parallel() inside a for loop, the loop variables must be copied, otherwise only the last test will be executed.

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()

changed to

	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()

also the AuthServer must be created within the test, otherwise some tests fail.

t.Log(test.Name) can be used to verify the issue:

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()
			t.Log("Test name:", test.name)
Screenshot 2023-09-24 at 8 22 42 PM

In case of using `t.Parallel()` inside a for loop, the loop variables must be copied, otherwise only the last test will be executed.

```go
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()
```
changed to
```go
	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()
```

also the AuthServer must be created within the test, otherwise some tests fail.

`t.Log(test.Name)` can be used to verify the issue:
```go
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()
			t.Log("Test name:", test.name)
```
@codecov
Copy link

codecov bot commented Sep 25, 2023

Codecov Report

Patch coverage has no change and project coverage change: +0.55% 🎉

Comparison is base (1ae2aac) 95.65% compared to head (b6a0e29) 96.21%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #710      +/-   ##
==========================================
+ Coverage   95.65%   96.21%   +0.55%     
==========================================
  Files          12       12              
  Lines        1612     1612              
==========================================
+ Hits         1542     1551       +9     
+ Misses         42       38       -4     
+ Partials       28       23       -5     

see 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@jeevatkm jeevatkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SVilgelm Thanks for the unit test improvements.

@jeevatkm jeevatkm merged commit b852413 into go-resty:master Sep 25, 2023
@SVilgelm
Copy link
Contributor Author

I saw this issue so many times :)

@jeevatkm
Copy link
Member

jeevatkm commented Sep 25, 2023

It would be helpful if go test throws some warning or error if it detects this case.

@SVilgelm SVilgelm deleted the fix-TestClientOnResponseError branch September 26, 2023 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants