doubingjian2006
2014-08-27 10:14
浏览 23
已采纳

异常期间在golang中运行测试用例

I am writing testcases for method with doesnt return any values , for eg:

func GetByNameReturnNull(serName string)
{
 //Logic
}

My testcasefile is myTest.go which has two parameters , one calling the method with invalid input and calling the method with valid input.

func Test1(t *testing.T) { 
    GetByNameReturnNull("Invalid")
}


func Test2(t *testing.T) { 

    GetByNameReturnNull("valid")
}

So , the first testcase will fail and throw the exception , I cant handle it in the conventional way like ,

"check for err from the returned method because the method doesnt return anything at all. When I execute the command,

$go test ./... -v

the second testcase will not execute because of the exception of the first.

So Without changing any logic in the base method(GetByNameReturnNull) to return err or anything , is there any way to handle this scenario in the testcase file itself to print

1 fail 1 pass in the output?

图片转代码服务由CSDN问答提供 功能建议

我正在为方法编写测试用例,并且不返回任何值,例如: < pre> func GetByNameReturnNull(serName字符串) { // Logic }

我的测试用例文件是myTest.go,它具有两个参数, 一个使用无效输入调用该方法并使用有效输入调用该方法。

  func Test1(t * testing.T){
 GetByNameReturnNull(“ Invalid”)
}  
 
 
func Test2(t * testing.T){
 
 GetByNameReturnNull(“ valid”)
} 
   
 
 

因此,第一个测试用例 将会失败并抛出异常,我无法以常规方式来处理它,

“从返回的方法中检查错误,因为该方法根本不返回任何内容。 当我执行 命令,

  $ go test。/ ... -v 
   
 
 

第二个测试用例将不会执行,因为

因此无需更改基本方法(GetByNameReturnNull)中的任何逻辑即可返回err 或其他任何方法,是否有任何方法可以在测试用例文件本身中处理这种情况,以进行打印

  1失败1则输出失败?
    
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqba94394 2014-08-27 15:41
    已采纳

    @VonC is correct, there's no way to automatically handle it, however you can simply make a wrapper and call it in each test.

    This way you don't have to use a global variable to keep track of the tests.

    Example:

    func logPanic(t *testing.T, f func()) {
        defer func() {
            if err := recover(); err != nil {
                t.Errorf("paniced: %v", err)
            }
        }()
        f()
    }
    
    func Test1(t *testing.T) {
        logPanic(t, func() {
            GetByNameReturnNull("invalid")
        })
        //or if the function doesn't take arguments
        //logPanic(t, GetByNameReturnNull)
    }
    
    func Test2(t *testing.T) {
        logPanic(t, func() {
            GetByNameReturnNull("valid")
        })
    }
    
    打赏 评论
  • duanjian9148 2014-08-27 10:46

    You shouldn't see "1 fail", if you expect your test to panic.
    You should see both tests succeed.

    Instead, you should test specifically the panic case, as described, for instance, in "Understanding Defer, Panic and Recover ":

    func TestPanic(t *testing.T) error {
        defer func() {
            fmt.Println("Start Panic Defer")
            if r := recover(); r != nil {
                fmt.Println("Defer Panic:", r)
            } else {
                t.Error("Should have panicked!")
            }
        }()
    
        fmt.Println("Start Test")
        panic("Mimic Panic")
    }
    

    That test would pass if you call a function which exits with a panic.

    打赏 评论

相关推荐 更多相似问题