1

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?
4
  • 1
    Should we assume that this GetByNameReturnNull function can't be modified to return an error? If it can create an error, it should really return one. Commented Aug 27, 2014 at 10:25
  • 1
    If so, please have a look at blog.golang.org/defer-panic-and-recover Commented Aug 27, 2014 at 10:26
  • 1
    I dont want to defer the testcases either , it should execute the testcases in sequence like , test1, test2,test3 , and finally if any of the tests fail , it should return 1 pass 2 fail , suppose if all the testcases pass , the output should be 3 pass... Commented Aug 27, 2014 at 11:05
  • throwing exceptions ?? this is not java. It can't be easily tested? Code smell! return an error. Commented Aug 27, 2014 at 18:17

2 Answers 2

4

@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")
    })
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice idea, more practical than my answer. +1
3

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.

4 Comments

One observation is , If I have 10 testcases to be executed in sequence I wont know in the beginning which will Fail and which will pass .Typically it can be 8 pass 2 fail/ 10 pass 0 fail / 7 pass 3 fail ,depending on various factors like network etc. Ultimately the user should be aware on the pass and fail testcases.
@user1907849 then you can customize the defer func() and its recover in order to print the fail cases.
But I dont know which all testcases will fail , thats the hardcore problem here.... sometimes testcase1 may fail , others testcase10 may fail ... if sometimes all the testcases may pass ...
@user1907849 yes, but you can record in a global variable which test fail (in the recover) and which passes. And then you can print the total at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.