From 57d6437cef46c542c7369b2f435447b39461990b Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Thu, 8 May 2025 18:41:57 +0200 Subject: [PATCH] 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. --- assert/assertions_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index f3d41a5ac..6150d4416 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -3198,12 +3198,12 @@ func (ctt *captureTestingT) checkResultAndErrMsg(t *testing.T, expectedRes, res contents := parseLabeledOutput(ctt.msg) if res == true { if contents != nil { - t.Errorf("Should not log an error") + t.Errorf("Should not log an error. Log output: %q", ctt.msg) } return } if contents == nil { - t.Errorf("Should log an error. Log output: %v", ctt.msg) + t.Errorf("Should log an error. Log output: %q", ctt.msg) return } for _, content := range contents { @@ -3211,10 +3211,10 @@ func (ctt *captureTestingT) checkResultAndErrMsg(t *testing.T, expectedRes, res if expectedErrMsg == content.content { return } - t.Errorf("Logged Error: %v", content.content) + t.Errorf("Recorded Error: %q", content.content) } } - t.Errorf("Should log Error: %v", expectedErrMsg) + t.Errorf("Expected Error: %q", expectedErrMsg) } func TestErrorIs(t *testing.T) {