Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: testing: Allow a unit test to specify that the process it runs in should not be reused by other unit tests. #59640

Closed
cwmos opened this issue Apr 14, 2023 · 4 comments

Comments

@cwmos
Copy link

cwmos commented Apr 14, 2023

Some unit tests modify global variables in the process they run in. Currently, unit tests need to restore those variables when they are completed since the same process may be used to run other unit tests.

I propose to extend testing.T with a new function ExitProcess. When a unit test calls ExitProcess, a new process will be started for subsequent unit tests.

With this change, the following test would succeed:

package main 

import "testing"

var globalVariable = 42

func TestMyTest1(t *testing.T) {
  if globalVariable!=42 {
    t.Fail()
  }
  globalVariable=43
  t.ExitProcess()
}

func TestMyTest2(t *testing.T) {
  if globalVariable!=42 {
    t.Fail()
  }
  globalVariable=44
  t.ExitProcess()
}

The ExitProcess() function should probably not be supported for parallel tests.

@cwmos cwmos added the Proposal label Apr 14, 2023
@gopherbot gopherbot added this to the Proposal milestone Apr 14, 2023
@seankhliao
Copy link
Member

Tests that need to run as an independent child process should probably do so explicitly, rather than only exiting after the test.
See https://go.dev/talks/2014/testing.slide#23

@bcmills
Copy link
Contributor

bcmills commented Apr 14, 2023

See, for example, runtime.TestFinalizerRegisterABI.

@bitfield
Copy link

Some unit tests modify global variables in the process they run in.

I don't mean to sound flip, but there's your problem.

@cwmos
Copy link
Author

cwmos commented Apr 17, 2023

Ok, does not seem like a lot of support for this, so I will close it.

Thanks for the input and the idea of starting a subprocess manually! I guess it would also be possible to get coverage reports in some way with this approach.

@cwmos cwmos closed this as completed Apr 17, 2023
@golang golang locked and limited conversation to collaborators Apr 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants