Skip to content

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

Description

@cwmos

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions