mirror of https://github.com/stretchr/testify.git
Add test for logging capture.
parent
72c1136caa
commit
814eb1589b
|
@ -3,6 +3,10 @@ package suite
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"os"
|
||||||
|
"io/ioutil"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This suite is intended to store values to make sure that only
|
// This suite is intended to store values to make sure that only
|
||||||
|
@ -100,3 +104,56 @@ func TestRunSuite(t *testing.T) {
|
||||||
// have been run at all.
|
// have been run at all.
|
||||||
assert.Equal(t, suiteTester.NonTestMethodRunCount, 0)
|
assert.Equal(t, suiteTester.NonTestMethodRunCount, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SuiteLoggingTester struct {
|
||||||
|
Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SuiteLoggingTester) TestLoggingPass() {
|
||||||
|
s.T().Log("TESTLOGPASS")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SuiteLoggingTester) TestLoggingFail() {
|
||||||
|
s.T().Log("TESTLOGFAIL")
|
||||||
|
assert.NotNil(s.T(), nil) //expected to fail
|
||||||
|
}
|
||||||
|
|
||||||
|
type StdoutCapture struct {
|
||||||
|
oldStdout *os.File
|
||||||
|
readPipe *os.File
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sc *StdoutCapture) StartCapture() {
|
||||||
|
fmt.Println("Starting capture1!")
|
||||||
|
sc.oldStdout = os.Stdout
|
||||||
|
sc.readPipe, os.Stdout, _ = os.Pipe()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sc *StdoutCapture) StopCapture() (string, error) {
|
||||||
|
if sc.oldStdout == nil || sc.readPipe == nil {
|
||||||
|
return "", errors.New("StartCapture not called before StopCapture")
|
||||||
|
}
|
||||||
|
os.Stdout.Close()
|
||||||
|
os.Stdout = sc.oldStdout
|
||||||
|
bytes, err := ioutil.ReadAll(sc.readPipe)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(bytes), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSuiteLogging(t *testing.T) {
|
||||||
|
testT := testing.T{}
|
||||||
|
|
||||||
|
suiteLoggingTester := new(SuiteLoggingTester)
|
||||||
|
|
||||||
|
capture := StdoutCapture{}
|
||||||
|
capture.StartCapture()
|
||||||
|
Run(&testT, suiteLoggingTester)
|
||||||
|
output, err := capture.StopCapture()
|
||||||
|
|
||||||
|
assert.Nil(t, err, "Got an error trying to capture stdout!")
|
||||||
|
|
||||||
|
assert.Contains(t, output, "TESTLOGFAIL")
|
||||||
|
assert.NotContains(t, output, "TESTLOGPASS")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue