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
To test os.Exit(), one way is to use exec.Command and check exit status. But in this case, since exec.Command open a new process to call exit func, the coverage rate for main test is still zero.
For example, example by Andrew Gerrand
package main
import (
"fmt"
"os"
)
func Crasher() {
fmt.Println("Going down in flames!")
os.Exit(1)
}
package main
import (
"os"
"os/exec"
"testing"
)
func TestCrasher(t *testing.T) {
if os.Getenv("BE_CRASHER") == "1" {
Crasher()
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestCrasher")
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}
In this case, the coverage rate for Exit is still zero although we have a test for it.
I think this is a problem for all similar cases, if test start new processes to test the function
The text was updated successfully, but these errors were encountered:
To test os.Exit(), one way is to use exec.Command and check exit status. But in this case, since exec.Command open a new process to call exit func, the coverage rate for main test is still zero.
For example, example by Andrew Gerrand
In this case, the coverage rate for Exit is still zero although we have a test for it.
I think this is a problem for all similar cases, if test start new processes to test the function
The text was updated successfully, but these errors were encountered: