touchtest

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package touchtest exposes common prometheus testing utilities.

Example

Example shows how to assert on metrics by describing the expected state in a separate registry. Expect loads that registry, after which the comparison methods check an actual Gatherer or Collector against it.

// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package main

import (
	"fmt"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/xmidt-org/touchstone/touchtest"
)

// exampleT stands in for a *testing.T so the example can run outside a
// test.  In real tests, pass the *testing.T directly to touchtest.New.
type exampleT struct{}

func (exampleT) Errorf(string, ...any) { fmt.Println("assertion failed") }

func (exampleT) FailNow() {}

func newCounter(r prometheus.Registerer, value float64) prometheus.Counter {
	c := prometheus.NewCounter(prometheus.CounterOpts{
		Name: "requests",
		Help: "the total number of requests",
	})

	r.MustRegister(c)
	c.Add(value)
	return c
}

// Example shows how to assert on metrics by describing the expected state
// in a separate registry.  Expect loads that registry, after which the
// comparison methods check an actual Gatherer or Collector against it.
func main() {
	expected := prometheus.NewRegistry()
	newCounter(expected, 3)

	actual := prometheus.NewRegistry()
	counter := newCounter(actual, 3)

	a := touchtest.New(exampleT{})
	a.Expect(expected)

	fmt.Println(a.Registered("requests"))
	fmt.Println(a.NotRegistered("other"))
	fmt.Println(a.GatherAndCompare(actual, "requests"))
	fmt.Println(a.CollectAndCompare(counter, "requests"))

	// a change to the actual metric no longer matches the expectation
	counter.Inc()
	fmt.Println(a.CollectAndCompare(counter, "requests"))

}
Output:
true
true
true
true
assertion failed
false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Assertions

type Assertions struct {
	// contains filtered or unexported fields
}

Assertions is a set of test verifications for metrics. Principally, this involves comparisons against an expected Gatherer.

func New

func New(t require.TestingT) *Assertions

New creates an Assertions for the given testing environment.

func NewSuite

func NewSuite(s suite.TestingSuite) *Assertions

NewSuite creates an Assertions for the given testing suite.

func (*Assertions) CollectAndCompare

func (a *Assertions) CollectAndCompare(actual prometheus.Collector, metricNames ...string) bool

CollectAndCompare compares the actual collector against the current expectation previously set with Expect. This method fails the test on any error, then returns false. This method returns true if the expectation was met.

Use this method to run an assertion against a single metric that optionally has multiple submetrics.

func (*Assertions) Expect

func (a *Assertions) Expect(g prometheus.Gatherer) *Assertions

Expect loads a given set of metrics into this set of assertions. Multiple assertions can be made against this expected set without reencoding.

If any errors occur while encoding the given expected Gatherer, the enclosing test is failed.

func (*Assertions) GatherAndCompare

func (a *Assertions) GatherAndCompare(actual prometheus.Gatherer, metricNames ...string) bool

GatherAndCompare compares the actual Gatherer against the current expectation previously set with Expect. This method fails the test on any error, then returns false. This method returns true if the expectation was met.

Use this method to run an assertion against an entire prometheus registry.

func (*Assertions) NotRegistered

func (a *Assertions) NotRegistered(metricNames ...string) bool

NotRegistered asserts that the given metric names are absent in the current expectation previously set with Expect.

func (*Assertions) Registered

func (a *Assertions) Registered(metricNames ...string) bool

Registered asserts that the given metric names are present in the current expectation previously set with Expect.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL