You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a function that pulls a git repo. I don't want to pull too frequently if this function is called multiple times so at the end of the function it stores time.Now() into a package level variable and won't attempt to re-pull again until a certain amount of time has passed. This could be another shining reason why not to use global variables, but the point I want to make here is that I have multiple go tests that end up calling this function. When I run go test ./... and run these tests together in one command, the repeated calls trip the "we just pulled, don't pull again" bit and skips a bunch of mocked function calls. The missed mocked calls will fail the test.
Somewhere I picked up the idea that each test ran more independently than that. One of my tests would call my git pull function and put the time in the cache and in the same test run a completely different test would then call the same function and see the state change from the previous test.
I have already rewritten my particular functions and tests to get around this but I'm surprised to find out that each test isn't fully isolated from changes other tests have made. Is running multiple tests at once supposed to persist package level variables from test to test?
What did you see happen?
A test early in the run will call my function and set a package-level variable that persisted in memory until an entirely different test acted on that change and failed.
What did you expect to see?
I expected each test to be run independently in a way that won't preserve memory from one test to another. If a test fails and leaves things in a bad state, it can cause other tests to fail. I thought this was minimized.
The text was updated successfully, but these errors were encountered:
Go version
go version go1.22.5 linux/amd64
Output of
go env
in your module/workspace:What did you do?
I have a function that pulls a git repo. I don't want to pull too frequently if this function is called multiple times so at the end of the function it stores time.Now() into a package level variable and won't attempt to re-pull again until a certain amount of time has passed. This could be another shining reason why not to use global variables, but the point I want to make here is that I have multiple go tests that end up calling this function. When I run
go test ./...
and run these tests together in one command, the repeated calls trip the "we just pulled, don't pull again" bit and skips a bunch of mocked function calls. The missed mocked calls will fail the test.Somewhere I picked up the idea that each test ran more independently than that. One of my tests would call my git pull function and put the time in the cache and in the same test run a completely different test would then call the same function and see the state change from the previous test.
I have already rewritten my particular functions and tests to get around this but I'm surprised to find out that each test isn't fully isolated from changes other tests have made. Is running multiple tests at once supposed to persist package level variables from test to test?
What did you see happen?
A test early in the run will call my function and set a package-level variable that persisted in memory until an entirely different test acted on that change and failed.
What did you expect to see?
I expected each test to be run independently in a way that won't preserve memory from one test to another. If a test fails and leaves things in a bad state, it can cause other tests to fail. I thought this was minimized.
The text was updated successfully, but these errors were encountered: