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

go test coverage rate not work for exec.Command #47515

Closed
suchen-sci opened this issue Aug 3, 2021 · 1 comment
Closed

go test coverage rate not work for exec.Command #47515

suchen-sci opened this issue Aug 3, 2021 · 1 comment

Comments

@suchen-sci
Copy link

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

@seankhliao
Copy link
Member

Related #30219
Duplicate of #31007

@golang golang locked and limited conversation to collaborators Aug 3, 2022
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

3 participants