9
// TestName returns the current test name in the form "SuiteName.TestName"
10
func (c *C) TestName() string {
14
// -----------------------------------------------------------------------
15
// Basic succeeding/failing logic.
17
// Failed returns whether the currently running test has already failed.
18
func (c *C) Failed() bool {
19
return c.status() == failedSt
22
// Fail marks the currently running test as failed.
24
// Something ought to have been previously logged so the developer can tell
25
// what went wrong. The higher level helper functions will fail the test
26
// and do the logging properly.
31
// FailNow marks the currently running test as failed and stops running it.
32
// Something ought to have been previously logged so the developer can tell
33
// what went wrong. The higher level helper functions will fail the test
34
// and do the logging properly.
35
func (c *C) FailNow() {
40
// Succeed marks the currently running test as succeeded, undoing any
42
func (c *C) Succeed() {
43
c.setStatus(succeededSt)
46
// SucceedNow marks the currently running test as succeeded, undoing any
47
// previous failures, and stops running the test.
48
func (c *C) SucceedNow() {
53
// ExpectFailure informs that the running test is knowingly broken for
54
// the provided reason. If the test does not fail, an error will be reported
55
// to raise attention to this fact. This method is useful to temporarily
56
// disable tests which cover well known problems until a better time to
57
// fix the problem is found, without forgetting about the fact that a
58
// failure still exists.
59
func (c *C) ExpectFailure(reason string) {
61
panic("Missing reason why the test is expected to fail")
67
// Skip skips the running test for the provided reason. If run from within
68
// SetUpTest, the individual test being set up will be skipped, and if run
69
// from within SetUpSuite, the whole suite is skipped.
70
func (c *C) Skip(reason string) {
72
panic("Missing reason why the test is being skipped")
75
c.setStatus(skippedSt)
79
// -----------------------------------------------------------------------
82
// GetTestLog returns the current test error output.
83
func (c *C) GetTestLog() string {
84
return c.logb.String()
87
// Log logs some information into the test error output.
88
// The provided arguments are assembled together into a string with fmt.Sprint.
89
func (c *C) Log(args ...interface{}) {
93
// Log logs some information into the test error output.
94
// The provided arguments are assembled together into a string with fmt.Sprintf.
95
func (c *C) Logf(format string, args ...interface{}) {
96
c.logf(format, args...)
99
// Output enables *C to be used as a logger in functions that require only
100
// the minimum interface of *log.Logger.
101
func (c *C) Output(calldepth int, s string) error {
102
d := time.Now().Sub(c.startTime)
103
msec := d / time.Millisecond
104
sec := d / time.Second
105
min := d / time.Minute
107
c.Logf("[LOG] %d:%02d.%03d %s", min, sec%60, msec%1000, s)
111
// Error logs an error into the test error output and marks the test as failed.
112
// The provided arguments are assembled together into a string with fmt.Sprint.
113
func (c *C) Error(args ...interface{}) {
115
c.logString(fmt.Sprint("Error: ", fmt.Sprint(args...)))
120
// Errorf logs an error into the test error output and marks the test as failed.
121
// The provided arguments are assembled together into a string with fmt.Sprintf.
122
func (c *C) Errorf(format string, args ...interface{}) {
124
c.logString(fmt.Sprintf("Error: "+format, args...))
129
// Fatal logs an error into the test error output, marks the test as failed, and
130
// stops the test execution. The provided arguments are assembled together into
131
// a string with fmt.Sprint.
132
func (c *C) Fatal(args ...interface{}) {
134
c.logString(fmt.Sprint("Error: ", fmt.Sprint(args...)))
139
// Fatlaf logs an error into the test error output, marks the test as failed, and
140
// stops the test execution. The provided arguments are assembled together into
141
// a string with fmt.Sprintf.
142
func (c *C) Fatalf(format string, args ...interface{}) {
144
c.logString(fmt.Sprint("Error: ", fmt.Sprintf(format, args...)))
149
// -----------------------------------------------------------------------
150
// Generic checks and assertions based on checkers.
152
// Check verifies if the first value matches the expected value according
153
// to the provided checker. If they do not match, an error is logged, the
154
// test is marked as failed, and the test execution continues.
156
// Some checkers may not need the expected argument (e.g. IsNil).
158
// Extra arguments provided to the function are logged next to the reported
159
// problem when the matching fails.
160
func (c *C) Check(obtained interface{}, checker Checker, args ...interface{}) bool {
161
return c.internalCheck("Check", obtained, checker, args...)
164
// Assert ensures that the first value matches the expected value according
165
// to the provided checker. If they do not match, an error is logged, the
166
// test is marked as failed, and the test execution stops.
168
// Some checkers may not need the expected argument (e.g. IsNil).
170
// Extra arguments provided to the function are logged next to the reported
171
// problem when the matching fails.
172
func (c *C) Assert(obtained interface{}, checker Checker, args ...interface{}) {
173
if !c.internalCheck("Assert", obtained, checker, args...) {
178
func (c *C) internalCheck(funcName string, obtained interface{}, checker Checker, args ...interface{}) bool {
181
c.logString(fmt.Sprintf("%s(obtained, nil!?, ...):", funcName))
182
c.logString("Oops.. you've provided a nil checker!")
188
// If the last argument is a bug info, extract it out.
189
var comment CommentInterface
191
if c, ok := args[len(args)-1].(CommentInterface); ok {
193
args = args[:len(args)-1]
197
params := append([]interface{}{obtained}, args...)
198
info := checker.Info()
200
if len(params) != len(info.Params) {
201
names := append([]string{info.Params[0], info.Name}, info.Params[1:]...)
203
c.logString(fmt.Sprintf("%s(%s):", funcName, strings.Join(names, ", ")))
204
c.logString(fmt.Sprintf("Wrong number of parameters for %s: want %d, got %d", info.Name, len(names), len(params)+1))
210
// Copy since it may be mutated by Check.
211
names := append([]string{}, info.Params...)
213
// Do the actual check.
214
result, error := checker.Check(params, names)
215
if !result || error != "" {
217
for i := 0; i != len(params); i++ {
218
c.logValue(names[i], params[i])
221
c.logString(comment.CheckCommentString())