doujiyun0041 2018-03-26 18:23
浏览 59
已采纳

标准库错误的Golang覆盖率测试

I've been trying to learn about Go's built-in testing framework and getting proper test coverage.

In one of the files I'm testing I'm only getting ~87% coverage:

coverage: 87.5% of statements

Here's a section of the code covered in the test:

// Check that the working directory is set
if *strctFlags.PtrWorkDir == "" {

// if the working directory is empty, set it to the current directory
strTemp, err := os.Getwd()
if err != nil {
    return "", errors.New("Could not get current working directory")
}

*strctFlags.PtrWorkDir = strTemp

} else if stat, err := os.Stat(*strctFlags.PtrWorkDir); err != nil ||
!stat.IsDir() {

    // Existence check of the work dir
    return "", errors.New("Specified directory \"" + *strctFlags.PtrWorkDir + "\" could not be found or was not a directory")
}

*strctFlags.PtrWorkDir, err = filepath.Abs(*strctFlags.PtrWorkDir)
if err != nil {
    return "", errors.New("Could not determine absolute filepath: " + err.Error())
}

The parts not covered in the test according to the .out file are the "if err != nil {}" blocks, which would be errors returned from standard library calls.

While I think the likelihood of the standard library passing errors would be slim unless there would be hardware failure, I think it would be good to know that the error is handled in the application properly. Also, checking returned errors is, to my understanding, idiomatic Go so I would think it would be good to be able to test error handling properly.

How do people handle testing errors like the situations above? Is it possible to get 100% coverage, or am I doing or structuring something incorrectly? Or do people skip testing those conditions?

  • 写回答

2条回答 默认 最新

  • dongwo5940 2018-03-27 03:38
    关注

    As @flimzy explained above it is not good to aim for 100% coverage instead aim for useful test coverage.

    Though you can test the system calls with slight modification to the code like this

    package foo
    
    // Get Working directory function
    var osGetWd = os.Getwd
    
    func GetWorkingDirectory() (string,error){
        strTemp, err := osGetWd() // Using the function defined above
        if err != nil {
           return "", errors.New("Could not get current working directory")
        return strTemp,nil
    }
    

    And while testing

    package foo
    
    func TestGetWdError() {
        // Mocked function for os.Getwd
        myGetWd := func() (string,error) {
          myErr := errors.New("Simulated error")
          return "",myErr
        }
    
        // Update the var to this mocked function
        osGetWd = myGetWd
    
       // This will return error
       val,err := GetWorkingDirectory()
    }
    

    This will help you to achieve 100% coverage

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误