123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- // Tests for the behavior of the test fixture system.
- package check_test
- import (
- . "gopkg.in/check.v1"
- )
- // -----------------------------------------------------------------------
- // Fixture test suite.
- type FixtureS struct{}
- var fixtureS = Suite(&FixtureS{})
- func (s *FixtureS) TestCountSuite(c *C) {
- suitesRun += 1
- }
- // -----------------------------------------------------------------------
- // Basic fixture ordering verification.
- func (s *FixtureS) TestOrder(c *C) {
- helper := FixtureHelper{}
- Run(&helper, nil)
- c.Check(helper.calls[0], Equals, "SetUpSuite")
- c.Check(helper.calls[1], Equals, "SetUpTest")
- c.Check(helper.calls[2], Equals, "Test1")
- c.Check(helper.calls[3], Equals, "TearDownTest")
- c.Check(helper.calls[4], Equals, "SetUpTest")
- c.Check(helper.calls[5], Equals, "Test2")
- c.Check(helper.calls[6], Equals, "TearDownTest")
- c.Check(helper.calls[7], Equals, "TearDownSuite")
- c.Check(len(helper.calls), Equals, 8)
- }
- // -----------------------------------------------------------------------
- // Check the behavior when panics occur within tests and fixtures.
- func (s *FixtureS) TestPanicOnTest(c *C) {
- helper := FixtureHelper{panicOn: "Test1"}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Check(helper.calls[0], Equals, "SetUpSuite")
- c.Check(helper.calls[1], Equals, "SetUpTest")
- c.Check(helper.calls[2], Equals, "Test1")
- c.Check(helper.calls[3], Equals, "TearDownTest")
- c.Check(helper.calls[4], Equals, "SetUpTest")
- c.Check(helper.calls[5], Equals, "Test2")
- c.Check(helper.calls[6], Equals, "TearDownTest")
- c.Check(helper.calls[7], Equals, "TearDownSuite")
- c.Check(len(helper.calls), Equals, 8)
- expected := "^\n-+\n" +
- "PANIC: check_test\\.go:[0-9]+: FixtureHelper.Test1\n\n" +
- "\\.\\.\\. Panic: Test1 \\(PC=[xA-F0-9]+\\)\n\n" +
- ".+:[0-9]+\n" +
- " in (go)?panic\n" +
- ".*check_test.go:[0-9]+\n" +
- " in FixtureHelper.trace\n" +
- ".*check_test.go:[0-9]+\n" +
- " in FixtureHelper.Test1\n" +
- "(.|\n)*$"
- c.Check(output.value, Matches, expected)
- }
- func (s *FixtureS) TestPanicOnSetUpTest(c *C) {
- helper := FixtureHelper{panicOn: "SetUpTest"}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Check(helper.calls[0], Equals, "SetUpSuite")
- c.Check(helper.calls[1], Equals, "SetUpTest")
- c.Check(helper.calls[2], Equals, "TearDownTest")
- c.Check(helper.calls[3], Equals, "TearDownSuite")
- c.Check(len(helper.calls), Equals, 4)
- expected := "^\n-+\n" +
- "PANIC: check_test\\.go:[0-9]+: " +
- "FixtureHelper\\.SetUpTest\n\n" +
- "\\.\\.\\. Panic: SetUpTest \\(PC=[xA-F0-9]+\\)\n\n" +
- ".+:[0-9]+\n" +
- " in (go)?panic\n" +
- ".*check_test.go:[0-9]+\n" +
- " in FixtureHelper.trace\n" +
- ".*check_test.go:[0-9]+\n" +
- " in FixtureHelper.SetUpTest\n" +
- "(.|\n)*" +
- "\n-+\n" +
- "PANIC: check_test\\.go:[0-9]+: " +
- "FixtureHelper\\.Test1\n\n" +
- "\\.\\.\\. Panic: Fixture has panicked " +
- "\\(see related PANIC\\)\n$"
- c.Check(output.value, Matches, expected)
- }
- func (s *FixtureS) TestPanicOnTearDownTest(c *C) {
- helper := FixtureHelper{panicOn: "TearDownTest"}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Check(helper.calls[0], Equals, "SetUpSuite")
- c.Check(helper.calls[1], Equals, "SetUpTest")
- c.Check(helper.calls[2], Equals, "Test1")
- c.Check(helper.calls[3], Equals, "TearDownTest")
- c.Check(helper.calls[4], Equals, "TearDownSuite")
- c.Check(len(helper.calls), Equals, 5)
- expected := "^\n-+\n" +
- "PANIC: check_test\\.go:[0-9]+: " +
- "FixtureHelper.TearDownTest\n\n" +
- "\\.\\.\\. Panic: TearDownTest \\(PC=[xA-F0-9]+\\)\n\n" +
- ".+:[0-9]+\n" +
- " in (go)?panic\n" +
- ".*check_test.go:[0-9]+\n" +
- " in FixtureHelper.trace\n" +
- ".*check_test.go:[0-9]+\n" +
- " in FixtureHelper.TearDownTest\n" +
- "(.|\n)*" +
- "\n-+\n" +
- "PANIC: check_test\\.go:[0-9]+: " +
- "FixtureHelper\\.Test1\n\n" +
- "\\.\\.\\. Panic: Fixture has panicked " +
- "\\(see related PANIC\\)\n$"
- c.Check(output.value, Matches, expected)
- }
- func (s *FixtureS) TestPanicOnSetUpSuite(c *C) {
- helper := FixtureHelper{panicOn: "SetUpSuite"}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Check(helper.calls[0], Equals, "SetUpSuite")
- c.Check(helper.calls[1], Equals, "TearDownSuite")
- c.Check(len(helper.calls), Equals, 2)
- expected := "^\n-+\n" +
- "PANIC: check_test\\.go:[0-9]+: " +
- "FixtureHelper.SetUpSuite\n\n" +
- "\\.\\.\\. Panic: SetUpSuite \\(PC=[xA-F0-9]+\\)\n\n" +
- ".+:[0-9]+\n" +
- " in (go)?panic\n" +
- ".*check_test.go:[0-9]+\n" +
- " in FixtureHelper.trace\n" +
- ".*check_test.go:[0-9]+\n" +
- " in FixtureHelper.SetUpSuite\n" +
- "(.|\n)*$"
- c.Check(output.value, Matches, expected)
- }
- func (s *FixtureS) TestPanicOnTearDownSuite(c *C) {
- helper := FixtureHelper{panicOn: "TearDownSuite"}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Check(helper.calls[0], Equals, "SetUpSuite")
- c.Check(helper.calls[1], Equals, "SetUpTest")
- c.Check(helper.calls[2], Equals, "Test1")
- c.Check(helper.calls[3], Equals, "TearDownTest")
- c.Check(helper.calls[4], Equals, "SetUpTest")
- c.Check(helper.calls[5], Equals, "Test2")
- c.Check(helper.calls[6], Equals, "TearDownTest")
- c.Check(helper.calls[7], Equals, "TearDownSuite")
- c.Check(len(helper.calls), Equals, 8)
- expected := "^\n-+\n" +
- "PANIC: check_test\\.go:[0-9]+: " +
- "FixtureHelper.TearDownSuite\n\n" +
- "\\.\\.\\. Panic: TearDownSuite \\(PC=[xA-F0-9]+\\)\n\n" +
- ".+:[0-9]+\n" +
- " in (go)?panic\n" +
- ".*check_test.go:[0-9]+\n" +
- " in FixtureHelper.trace\n" +
- ".*check_test.go:[0-9]+\n" +
- " in FixtureHelper.TearDownSuite\n" +
- "(.|\n)*$"
- c.Check(output.value, Matches, expected)
- }
- // -----------------------------------------------------------------------
- // A wrong argument on a test or fixture will produce a nice error.
- func (s *FixtureS) TestPanicOnWrongTestArg(c *C) {
- helper := WrongTestArgHelper{}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Check(helper.calls[0], Equals, "SetUpSuite")
- c.Check(helper.calls[1], Equals, "SetUpTest")
- c.Check(helper.calls[2], Equals, "TearDownTest")
- c.Check(helper.calls[3], Equals, "SetUpTest")
- c.Check(helper.calls[4], Equals, "Test2")
- c.Check(helper.calls[5], Equals, "TearDownTest")
- c.Check(helper.calls[6], Equals, "TearDownSuite")
- c.Check(len(helper.calls), Equals, 7)
- expected := "^\n-+\n" +
- "PANIC: fixture_test\\.go:[0-9]+: " +
- "WrongTestArgHelper\\.Test1\n\n" +
- "\\.\\.\\. Panic: WrongTestArgHelper\\.Test1 argument " +
- "should be \\*check\\.C\n"
- c.Check(output.value, Matches, expected)
- }
- func (s *FixtureS) TestPanicOnWrongSetUpTestArg(c *C) {
- helper := WrongSetUpTestArgHelper{}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Check(len(helper.calls), Equals, 0)
- expected :=
- "^\n-+\n" +
- "PANIC: fixture_test\\.go:[0-9]+: " +
- "WrongSetUpTestArgHelper\\.SetUpTest\n\n" +
- "\\.\\.\\. Panic: WrongSetUpTestArgHelper\\.SetUpTest argument " +
- "should be \\*check\\.C\n"
- c.Check(output.value, Matches, expected)
- }
- func (s *FixtureS) TestPanicOnWrongSetUpSuiteArg(c *C) {
- helper := WrongSetUpSuiteArgHelper{}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Check(len(helper.calls), Equals, 0)
- expected :=
- "^\n-+\n" +
- "PANIC: fixture_test\\.go:[0-9]+: " +
- "WrongSetUpSuiteArgHelper\\.SetUpSuite\n\n" +
- "\\.\\.\\. Panic: WrongSetUpSuiteArgHelper\\.SetUpSuite argument " +
- "should be \\*check\\.C\n"
- c.Check(output.value, Matches, expected)
- }
- // -----------------------------------------------------------------------
- // Nice errors also when tests or fixture have wrong arg count.
- func (s *FixtureS) TestPanicOnWrongTestArgCount(c *C) {
- helper := WrongTestArgCountHelper{}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Check(helper.calls[0], Equals, "SetUpSuite")
- c.Check(helper.calls[1], Equals, "SetUpTest")
- c.Check(helper.calls[2], Equals, "TearDownTest")
- c.Check(helper.calls[3], Equals, "SetUpTest")
- c.Check(helper.calls[4], Equals, "Test2")
- c.Check(helper.calls[5], Equals, "TearDownTest")
- c.Check(helper.calls[6], Equals, "TearDownSuite")
- c.Check(len(helper.calls), Equals, 7)
- expected := "^\n-+\n" +
- "PANIC: fixture_test\\.go:[0-9]+: " +
- "WrongTestArgCountHelper\\.Test1\n\n" +
- "\\.\\.\\. Panic: WrongTestArgCountHelper\\.Test1 argument " +
- "should be \\*check\\.C\n"
- c.Check(output.value, Matches, expected)
- }
- func (s *FixtureS) TestPanicOnWrongSetUpTestArgCount(c *C) {
- helper := WrongSetUpTestArgCountHelper{}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Check(len(helper.calls), Equals, 0)
- expected :=
- "^\n-+\n" +
- "PANIC: fixture_test\\.go:[0-9]+: " +
- "WrongSetUpTestArgCountHelper\\.SetUpTest\n\n" +
- "\\.\\.\\. Panic: WrongSetUpTestArgCountHelper\\.SetUpTest argument " +
- "should be \\*check\\.C\n"
- c.Check(output.value, Matches, expected)
- }
- func (s *FixtureS) TestPanicOnWrongSetUpSuiteArgCount(c *C) {
- helper := WrongSetUpSuiteArgCountHelper{}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Check(len(helper.calls), Equals, 0)
- expected :=
- "^\n-+\n" +
- "PANIC: fixture_test\\.go:[0-9]+: " +
- "WrongSetUpSuiteArgCountHelper\\.SetUpSuite\n\n" +
- "\\.\\.\\. Panic: WrongSetUpSuiteArgCountHelper" +
- "\\.SetUpSuite argument should be \\*check\\.C\n"
- c.Check(output.value, Matches, expected)
- }
- // -----------------------------------------------------------------------
- // Helper test suites with wrong function arguments.
- type WrongTestArgHelper struct {
- FixtureHelper
- }
- func (s *WrongTestArgHelper) Test1(t int) {
- }
- type WrongSetUpTestArgHelper struct {
- FixtureHelper
- }
- func (s *WrongSetUpTestArgHelper) SetUpTest(t int) {
- }
- type WrongSetUpSuiteArgHelper struct {
- FixtureHelper
- }
- func (s *WrongSetUpSuiteArgHelper) SetUpSuite(t int) {
- }
- type WrongTestArgCountHelper struct {
- FixtureHelper
- }
- func (s *WrongTestArgCountHelper) Test1(c *C, i int) {
- }
- type WrongSetUpTestArgCountHelper struct {
- FixtureHelper
- }
- func (s *WrongSetUpTestArgCountHelper) SetUpTest(c *C, i int) {
- }
- type WrongSetUpSuiteArgCountHelper struct {
- FixtureHelper
- }
- func (s *WrongSetUpSuiteArgCountHelper) SetUpSuite(c *C, i int) {
- }
- // -----------------------------------------------------------------------
- // Ensure fixture doesn't run without tests.
- type NoTestsHelper struct {
- hasRun bool
- }
- func (s *NoTestsHelper) SetUpSuite(c *C) {
- s.hasRun = true
- }
- func (s *NoTestsHelper) TearDownSuite(c *C) {
- s.hasRun = true
- }
- func (s *FixtureS) TestFixtureDoesntRunWithoutTests(c *C) {
- helper := NoTestsHelper{}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Check(helper.hasRun, Equals, false)
- }
- // -----------------------------------------------------------------------
- // Verify that checks and assertions work correctly inside the fixture.
- type FixtureCheckHelper struct {
- fail string
- completed bool
- }
- func (s *FixtureCheckHelper) SetUpSuite(c *C) {
- switch s.fail {
- case "SetUpSuiteAssert":
- c.Assert(false, Equals, true)
- case "SetUpSuiteCheck":
- c.Check(false, Equals, true)
- }
- s.completed = true
- }
- func (s *FixtureCheckHelper) SetUpTest(c *C) {
- switch s.fail {
- case "SetUpTestAssert":
- c.Assert(false, Equals, true)
- case "SetUpTestCheck":
- c.Check(false, Equals, true)
- }
- s.completed = true
- }
- func (s *FixtureCheckHelper) Test(c *C) {
- // Do nothing.
- }
- func (s *FixtureS) TestSetUpSuiteCheck(c *C) {
- helper := FixtureCheckHelper{fail: "SetUpSuiteCheck"}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Assert(output.value, Matches,
- "\n---+\n"+
- "FAIL: fixture_test\\.go:[0-9]+: "+
- "FixtureCheckHelper\\.SetUpSuite\n\n"+
- "fixture_test\\.go:[0-9]+:\n"+
- " c\\.Check\\(false, Equals, true\\)\n"+
- "\\.+ obtained bool = false\n"+
- "\\.+ expected bool = true\n\n")
- c.Assert(helper.completed, Equals, true)
- }
- func (s *FixtureS) TestSetUpSuiteAssert(c *C) {
- helper := FixtureCheckHelper{fail: "SetUpSuiteAssert"}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Assert(output.value, Matches,
- "\n---+\n"+
- "FAIL: fixture_test\\.go:[0-9]+: "+
- "FixtureCheckHelper\\.SetUpSuite\n\n"+
- "fixture_test\\.go:[0-9]+:\n"+
- " c\\.Assert\\(false, Equals, true\\)\n"+
- "\\.+ obtained bool = false\n"+
- "\\.+ expected bool = true\n\n")
- c.Assert(helper.completed, Equals, false)
- }
- // -----------------------------------------------------------------------
- // Verify that logging within SetUpTest() persists within the test log itself.
- type FixtureLogHelper struct {
- c *C
- }
- func (s *FixtureLogHelper) SetUpTest(c *C) {
- s.c = c
- c.Log("1")
- }
- func (s *FixtureLogHelper) Test(c *C) {
- c.Log("2")
- s.c.Log("3")
- c.Log("4")
- c.Fail()
- }
- func (s *FixtureLogHelper) TearDownTest(c *C) {
- s.c.Log("5")
- }
- func (s *FixtureS) TestFixtureLogging(c *C) {
- helper := FixtureLogHelper{}
- output := String{}
- Run(&helper, &RunConf{Output: &output})
- c.Assert(output.value, Matches,
- "\n---+\n"+
- "FAIL: fixture_test\\.go:[0-9]+: "+
- "FixtureLogHelper\\.Test\n\n"+
- "1\n2\n3\n4\n5\n")
- }
- // -----------------------------------------------------------------------
- // Skip() within fixture methods.
- func (s *FixtureS) TestSkipSuite(c *C) {
- helper := FixtureHelper{skip: true, skipOnN: 0}
- output := String{}
- result := Run(&helper, &RunConf{Output: &output})
- c.Assert(output.value, Equals, "")
- c.Assert(helper.calls[0], Equals, "SetUpSuite")
- c.Assert(helper.calls[1], Equals, "TearDownSuite")
- c.Assert(len(helper.calls), Equals, 2)
- c.Assert(result.Skipped, Equals, 2)
- }
- func (s *FixtureS) TestSkipTest(c *C) {
- helper := FixtureHelper{skip: true, skipOnN: 1}
- output := String{}
- result := Run(&helper, &RunConf{Output: &output})
- c.Assert(helper.calls[0], Equals, "SetUpSuite")
- c.Assert(helper.calls[1], Equals, "SetUpTest")
- c.Assert(helper.calls[2], Equals, "SetUpTest")
- c.Assert(helper.calls[3], Equals, "Test2")
- c.Assert(helper.calls[4], Equals, "TearDownTest")
- c.Assert(helper.calls[5], Equals, "TearDownSuite")
- c.Assert(len(helper.calls), Equals, 6)
- c.Assert(result.Skipped, Equals, 1)
- }
|