Add test for logging capture.

pull/44/head
Sean Talts 2014-03-07 14:51:52 -05:00
parent 72c1136caa
commit 814eb1589b
1 changed files with 57 additions and 0 deletions

View File

@ -3,6 +3,10 @@ package suite
import (
"testing"
"github.com/stretchr/testify/assert"
"os"
"io/ioutil"
"errors"
"fmt"
)
// 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.
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")
}