launchpad.net/gocheck@v0.0.0-20140225173054-000000000087/fixture_test.go (about)

     1  // Tests for the behavior of the test fixture system.
     2  
     3  package gocheck_test
     4  
     5  import (
     6  	. "launchpad.net/gocheck"
     7  )
     8  
     9  // -----------------------------------------------------------------------
    10  // Fixture test suite.
    11  
    12  type FixtureS struct{}
    13  
    14  var fixtureS = Suite(&FixtureS{})
    15  
    16  func (s *FixtureS) TestCountSuite(c *C) {
    17  	suitesRun += 1
    18  }
    19  
    20  // -----------------------------------------------------------------------
    21  // Basic fixture ordering verification.
    22  
    23  func (s *FixtureS) TestOrder(c *C) {
    24  	helper := FixtureHelper{}
    25  	Run(&helper, nil)
    26  	c.Check(helper.calls[0], Equals, "SetUpSuite")
    27  	c.Check(helper.calls[1], Equals, "SetUpTest")
    28  	c.Check(helper.calls[2], Equals, "Test1")
    29  	c.Check(helper.calls[3], Equals, "TearDownTest")
    30  	c.Check(helper.calls[4], Equals, "SetUpTest")
    31  	c.Check(helper.calls[5], Equals, "Test2")
    32  	c.Check(helper.calls[6], Equals, "TearDownTest")
    33  	c.Check(helper.calls[7], Equals, "TearDownSuite")
    34  	c.Check(len(helper.calls), Equals, 8)
    35  }
    36  
    37  // -----------------------------------------------------------------------
    38  // Check the behavior when panics occur within tests and fixtures.
    39  
    40  func (s *FixtureS) TestPanicOnTest(c *C) {
    41  	helper := FixtureHelper{panicOn: "Test1"}
    42  	output := String{}
    43  	Run(&helper, &RunConf{Output: &output})
    44  	c.Check(helper.calls[0], Equals, "SetUpSuite")
    45  	c.Check(helper.calls[1], Equals, "SetUpTest")
    46  	c.Check(helper.calls[2], Equals, "Test1")
    47  	c.Check(helper.calls[3], Equals, "TearDownTest")
    48  	c.Check(helper.calls[4], Equals, "SetUpTest")
    49  	c.Check(helper.calls[5], Equals, "Test2")
    50  	c.Check(helper.calls[6], Equals, "TearDownTest")
    51  	c.Check(helper.calls[7], Equals, "TearDownSuite")
    52  	c.Check(len(helper.calls), Equals, 8)
    53  
    54  	expected := "^\n-+\n" +
    55  		"PANIC: gocheck_test\\.go:[0-9]+: FixtureHelper.Test1\n\n" +
    56  		"\\.\\.\\. Panic: Test1 \\(PC=[xA-F0-9]+\\)\n\n" +
    57  		".+:[0-9]+\n" +
    58  		"  in panic\n" +
    59  		".*gocheck_test.go:[0-9]+\n" +
    60  		"  in FixtureHelper.trace\n" +
    61  		".*gocheck_test.go:[0-9]+\n" +
    62  		"  in FixtureHelper.Test1\n$"
    63  
    64  	c.Check(output.value, Matches, expected)
    65  }
    66  
    67  func (s *FixtureS) TestPanicOnSetUpTest(c *C) {
    68  	helper := FixtureHelper{panicOn: "SetUpTest"}
    69  	output := String{}
    70  	Run(&helper, &RunConf{Output: &output})
    71  	c.Check(helper.calls[0], Equals, "SetUpSuite")
    72  	c.Check(helper.calls[1], Equals, "SetUpTest")
    73  	c.Check(helper.calls[2], Equals, "TearDownTest")
    74  	c.Check(helper.calls[3], Equals, "TearDownSuite")
    75  	c.Check(len(helper.calls), Equals, 4)
    76  
    77  	expected := "^\n-+\n" +
    78  		"PANIC: gocheck_test\\.go:[0-9]+: " +
    79  		"FixtureHelper\\.SetUpTest\n\n" +
    80  		"\\.\\.\\. Panic: SetUpTest \\(PC=[xA-F0-9]+\\)\n\n" +
    81  		".+:[0-9]+\n" +
    82  		"  in panic\n" +
    83  		".*gocheck_test.go:[0-9]+\n" +
    84  		"  in FixtureHelper.trace\n" +
    85  		".*gocheck_test.go:[0-9]+\n" +
    86  		"  in FixtureHelper.SetUpTest\n" +
    87  		"\n-+\n" +
    88  		"PANIC: gocheck_test\\.go:[0-9]+: " +
    89  		"FixtureHelper\\.Test1\n\n" +
    90  		"\\.\\.\\. Panic: Fixture has panicked " +
    91  		"\\(see related PANIC\\)\n$"
    92  
    93  	c.Check(output.value, Matches, expected)
    94  }
    95  
    96  func (s *FixtureS) TestPanicOnTearDownTest(c *C) {
    97  	helper := FixtureHelper{panicOn: "TearDownTest"}
    98  	output := String{}
    99  	Run(&helper, &RunConf{Output: &output})
   100  	c.Check(helper.calls[0], Equals, "SetUpSuite")
   101  	c.Check(helper.calls[1], Equals, "SetUpTest")
   102  	c.Check(helper.calls[2], Equals, "Test1")
   103  	c.Check(helper.calls[3], Equals, "TearDownTest")
   104  	c.Check(helper.calls[4], Equals, "TearDownSuite")
   105  	c.Check(len(helper.calls), Equals, 5)
   106  
   107  	expected := "^\n-+\n" +
   108  		"PANIC: gocheck_test\\.go:[0-9]+: " +
   109  		"FixtureHelper.TearDownTest\n\n" +
   110  		"\\.\\.\\. Panic: TearDownTest \\(PC=[xA-F0-9]+\\)\n\n" +
   111  		".+:[0-9]+\n" +
   112  		"  in panic\n" +
   113  		".*gocheck_test.go:[0-9]+\n" +
   114  		"  in FixtureHelper.trace\n" +
   115  		".*gocheck_test.go:[0-9]+\n" +
   116  		"  in FixtureHelper.TearDownTest\n" +
   117  		"\n-+\n" +
   118  		"PANIC: gocheck_test\\.go:[0-9]+: " +
   119  		"FixtureHelper\\.Test1\n\n" +
   120  		"\\.\\.\\. Panic: Fixture has panicked " +
   121  		"\\(see related PANIC\\)\n$"
   122  
   123  	c.Check(output.value, Matches, expected)
   124  }
   125  
   126  func (s *FixtureS) TestPanicOnSetUpSuite(c *C) {
   127  	helper := FixtureHelper{panicOn: "SetUpSuite"}
   128  	output := String{}
   129  	Run(&helper, &RunConf{Output: &output})
   130  	c.Check(helper.calls[0], Equals, "SetUpSuite")
   131  	c.Check(helper.calls[1], Equals, "TearDownSuite")
   132  	c.Check(len(helper.calls), Equals, 2)
   133  
   134  	expected := "^\n-+\n" +
   135  		"PANIC: gocheck_test\\.go:[0-9]+: " +
   136  		"FixtureHelper.SetUpSuite\n\n" +
   137  		"\\.\\.\\. Panic: SetUpSuite \\(PC=[xA-F0-9]+\\)\n\n" +
   138  		".+:[0-9]+\n" +
   139  		"  in panic\n" +
   140  		".*gocheck_test.go:[0-9]+\n" +
   141  		"  in FixtureHelper.trace\n" +
   142  		".*gocheck_test.go:[0-9]+\n" +
   143  		"  in FixtureHelper.SetUpSuite\n$"
   144  
   145  	c.Check(output.value, Matches, expected)
   146  }
   147  
   148  func (s *FixtureS) TestPanicOnTearDownSuite(c *C) {
   149  	helper := FixtureHelper{panicOn: "TearDownSuite"}
   150  	output := String{}
   151  	Run(&helper, &RunConf{Output: &output})
   152  	c.Check(helper.calls[0], Equals, "SetUpSuite")
   153  	c.Check(helper.calls[1], Equals, "SetUpTest")
   154  	c.Check(helper.calls[2], Equals, "Test1")
   155  	c.Check(helper.calls[3], Equals, "TearDownTest")
   156  	c.Check(helper.calls[4], Equals, "SetUpTest")
   157  	c.Check(helper.calls[5], Equals, "Test2")
   158  	c.Check(helper.calls[6], Equals, "TearDownTest")
   159  	c.Check(helper.calls[7], Equals, "TearDownSuite")
   160  	c.Check(len(helper.calls), Equals, 8)
   161  
   162  	expected := "^\n-+\n" +
   163  		"PANIC: gocheck_test\\.go:[0-9]+: " +
   164  		"FixtureHelper.TearDownSuite\n\n" +
   165  		"\\.\\.\\. Panic: TearDownSuite \\(PC=[xA-F0-9]+\\)\n\n" +
   166  		".+:[0-9]+\n" +
   167  		"  in panic\n" +
   168  		".*gocheck_test.go:[0-9]+\n" +
   169  		"  in FixtureHelper.trace\n" +
   170  		".*gocheck_test.go:[0-9]+\n" +
   171  		"  in FixtureHelper.TearDownSuite\n$"
   172  
   173  	c.Check(output.value, Matches, expected)
   174  }
   175  
   176  // -----------------------------------------------------------------------
   177  // A wrong argument on a test or fixture will produce a nice error.
   178  
   179  func (s *FixtureS) TestPanicOnWrongTestArg(c *C) {
   180  	helper := WrongTestArgHelper{}
   181  	output := String{}
   182  	Run(&helper, &RunConf{Output: &output})
   183  	c.Check(helper.calls[0], Equals, "SetUpSuite")
   184  	c.Check(helper.calls[1], Equals, "SetUpTest")
   185  	c.Check(helper.calls[2], Equals, "TearDownTest")
   186  	c.Check(helper.calls[3], Equals, "SetUpTest")
   187  	c.Check(helper.calls[4], Equals, "Test2")
   188  	c.Check(helper.calls[5], Equals, "TearDownTest")
   189  	c.Check(helper.calls[6], Equals, "TearDownSuite")
   190  	c.Check(len(helper.calls), Equals, 7)
   191  
   192  	expected := "^\n-+\n" +
   193  		"PANIC: fixture_test\\.go:[0-9]+: " +
   194  		"WrongTestArgHelper\\.Test1\n\n" +
   195  		"\\.\\.\\. Panic: WrongTestArgHelper\\.Test1 argument " +
   196  		"should be \\*gocheck\\.C\n"
   197  
   198  	c.Check(output.value, Matches, expected)
   199  }
   200  
   201  func (s *FixtureS) TestPanicOnWrongSetUpTestArg(c *C) {
   202  	helper := WrongSetUpTestArgHelper{}
   203  	output := String{}
   204  	Run(&helper, &RunConf{Output: &output})
   205  	c.Check(len(helper.calls), Equals, 0)
   206  
   207  	expected :=
   208  		"^\n-+\n" +
   209  			"PANIC: fixture_test\\.go:[0-9]+: " +
   210  			"WrongSetUpTestArgHelper\\.SetUpTest\n\n" +
   211  			"\\.\\.\\. Panic: WrongSetUpTestArgHelper\\.SetUpTest argument " +
   212  			"should be \\*gocheck\\.C\n"
   213  
   214  	c.Check(output.value, Matches, expected)
   215  }
   216  
   217  func (s *FixtureS) TestPanicOnWrongSetUpSuiteArg(c *C) {
   218  	helper := WrongSetUpSuiteArgHelper{}
   219  	output := String{}
   220  	Run(&helper, &RunConf{Output: &output})
   221  	c.Check(len(helper.calls), Equals, 0)
   222  
   223  	expected :=
   224  		"^\n-+\n" +
   225  			"PANIC: fixture_test\\.go:[0-9]+: " +
   226  			"WrongSetUpSuiteArgHelper\\.SetUpSuite\n\n" +
   227  			"\\.\\.\\. Panic: WrongSetUpSuiteArgHelper\\.SetUpSuite argument " +
   228  			"should be \\*gocheck\\.C\n"
   229  
   230  	c.Check(output.value, Matches, expected)
   231  }
   232  
   233  // -----------------------------------------------------------------------
   234  // Nice errors also when tests or fixture have wrong arg count.
   235  
   236  func (s *FixtureS) TestPanicOnWrongTestArgCount(c *C) {
   237  	helper := WrongTestArgCountHelper{}
   238  	output := String{}
   239  	Run(&helper, &RunConf{Output: &output})
   240  	c.Check(helper.calls[0], Equals, "SetUpSuite")
   241  	c.Check(helper.calls[1], Equals, "SetUpTest")
   242  	c.Check(helper.calls[2], Equals, "TearDownTest")
   243  	c.Check(helper.calls[3], Equals, "SetUpTest")
   244  	c.Check(helper.calls[4], Equals, "Test2")
   245  	c.Check(helper.calls[5], Equals, "TearDownTest")
   246  	c.Check(helper.calls[6], Equals, "TearDownSuite")
   247  	c.Check(len(helper.calls), Equals, 7)
   248  
   249  	expected := "^\n-+\n" +
   250  		"PANIC: fixture_test\\.go:[0-9]+: " +
   251  		"WrongTestArgCountHelper\\.Test1\n\n" +
   252  		"\\.\\.\\. Panic: WrongTestArgCountHelper\\.Test1 argument " +
   253  		"should be \\*gocheck\\.C\n"
   254  
   255  	c.Check(output.value, Matches, expected)
   256  }
   257  
   258  func (s *FixtureS) TestPanicOnWrongSetUpTestArgCount(c *C) {
   259  	helper := WrongSetUpTestArgCountHelper{}
   260  	output := String{}
   261  	Run(&helper, &RunConf{Output: &output})
   262  	c.Check(len(helper.calls), Equals, 0)
   263  
   264  	expected :=
   265  		"^\n-+\n" +
   266  			"PANIC: fixture_test\\.go:[0-9]+: " +
   267  			"WrongSetUpTestArgCountHelper\\.SetUpTest\n\n" +
   268  			"\\.\\.\\. Panic: WrongSetUpTestArgCountHelper\\.SetUpTest argument " +
   269  			"should be \\*gocheck\\.C\n"
   270  
   271  	c.Check(output.value, Matches, expected)
   272  }
   273  
   274  func (s *FixtureS) TestPanicOnWrongSetUpSuiteArgCount(c *C) {
   275  	helper := WrongSetUpSuiteArgCountHelper{}
   276  	output := String{}
   277  	Run(&helper, &RunConf{Output: &output})
   278  	c.Check(len(helper.calls), Equals, 0)
   279  
   280  	expected :=
   281  		"^\n-+\n" +
   282  			"PANIC: fixture_test\\.go:[0-9]+: " +
   283  			"WrongSetUpSuiteArgCountHelper\\.SetUpSuite\n\n" +
   284  			"\\.\\.\\. Panic: WrongSetUpSuiteArgCountHelper" +
   285  			"\\.SetUpSuite argument should be \\*gocheck\\.C\n"
   286  
   287  	c.Check(output.value, Matches, expected)
   288  }
   289  
   290  // -----------------------------------------------------------------------
   291  // Helper test suites with wrong function arguments.
   292  
   293  type WrongTestArgHelper struct {
   294  	FixtureHelper
   295  }
   296  
   297  func (s *WrongTestArgHelper) Test1(t int) {
   298  }
   299  
   300  type WrongSetUpTestArgHelper struct {
   301  	FixtureHelper
   302  }
   303  
   304  func (s *WrongSetUpTestArgHelper) SetUpTest(t int) {
   305  }
   306  
   307  type WrongSetUpSuiteArgHelper struct {
   308  	FixtureHelper
   309  }
   310  
   311  func (s *WrongSetUpSuiteArgHelper) SetUpSuite(t int) {
   312  }
   313  
   314  type WrongTestArgCountHelper struct {
   315  	FixtureHelper
   316  }
   317  
   318  func (s *WrongTestArgCountHelper) Test1(c *C, i int) {
   319  }
   320  
   321  type WrongSetUpTestArgCountHelper struct {
   322  	FixtureHelper
   323  }
   324  
   325  func (s *WrongSetUpTestArgCountHelper) SetUpTest(c *C, i int) {
   326  }
   327  
   328  type WrongSetUpSuiteArgCountHelper struct {
   329  	FixtureHelper
   330  }
   331  
   332  func (s *WrongSetUpSuiteArgCountHelper) SetUpSuite(c *C, i int) {
   333  }
   334  
   335  // -----------------------------------------------------------------------
   336  // Ensure fixture doesn't run without tests.
   337  
   338  type NoTestsHelper struct {
   339  	hasRun bool
   340  }
   341  
   342  func (s *NoTestsHelper) SetUpSuite(c *C) {
   343  	s.hasRun = true
   344  }
   345  
   346  func (s *NoTestsHelper) TearDownSuite(c *C) {
   347  	s.hasRun = true
   348  }
   349  
   350  func (s *FixtureS) TestFixtureDoesntRunWithoutTests(c *C) {
   351  	helper := NoTestsHelper{}
   352  	output := String{}
   353  	Run(&helper, &RunConf{Output: &output})
   354  	c.Check(helper.hasRun, Equals, false)
   355  }
   356  
   357  // -----------------------------------------------------------------------
   358  // Verify that checks and assertions work correctly inside the fixture.
   359  
   360  type FixtureCheckHelper struct {
   361  	fail      string
   362  	completed bool
   363  }
   364  
   365  func (s *FixtureCheckHelper) SetUpSuite(c *C) {
   366  	switch s.fail {
   367  	case "SetUpSuiteAssert":
   368  		c.Assert(false, Equals, true)
   369  	case "SetUpSuiteCheck":
   370  		c.Check(false, Equals, true)
   371  	}
   372  	s.completed = true
   373  }
   374  
   375  func (s *FixtureCheckHelper) SetUpTest(c *C) {
   376  	switch s.fail {
   377  	case "SetUpTestAssert":
   378  		c.Assert(false, Equals, true)
   379  	case "SetUpTestCheck":
   380  		c.Check(false, Equals, true)
   381  	}
   382  	s.completed = true
   383  }
   384  
   385  func (s *FixtureCheckHelper) Test(c *C) {
   386  	// Do nothing.
   387  }
   388  
   389  func (s *FixtureS) TestSetUpSuiteCheck(c *C) {
   390  	helper := FixtureCheckHelper{fail: "SetUpSuiteCheck"}
   391  	output := String{}
   392  	Run(&helper, &RunConf{Output: &output})
   393  	c.Assert(output.value, Matches,
   394  		"\n---+\n"+
   395  			"FAIL: fixture_test\\.go:[0-9]+: "+
   396  			"FixtureCheckHelper\\.SetUpSuite\n\n"+
   397  			"fixture_test\\.go:[0-9]+:\n"+
   398  			"    c\\.Check\\(false, Equals, true\\)\n"+
   399  			"\\.+ obtained bool = false\n"+
   400  			"\\.+ expected bool = true\n\n")
   401  	c.Assert(helper.completed, Equals, true)
   402  }
   403  
   404  func (s *FixtureS) TestSetUpSuiteAssert(c *C) {
   405  	helper := FixtureCheckHelper{fail: "SetUpSuiteAssert"}
   406  	output := String{}
   407  	Run(&helper, &RunConf{Output: &output})
   408  	c.Assert(output.value, Matches,
   409  		"\n---+\n"+
   410  			"FAIL: fixture_test\\.go:[0-9]+: "+
   411  			"FixtureCheckHelper\\.SetUpSuite\n\n"+
   412  			"fixture_test\\.go:[0-9]+:\n"+
   413  			"    c\\.Assert\\(false, Equals, true\\)\n"+
   414  			"\\.+ obtained bool = false\n"+
   415  			"\\.+ expected bool = true\n\n")
   416  	c.Assert(helper.completed, Equals, false)
   417  }
   418  
   419  // -----------------------------------------------------------------------
   420  // Verify that logging within SetUpTest() persists within the test log itself.
   421  
   422  type FixtureLogHelper struct {
   423  	c *C
   424  }
   425  
   426  func (s *FixtureLogHelper) SetUpTest(c *C) {
   427  	s.c = c
   428  	c.Log("1")
   429  }
   430  
   431  func (s *FixtureLogHelper) Test(c *C) {
   432  	c.Log("2")
   433  	s.c.Log("3")
   434  	c.Log("4")
   435  	c.Fail()
   436  }
   437  
   438  func (s *FixtureLogHelper) TearDownTest(c *C) {
   439  	s.c.Log("5")
   440  }
   441  
   442  func (s *FixtureS) TestFixtureLogging(c *C) {
   443  	helper := FixtureLogHelper{}
   444  	output := String{}
   445  	Run(&helper, &RunConf{Output: &output})
   446  	c.Assert(output.value, Matches,
   447  		"\n---+\n"+
   448  			"FAIL: fixture_test\\.go:[0-9]+: "+
   449  			"FixtureLogHelper\\.Test\n\n"+
   450  			"1\n2\n3\n4\n5\n")
   451  }
   452  
   453  // -----------------------------------------------------------------------
   454  // Skip() within fixture methods.
   455  
   456  func (s *FixtureS) TestSkipSuite(c *C) {
   457  	helper := FixtureHelper{skip: true, skipOnN: 0}
   458  	output := String{}
   459  	result := Run(&helper, &RunConf{Output: &output})
   460  	c.Assert(output.value, Equals, "")
   461  	c.Assert(helper.calls[0], Equals, "SetUpSuite")
   462  	c.Assert(helper.calls[1], Equals, "TearDownSuite")
   463  	c.Assert(len(helper.calls), Equals, 2)
   464  	c.Assert(result.Skipped, Equals, 2)
   465  }
   466  
   467  func (s *FixtureS) TestSkipTest(c *C) {
   468  	helper := FixtureHelper{skip: true, skipOnN: 1}
   469  	output := String{}
   470  	result := Run(&helper, &RunConf{Output: &output})
   471  	c.Assert(helper.calls[0], Equals, "SetUpSuite")
   472  	c.Assert(helper.calls[1], Equals, "SetUpTest")
   473  	c.Assert(helper.calls[2], Equals, "SetUpTest")
   474  	c.Assert(helper.calls[3], Equals, "Test2")
   475  	c.Assert(helper.calls[4], Equals, "TearDownTest")
   476  	c.Assert(helper.calls[5], Equals, "TearDownSuite")
   477  	c.Assert(len(helper.calls), Equals, 6)
   478  	c.Assert(result.Skipped, Equals, 1)
   479  }