Skip to content

Commit a53be35

Browse files
ccoVeilledolmen
authored andcommitted
Improve captureTestingT helper
This helper is used to capture the testing.TB interface, and compare the log output with the expected output. This is useful for testing and refactoring purposes. This commit improves the helper by displaying: - the captured and expected outputs on a newline. - the special characters in the captured output, such as newlines and tabs. Both help with readability. Here is an example of the output before and after the change: Before: assertions_test.go:3422: Logged Error: Should be in error chain expected: *assert.customError in chain: "EOF" (*errors.errorString) assertions_test.go:3422: Should log Error: Should be in error chain: expected: *assert.customError in chain: "EOF" (*errors.errorString) After: assertions_test.go:3394: Recorded Error: "Should be in error chain:\nexpected: *assert.customError\nin chain: \"EOF\" (*errors.errorString)\n" assertions_test.go:3394: Expected Error: "Should be in error chain\nexpected: *assert.customError\nin chain: \"EOF\" (*errors.errorString)" The new format helps to identify the differences: - the missing colon after "Should be in error chain" - the extra newline in the captured output Note: I spent 10 minutes on this change, because I lost 10 minutes in finding the differences between the captured and expected output on a refactoring I was doing.
1 parent aafb604 commit a53be35

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

assert/assertions_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,23 +3675,23 @@ func (ctt *captureTestingT) checkResultAndErrMsg(t *testing.T, expectedRes, res
36753675
contents := parseLabeledOutput(ctt.msg)
36763676
if res == true {
36773677
if contents != nil {
3678-
t.Errorf("Should not log an error")
3678+
t.Errorf("Should not log an error. Log output: %q", ctt.msg)
36793679
}
36803680
return
36813681
}
36823682
if contents == nil {
3683-
t.Errorf("Should log an error. Log output: %v", ctt.msg)
3683+
t.Errorf("Should log an error. Log output: %q", ctt.msg)
36843684
return
36853685
}
36863686
for _, content := range contents {
36873687
if content.label == "Error" {
36883688
if expectedErrMsg == content.content {
36893689
return
36903690
}
3691-
t.Errorf("Logged Error: %v", content.content)
3691+
t.Errorf("Recorded Error: %q", content.content)
36923692
}
36933693
}
3694-
t.Errorf("Should log Error: %v", expectedErrMsg)
3694+
t.Errorf("Expected Error: %q", expectedErrMsg)
36953695
}
36963696

36973697
func TestErrorIs(t *testing.T) {

0 commit comments

Comments
 (0)