From 50ba9cfb04e257fafa3fdd566f9f70a1986339e8 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 18 Aug 2026 11:30:54 +0200 Subject: assert: add Panics, PanicsWith I want to use this for test coverage in microprom. This turned out to be a bigger change than expected because testcapture has a dependency on assert, which I had to invert to be able to use testcapture.Capture() in assert. --- testcapture/testcapture.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 testcapture/testcapture.go (limited to 'testcapture/testcapture.go') diff --git a/testcapture/testcapture.go b/testcapture/testcapture.go new file mode 100644 index 0000000..b0c0fac --- /dev/null +++ b/testcapture/testcapture.go @@ -0,0 +1,43 @@ +// SPDX-FileCopyrightText: 2026 Stefan Majewsky +// SPDX-License-Identifier: Apache-2.0 + +// Package testcapture contains [Capture], a function that executes test code in a way that captures error messages and side effects without failing the overall test. +// +// The main intended use case is testing test assertions where calls to e.g. t.Error() are an expected part of a successful test run. +package testcapture + +import ( + "context" + "io" + "testing" +) + +// TestingTB contains all the public functions of [testing.TB] (as of Go 1.26). +// Functions in this package use this type instead of [testing.TB] because the capture device used by package testcapture cannot implement [testing.TB]: It contains methods that are private to the standard library. +type TestingTB interface { + ArtifactDir() string + Attr(key, value string) + Chdir(dir string) + Cleanup(func()) + Context() context.Context + Error(args ...any) + Errorf(format string, args ...any) + Fail() + Failed() bool + FailNow() + Fatal(args ...any) + Fatalf(format string, args ...any) + Helper() + Log(args ...any) + Logf(format string, args ...any) + Name() string + Output() io.Writer + Setenv(key, value string) + Skip(args ...any) + Skipf(format string, args ...any) + SkipNow() + Skipped() bool + TempDir() string +} + +var _ TestingTB = testing.TB(nil) -- cgit v1.3.1