diff --git a/env_test.go b/env_test.go index c36d70c..caca509 100644 --- a/env_test.go +++ b/env_test.go @@ -17,7 +17,12 @@ type testMock struct { m sync.Mutex cleanups []func() - fatals []struct { + logs []struct { + format string + args []interface{} + resultString string + } + fatals []struct { format string args []interface{} resultString string @@ -69,6 +74,17 @@ func (t *testMock) Fatalf(format string, args ...interface{}) { } } +func (t *testMock) Logf(format string, args ...interface{}) { + t.m.Lock() + defer t.m.Unlock() + + t.logs = append(t.logs, struct { + format string + args []interface{} + resultString string + }{format: format, args: args, resultString: fmt.Sprintf(format, args...)}) +} + func (t *testMock) Name() string { if t.TestName == "" { return "mock" @@ -157,6 +173,7 @@ func Test_Env_Cache(t *testing.T) { cntF := func() int { res := e.Cache(nil, nil, func() (res interface{}, err error) { val++ + e.T().Logf("val: ", val) return val, nil }) return res.(int) diff --git a/examples/simple/simple_test.go b/examples/simple/simple_test.go index 04d825a..bb67d13 100644 --- a/examples/simple/simple_test.go +++ b/examples/simple/simple_test.go @@ -14,6 +14,7 @@ import ( func counter(e fixenv.Env) int { return fixenv.Cache(e, nil, nil, func() (res int, err error) { globalCounter++ + e.T().Logf("increment globalCounter to: ") return globalCounter, nil }) } @@ -43,6 +44,7 @@ func counterTestAndSubtest(e fixenv.Env) int { Scope: fixenv.ScopeTestAndSubtests, }, func() (res int, err error) { globalTestAndSubtestCounter++ + e.T().Logf("increment globalTestAndSubtestCounter to: ") return globalTestAndSubtestCounter, nil }) } diff --git a/interface.go b/interface.go index 3ac4e85..85eb82a 100644 --- a/interface.go +++ b/interface.go @@ -95,6 +95,13 @@ type T interface { // (which then runs all deferred calls in the current goroutine). Execution will continue at the next test or benchmark. FailNow must be called from the goroutine running the test or benchmark function, not from other goroutines created during the test. Calling FailNow does not stop those other goroutines. Fatalf(format string, args ...interface{}) + // Logf formats its arguments according to the format, analogous to Printf, and + // records the text in the error log. A final newline is added if not provided. For + // tests, the text will be printed only if the test fails or the -test.v flag is + // set. For benchmarks, the text is always printed to avoid having performance + // depend on the value of the -test.v flag. + Logf(format string, args ...interface{}) + // Name returns the name of the running (sub-) test or benchmark. // // The name will include the name of the test along with the names diff --git a/maintest.go b/maintest.go index d405ad4..9c54d51 100644 --- a/maintest.go +++ b/maintest.go @@ -2,6 +2,7 @@ package fixenv import ( "fmt" + "log" "sync" ) @@ -90,6 +91,10 @@ func (t *virtualTest) Fatalf(format string, args ...interface{}) { t.fatalf(format, args...) } +func (t *virtualTest) Logf(format string, args ...interface{}) { + log.Printf(format, args...) +} + func (t *virtualTest) Name() string { return packageScopeName } diff --git a/maintest_test.go b/maintest_test.go index 25ce227..aa44dc7 100644 --- a/maintest_test.go +++ b/maintest_test.go @@ -12,6 +12,7 @@ func TestCreateMainTestEnv(t *testing.T) { t.Run("simple", func(t *testing.T) { at := assert.New(t) e, cancel := CreateMainTestEnv(nil) + e.T().Logf("env created") at.Equal(packageScopeName, e.t.Name()) at.NotNil(globalScopeInfo[packageScopeName]) cancel()