dongshenling6585 2018-07-23 10:05
浏览 158

无法生成Go Coverage文件

I tried run a sytemTest in this article: https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests

so follow the tips first I create a system test file named main_test.go like this:

func TestSystem(t *testing.T) {
    t.Logf("systemtest mod=%v", *SystemTest)
    if *SystemTest {
        t.Log("runing system test....")
        main()
    }
}

when this unit test is executed, whole main function will be executed

then I build a test birnary :

go test -c -covermode=count -coverpkg ./... -o main.test

and execute the test birnary file in my test environment

./main.test -systemTest  -test.coverprofile ./coverage.cov

because the program will listen and waiting for client's request, so it will not exit unless I exit manunal, which means the coverprofile won't be genarated

so I start a timer to stop the program after 15 seconds... however when program exits , the coverprofile still not genarated

if test not call main, the coverprofile can be genarated normally

See the main function

var mkrtExitWait sync.WaitGroup
var mkrtExitCode int
var mkrtRunning bool = false

func MKrtRun() int {
    mkrtExitWait.Add(1)
    mkrtRunning = true
    mkrtExitWait.Wait()
    return mkrtExitCode
}
func MKrtExit(code int) {
    if !mkrtRunning {
        os.Exit(code)
    } else {
        mkrtRunning = false
        mkrtExitCode = code
        mkrtExitWait.Done()
    }
}


func main() {
    // listen and serve code 
    ......

    if *SystemTest {  // a command flag 
        go func(){
            time.Sleep(time.Second * 10)
            MKrtExit(0)
        }()
    }
    MKrtRun()
}

I tried some ways to genrate coverage file as followed, but it's not working:

  1. send a client request to tell test server to execute os.Exit(0) when program is running

  2. send a client request to tell test server to execute panic() when program is running

  3. kill the process to force exit the program

What's the problem?

How can I generate coverage file?

  • 写回答

3条回答 默认 最新

  • douxing5199 2018-07-23 12:02
    关注

    System testing is usually used when you are running some external tests on your testing

    This would mean you need to modify your main program to exit on certain intrrupts. SIGINT is most apt signal for your main program to exit as it confirms with POSIX signal compliance

    This can be done by adding following

            // Handle graceful shutdown of http server via OS Interrupt
            quit := make(chan os.Signal)
            signal.Notify(quit, os.Interrupt)
    
            // Block a goroutine on Interrupt channel and close
            // http server to exit gracefully
            go func() {
                <-quit
                log.Info("receive interrupt signal")
                MKrtEixt(0)
            }() 
    

    So then your testing steps would be

    1. Bring up system-under-test
    2. Run external tests
    3. Issue SIGINT to system-under-test, once tests complete
    4. Collect the coverage files
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据