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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c