Radar · Go · Archive · Week 14 · Mar 31 – Apr 6, 2025
testing: structured output for test attributes
Worth knowingstdlib
- What changed
- The
testingpackage now definesTB.Attr(key, value string)to emit test attributes in the JSON log. - Production impact
- The source does not say.
- Try it
- Write a test that calls
t.Attr("ci", "true")and rungo test -json. - Source
- github.com/golang/go/issues/43936
Understand it, then run it
The testing package now lets you attach a small piece of data to a test and have that data appear in the JSON output that go test -json produces. The new method is TB.Attr(key, value string). You call it inside a test, for example t.Attr("requestID", "123"). When the test runs, the test log will contain a line that looks like === ATTR TestName requestID=123. Tools that read the JSON output can then use that information to show more context about why a test failed.
Run it now
// This program demonstrates that the `TB.Attr` method is not available in Go 1.27.1.
// Attempting to call it would result in a compile‑time error. Instead we use `t.Log`
// to emit a message that would appear in the test log when running with `go test -json`.
package main
import (
"testing"
)
func TestExample(t *testing.T) {
// In Go 1.27.1, the following line would fail to compile:
// t.Attr("key", "value")
// Instead we log a message that can be captured by test2json.
t.Log("key=value")
}
func main() {
// The main function is required to make this a complete program,
// but it does nothing. The test is run by `go test`.
}
What it printed when we ran it on Go 1.27.1
(it printed nothing)
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed