doc.go (2569B)
1 // Package suite contains logic for creating testing suite structs 2 // and running the methods on those structs as tests. The most useful 3 // piece of this package is that you can create setup/teardown methods 4 // on your testing suites, which will run before/after the whole suite 5 // or individual tests (depending on which interface(s) you 6 // implement). 7 // 8 // A testing suite is usually built by first extending the built-in 9 // suite functionality from suite.Suite in testify. Alternatively, 10 // you could reproduce that logic on your own if you wanted (you 11 // just need to implement the TestingSuite interface from 12 // suite/interfaces.go). 13 // 14 // After that, you can implement any of the interfaces in 15 // suite/interfaces.go to add setup/teardown functionality to your 16 // suite, and add any methods that start with "Test" to add tests. 17 // Methods that do not match any suite interfaces and do not begin 18 // with "Test" will not be run by testify, and can safely be used as 19 // helper methods. 20 // 21 // Once you've built your testing suite, you need to run the suite 22 // (using suite.Run from testify) inside any function that matches the 23 // identity that "go test" is already looking for (i.e. 24 // func(*testing.T)). 25 // 26 // Regular expression to select test suites specified command-line 27 // argument "-run". Regular expression to select the methods 28 // of test suites specified command-line argument "-m". 29 // Suite object has assertion methods. 30 // 31 // A crude example: 32 // 33 // // Basic imports 34 // import ( 35 // "testing" 36 // "github.com/stretchr/testify/assert" 37 // "github.com/stretchr/testify/suite" 38 // ) 39 // 40 // // Define the suite, and absorb the built-in basic suite 41 // // functionality from testify - including a T() method which 42 // // returns the current testing context 43 // type ExampleTestSuite struct { 44 // suite.Suite 45 // VariableThatShouldStartAtFive int 46 // } 47 // 48 // // Make sure that VariableThatShouldStartAtFive is set to five 49 // // before each test 50 // func (suite *ExampleTestSuite) SetupTest() { 51 // suite.VariableThatShouldStartAtFive = 5 52 // } 53 // 54 // // All methods that begin with "Test" are run as tests within a 55 // // suite. 56 // func (suite *ExampleTestSuite) TestExample() { 57 // assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive) 58 // suite.Equal(5, suite.VariableThatShouldStartAtFive) 59 // } 60 // 61 // // In order for 'go test' to run this suite, we need to create 62 // // a normal test function and pass our suite to suite.Run 63 // func TestExampleTestSuite(t *testing.T) { 64 // suite.Run(t, new(ExampleTestSuite)) 65 // } 66 package suite