# golden | Status | | | ------------- |-----------| | Stability | [alpha]: traces, metrics, logs | | Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aopen%20label%3Apkg%2Fgolden%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Apkg%2Fgolden) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aclosed%20label%3Apkg%2Fgolden%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Apkg%2Fgolden) | | [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@djaglowski](https://www.github.com/djaglowski), [@atoulme](https://www.github.com/atoulme) | [alpha]: https://github.com/open-telemetry/opentelemetry-collector#alpha The package golden provides utilities for reading and writing files with metrics, traces and logs in YAML format. The package is expected to be used with pkg/pdatatest module. ## Generating an expected result file The easiest way to capture the expected result in a file is `golden.WriteMetrics` or `golden.WriteLogs`. When writing a new test: 1. Write the test as if the expected file exists. 2. Follow the steps below for updating an existing test. When updating an existing test: 1. Add a call to `golden.WriteMetrics` or `golden.WriteLogs` or in the appropriate place. 2. Run the test once. 3. Remove the call to `golden.WriteMetrics` or `golden.WriteLogs`. NOTE: `golden.WriteMetrics` will always mark the test as failed. This behavior is necessary to ensure the function is removed after the golden file is written. ```go func TestScraper(t *testing.T) { cfg := createDefaultConfig().(*Config) require.NoError(t, component.ValidateConfig(cfg)) scraper := newScraper(componenttest.NewNopReceiverCreateSettings(), cfg) err := scraper.start(context.Background(), componenttest.NewNopHost()) require.NoError(t, err) actualMetrics, err := scraper.scrape(context.Background()) require.NoError(t, err) expectedFile := filepath.Join("testdata", "scraper", "expected.yaml") golden.WriteMetrics(t, expectedFile, actualMetrics) // This line is temporary! TODO remove this!! expectedMetrics, err := golden.ReadMetrics(expectedFile) require.NoError(t, err) require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, actualMetrics)) } ```