Skip to content

Conversation

@pellared
Copy link
Contributor

@pellared pellared commented Jun 13, 2025

Per #38 (review)

Follow:

Note that the order here is actual != expected, and the message uses that order too. Some test frameworks encourage writing these backwards: 0 != x, “expected 0, got x”, and so on. Go does not.

When some test cases need to be checked using different logic from other test cases, it is more appropriate to write multiple test functions. The logic of your test code can get difficult to understand when every entry in a table has to be subjected to multiple kinds of conditional logic to do the right kind of output check for the right kind of input.

@pellared pellared changed the title refactor unit tests to follow Go Test Comments refactor unit tests to follow Go guidelines Jun 13, 2025
@pellared pellared marked this pull request as ready for review June 13, 2025 11:45
Copy link
Member

@ywwg ywwg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for these! I very rarely see tests done in pure Go, most of our stuff is testified

@aknuds1 aknuds1 requested a review from Copilot June 13, 2025 12:24
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors existing unit tests to follow Go’s testing style guidelines for clearer test failures and simpler test logic separation.

  • normalize_label_test.go: Converted to table-driven tests and split out UTF-8 allowed cases into a separate test function.
  • metric_namer_test.go: Renamed expected* fields to want* and updated error messages to use the “got, want” ordering.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
normalize_label_test.go Refactored TestNormalizeLabel into table-driven form and added a separate UTF-8 allowed test.
metric_namer_test.go Changed expectedMetricName/expectedUnitName to wantMetricName/wantUnitName and updated error calls.
Comments suppressed due to low confidence (1)

normalize_label_test.go:40

  • [nitpick] Using tt.label as the subtest name will produce an empty name for the case where label == "". Consider deriving a non-empty name (e.g. include the index or a descriptive string) so the subtest is easier to identify.
t.Run(tt.label, func(t *testing.T) {

@pellared
Copy link
Contributor Author

Anyone willing to merge this PR? 😁

@aknuds1 aknuds1 merged commit 9ed92fe into prometheus:main Jun 18, 2025
3 checks passed
@pellared pellared deleted the refactor-tests branch June 22, 2025 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants