Skip to content

Commit

Permalink
added verbose option tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cerebrovinny committed Jan 11, 2025
1 parent c399118 commit b209fb3
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 75 deletions.
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,19 @@ version-windows: build-windows
deps:
go mod download

# Run acceptance tests
testacc: get
go test $(TEST) -v $(TESTARGS) -timeout 2m
# Test verbosity levels: quiet, normal, verbose
TEST_VERBOSITY ?= normal

.PHONY: test
test: ## Run unit tests
ATMOS_TEST_VERBOSITY=$(TEST_VERBOSITY) go test -v ./...

.PHONY: testacc
testacc: ## Run acceptance tests
ATMOS_TEST_VERBOSITY=$(TEST_VERBOSITY) go test -v ./tests -timeout 20m

.PHONY: testrace
testrace: ## Run tests with race detector
ATMOS_TEST_VERBOSITY=$(TEST_VERBOSITY) go test -race -v ./...

.PHONY: lint get build version build-linux build-windows build-macos deps version-linux version-windows version-macos testacc
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,25 @@ Copyright © 2017-2024 [Cloud Posse, LLC](https://cpco.io/copyright)
<a href="https://cloudposse.com/readme/footer/link?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=readme_footer_link"><img alt="README footer" src="https://cloudposse.com/readme/footer/img"/></a>

<img alt="Beacon" width="0" src="https://ga-beacon.cloudposse.com/UA-76589703-4/cloudposse/atmos?pixel&cs=github&cm=readme&an=atmos"/>

## Testing

### Test Verbosity Control

The test suite supports three verbosity levels to control output:

- `quiet`: Minimal output, only shows failures
- `normal`: Standard test output (default)
- `verbose`: Detailed test output with additional debugging information

You can control the verbosity level using the `TEST_VERBOSITY` environment variable:

```bash
# Run tests with minimal output
make test TEST_VERBOSITY=quiet

# Run acceptance tests with verbose output
make testacc TEST_VERBOSITY=verbose
```

This is particularly useful when running tests in CI environments or when debugging specific test failures.
103 changes: 103 additions & 0 deletions pkg/utils/test_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package utils

import (
"fmt"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

// TestVerbosity controls the verbosity level of test output
type TestVerbosity int

const (
// TestVerbosityQuiet minimal output, only failures
TestVerbosityQuiet TestVerbosity = iota
// TestVerbosityNormal standard test output
TestVerbosityNormal
// TestVerbosityVerbose detailed test output
TestVerbosityVerbose
)

var (
// DefaultTestVerbosity is the default verbosity level for tests
DefaultTestVerbosity = TestVerbosityNormal
// Store descriptions to avoid repetition
printedDescriptions = make(map[string]bool)
)

// GetTestVerbosity returns the current test verbosity level
func GetTestVerbosity() TestVerbosity {
verbStr := os.Getenv("ATMOS_TEST_VERBOSITY")
switch verbStr {
case "quiet":
return TestVerbosityQuiet
case "verbose":
return TestVerbosityVerbose
default:
return DefaultTestVerbosity
}
}

// TestLogf logs a message during test execution based on verbosity level
func TestLogf(t *testing.T, minVerbosity TestVerbosity, format string, args ...interface{}) {
if GetTestVerbosity() >= minVerbosity {
t.Logf(format, args...)
}
}

// TestLog logs a message during test execution based on verbosity level
func TestLog(t *testing.T, minVerbosity TestVerbosity, args ...interface{}) {
if GetTestVerbosity() >= minVerbosity {
t.Log(args...)
}
}

// LogTestDescription logs a test description only once to avoid repetition
func LogTestDescription(t *testing.T, description string) {
if description == "" {
return
}

// Create a unique key for the description to avoid repetition within the same test
key := fmt.Sprintf("%s-%s", t.Name(), description)
if !printedDescriptions[key] {
if GetTestVerbosity() == TestVerbosityQuiet {
t.Logf("Test Description: %s", description)
} else {
t.Logf("\nTest Description: %s", description)
}
printedDescriptions[key] = true
}
}

// LogTestFailure logs detailed failure information based on verbosity
func LogTestFailure(t *testing.T, description string, expected, actual interface{}, extraInfo ...string) {
LogTestDescription(t, description)

verbosity := GetTestVerbosity()
if verbosity == TestVerbosityQuiet {
t.Errorf("Expected: %v\nGot: %v", expected, actual)
} else {
t.Errorf("\nExpected: %v\nGot: %v", expected, actual)
if len(extraInfo) > 0 && verbosity >= TestVerbosityNormal {
t.Logf("Additional Info:\n%s", strings.Join(extraInfo, "\n"))
}
}
}

// AssertTestResult is a wrapper for testify/assert that respects verbosity
func AssertTestResult(t *testing.T, assertion func() bool, description string, msgAndArgs ...interface{}) bool {
result := assertion()
if !result {
LogTestDescription(t, description)
return assert.True(t, result, msgAndArgs...)
}
if GetTestVerbosity() >= TestVerbosityVerbose {
LogTestDescription(t, description)
return assert.True(t, result, msgAndArgs...)
}
return result
}
Loading

0 comments on commit b209fb3

Please sign in to comment.