func (s *BootstrapS) TestLogfAndGetTestLog(c *check.C) { c.Logf("Hello %v", "there!") log := c.GetTestLog() if log != "Hello there!\n" { critical(fmt.Sprintf("Logf() or GetTestLog() is not working! Got: %#v", log)) } }
func (s *BootstrapS) TestMinLogger(c *check.C) { var logger minLogger logger = log.New(os.Stderr, "", 0) logger = c logger.Output(0, "Hello there") expected := `\[LOG\] [0-9]+:[0-9][0-9]\.[0-9][0-9][0-9] +Hello there\n` output := c.GetTestLog() c.Assert(output, check.Matches, expected) }
func (s *FoundationS) TestSucceedNow(c *check.C) { defer (func() { if c.Failed() { c.Error("SucceedNow() didn't succeed the test") } if c.GetTestLog() != "" { c.Error("Something got logged:\n" + c.GetTestLog()) } })() c.Fail() c.SucceedNow() c.Log("SucceedNow() didn't stop the test") }
func (s *FoundationS) TestErrorf(c *check.C) { // Do not use checkState() here. It depends on Errorf() working. expectedLog := fmt.Sprintf("foundation_test.go:%d:\n"+ " c.Errorf(\"Error %%v!\", \"message\")\n"+ "... Error: Error message!\n\n", getMyLine()+1) c.Errorf("Error %v!", "message") failed := c.Failed() c.Succeed() if log := c.GetTestLog(); log != expectedLog { c.Logf("Errorf() logged %#v rather than %#v", log, expectedLog) c.Fail() } if !failed { c.Logf("Errorf() didn't put the test in a failed state") c.Fail() } }
func (s *FoundationS) TestFatalf(c *check.C) { var line int defer (func() { if !c.Failed() { c.Error("Fatalf() didn't fail the test") } else { c.Succeed() expected := fmt.Sprintf("foundation_test.go:%d:\n"+ " c.Fatalf(\"Die %%s!\", \"now\")\n"+ "... Error: Die now!\n\n", line) if c.GetTestLog() != expected { c.Error("Incorrect log:", c.GetTestLog()) } } })() line = getMyLine() + 1 c.Fatalf("Die %s!", "now") c.Log("Fatalf() didn't stop the test") }
// Verify the state of the test. Note that since this also verifies if // the test is supposed to be in a failed state, no other checks should // be done in addition to what is being tested. func checkState(c *check.C, result interface{}, expected *expectedState) { failed := c.Failed() c.Succeed() log := c.GetTestLog() matched, matchError := regexp.MatchString("^"+expected.log+"$", log) if matchError != nil { c.Errorf("Error in matching expression used in testing %s", expected.name) } else if !matched { c.Errorf("%s logged:\n----------\n%s----------\n\nExpected:\n----------\n%s\n----------", expected.name, log, expected.log) } if result != expected.result { c.Errorf("%s returned %#v rather than %#v", expected.name, result, expected.result) } if failed != expected.failed { if failed { c.Errorf("%s has failed when it shouldn't", expected.name) } else { c.Errorf("%s has not failed when it should", expected.name) } } }