How to use NewTestingTB method of test Package

Best Go-testdeep code snippet using test.NewTestingTB

suite_test.go

Source:suite_test.go Github

copy

Full Screen

...207 }208 }209 })210 t.Run("ErrNil", func(t *testing.T) {211 tb := test.NewTestingTB("TestNil")212 tb.CatchFatal(func() { tdsuite.Run(tb, nil) })213 td.CmpTrue(t, tb.IsFatal)214 td.Cmp(t, tb.LastMessage(), "Run(): suite parameter cannot be nil")215 })216 t.Run("ErrNone", func(t *testing.T) {217 suite := ErrNone{}218 tb := test.NewTestingTB("TestErrNone")219 tb.CatchFatal(func() { tdsuite.Run(tb, suite) })220 td.CmpTrue(t, tb.IsFatal)221 td.Cmp(t, tb.LastMessage(), "Run(): no test methods found for type tdsuite_test.ErrNone")222 })223 t.Run("Full-no-ptr", func(t *testing.T) {224 suite := Full{}225 tb := test.NewTestingTB("Full-no-ptr")226 tb.CatchFatal(func() { tdsuite.Run(tb, suite) })227 td.CmpTrue(t, tb.IsFatal)228 td.Cmp(t, tb.Messages, []string{229 "Run(): several methods are not accessible as suite is not a pointer but tdsuite_test.Full: BetweenTests, Destroy, PostTest, PreTest, Setup, Test1, Test2, Test3",230 "Run(): no test methods found for type tdsuite_test.Full",231 })232 })233 t.Run("ErrOut1", func(t *testing.T) {234 suite := ErrOut1{}235 tb := test.NewTestingTB("TestErrOut1")236 tb.CatchFatal(func() { tdsuite.Run(tb, suite) })237 td.CmpTrue(t, tb.IsFatal)238 td.Cmp(t, tb.LastMessage(), "Run(): method tdsuite_test.ErrOut1.Test returns int value. Only bool or error are allowed")239 })240 t.Run("ErrOut2a", func(t *testing.T) {241 suite := ErrOut2a{}242 tb := test.NewTestingTB("TestErrOut2a")243 tb.CatchFatal(func() { tdsuite.Run(tb, suite) })244 td.CmpTrue(t, tb.IsFatal)245 td.Cmp(t, tb.LastMessage(), "Run(): method tdsuite_test.ErrOut2a.Test returns (bool, int) values. Only (bool, error) is allowed")246 })247 t.Run("ErrOut2b", func(t *testing.T) {248 suite := ErrOut2b{}249 tb := test.NewTestingTB("TestErrOut2b")250 tb.CatchFatal(func() { tdsuite.Run(tb, suite) })251 td.CmpTrue(t, tb.IsFatal)252 td.Cmp(t, tb.LastMessage(), "Run(): method tdsuite_test.ErrOut2b.Test returns (int, error) values. Only (bool, error) is allowed")253 })254 t.Run("ErrOut", func(t *testing.T) {255 suite := ErrOut{}256 tb := test.NewTestingTB("TestErrOut")257 tb.CatchFatal(func() { tdsuite.Run(tb, suite) })258 td.CmpTrue(t, tb.IsFatal)259 td.Cmp(t, tb.LastMessage(), "Run(): method tdsuite_test.ErrOut.Test returns 3 values. Only 0, 1 (bool or error) or 2 (bool, error) values are allowed")260 })261 t.Run("Skip", func(t *testing.T) {262 suite := Skip{}263 tb := test.NewTestingTB("TestSkip")264 tdsuite.Run(tb, &suite)265 test.IsFalse(t, tb.IsFatal)266 td.Cmp(t, suite.calls, []string{"TestOK"})267 const p = "Run(): method *tdsuite_test.Skip."268 td.Cmp(t, tb.Messages, []string{269 p + "Test1Param skipped, unrecognized parameter type int. Only *td.T allowed",270 p + "Test2ParamsA skipped, unrecognized parameters types (int, int). Only (*td.T, *td.T) allowed",271 p + "Test2ParamsB skipped, unrecognized first parameter type int. Only (*td.T, *td.T) allowed",272 p + "Test2ParamsC skipped, unrecognized second parameter type int. Only (*td.T, *td.T) allowed",273 p + "Test3Params skipped, too many parameters",274 p + "TestNoParams skipped, no input parameters",275 p + "TestVariadic skipped, variadic parameters not supported",276 "++++ TestOK", // (*T).Run() log as test.TestingTB has no Run() method277 })278 })279}280// Error allows to raise errors.281type Error struct {282 base283 setup bool284 destroy bool285 betweenTests bool286 preTest int287 postTest int288 testBool [2]bool289 testError [2]bool290 testBoolErrorBool [2]bool291 testBoolErrorErr [2]bool292}293func (e *Error) Setup(t *td.T) error {294 if e.setup {295 return errors.New("Setup error")296 }297 return nil298}299func (e *Error) PreTest(t *td.T, tn string) error {300 if e.preTest > 0 {301 e.preTest--302 if e.preTest == 0 {303 return errors.New("PreTest error")304 }305 }306 return nil307}308func (e *Error) PostTest(t *td.T, tn string) error {309 if e.postTest > 0 {310 e.postTest--311 if e.postTest == 0 {312 return errors.New("PostTest error")313 }314 }315 return nil316}317func (e *Error) BetweenTests(t *td.T, prev, next string) error {318 if e.betweenTests {319 return errors.New("BetweenTests error")320 }321 return nil322}323func (e *Error) Destroy(t *td.T) error {324 if e.destroy {325 return errors.New("Destroy error")326 }327 return nil328}329// 1 param methods.330func (e *Error) Test1Bool(t *td.T) bool {331 e.rec()332 return !e.testBool[0]333}334func (e *Error) Test1Error(t *td.T) error {335 e.rec()336 if e.testError[0] {337 return errors.New("Test1Error error")338 }339 return nil340}341func (e *Error) Test1BoolError(t *td.T) (b bool, err error) {342 e.rec()343 b = !e.testBoolErrorBool[0]344 if e.testBoolErrorErr[0] {345 err = errors.New("Test1BoolError error")346 }347 return348}349func (e *Error) Test1Z(t *td.T) { e.rec() }350// 2 params methods.351func (e *Error) Test2Bool(assert, require *td.T) bool {352 e.rec()353 return !e.testBool[1]354}355func (e *Error) Test2Error(assert, require *td.T) error {356 e.rec()357 if e.testError[1] {358 return errors.New("Test2Error error")359 }360 return nil361}362func (e *Error) Test2BoolError(assert, require *td.T) (b bool, err error) {363 e.rec()364 b = !e.testBoolErrorBool[1]365 if e.testBoolErrorErr[1] {366 err = errors.New("Test2BoolError error")367 }368 return369}370func (e *Error) Test2Z(assert, require *td.T) { e.rec() }371func TestRunErrors(t *testing.T) {372 t.Run("Setup", func(t *testing.T) {373 suite := Error{setup: true}374 tb := test.NewTestingTB("TestError")375 td.CmpFalse(t, tdsuite.Run(tb, &suite))376 td.CmpFalse(t, tb.IsFatal)377 td.Cmp(t, tb.Messages, []string{378 "*tdsuite_test.Error suite setup error: Setup error",379 })380 })381 t.Run("Destroy", func(t *testing.T) {382 suite := Error{destroy: true}383 tb := test.NewTestingTB("TestError")384 td.CmpFalse(t, tdsuite.Run(tb, &suite))385 td.CmpFalse(t, tb.IsFatal)386 td.Cmp(t, tb.Messages, []string{387 "++++ Test1Bool",388 "++++ Test1BoolError",389 "++++ Test1Error",390 "++++ Test1Z",391 //392 "++++ Test2Bool",393 "++++ Test2BoolError",394 "++++ Test2Error",395 "++++ Test2Z",396 "*tdsuite_test.Error suite destroy error: Destroy error",397 })398 })399 t.Run("PreTest", func(t *testing.T) {400 t.Run("1 param", func(t *testing.T) {401 suite := Error{preTest: 2}402 tb := test.NewTestingTB("TestError")403 td.CmpFalse(t, tdsuite.Run(tb, &suite))404 td.CmpFalse(t, tb.IsFatal)405 td.Cmp(t, tb.Messages, []string{406 "++++ Test1Bool",407 "++++ Test1BoolError",408 "Test1BoolError pre-test error: PreTest error",409 "++++ Test1Error",410 "++++ Test1Z",411 //412 "++++ Test2Bool",413 "++++ Test2BoolError",414 "++++ Test2Error",415 "++++ Test2Z",416 })417 })418 t.Run("2 params", func(t *testing.T) {419 suite := Error{preTest: 6}420 tb := test.NewTestingTB("TestError")421 td.CmpFalse(t, tdsuite.Run(tb, &suite))422 td.CmpFalse(t, tb.IsFatal)423 td.Cmp(t, tb.Messages, []string{424 "++++ Test1Bool",425 "++++ Test1BoolError",426 "++++ Test1Error",427 "++++ Test1Z",428 //429 "++++ Test2Bool",430 "++++ Test2BoolError",431 "Test2BoolError pre-test error: PreTest error",432 "++++ Test2Error",433 "++++ Test2Z",434 })435 })436 })437 t.Run("PostTest", func(t *testing.T) {438 t.Run("1 param", func(t *testing.T) {439 suite := Error{postTest: 3}440 tb := test.NewTestingTB("TestError")441 td.CmpFalse(t, tdsuite.Run(tb, &suite))442 td.CmpFalse(t, tb.IsFatal)443 td.Cmp(t, tb.Messages, []string{444 "++++ Test1Bool",445 "++++ Test1BoolError",446 "++++ Test1Error",447 "Test1Error post-test error: PostTest error",448 "++++ Test1Z",449 //450 "++++ Test2Bool",451 "++++ Test2BoolError",452 "++++ Test2Error",453 "++++ Test2Z",454 })455 })456 t.Run("2 params", func(t *testing.T) {457 suite := Error{postTest: 7}458 tb := test.NewTestingTB("TestError")459 td.CmpFalse(t, tdsuite.Run(tb, &suite))460 td.CmpFalse(t, tb.IsFatal)461 td.Cmp(t, tb.Messages, []string{462 "++++ Test1Bool",463 "++++ Test1BoolError",464 "++++ Test1Error",465 "++++ Test1Z",466 //467 "++++ Test2Bool",468 "++++ Test2BoolError",469 "++++ Test2Error",470 "Test2Error post-test error: PostTest error",471 "++++ Test2Z",472 })473 })474 })475 t.Run("BetweenTests", func(t *testing.T) {476 suite := Error{betweenTests: true}477 tb := test.NewTestingTB("TestError")478 td.CmpFalse(t, tdsuite.Run(tb, &suite))479 td.CmpFalse(t, tb.IsFatal)480 td.Cmp(t, tb.Messages, []string{481 "++++ Test1Bool",482 "Test1Bool / Test1BoolError between-tests error: BetweenTests error",483 })484 })485 t.Run("InvalidHooks", func(t *testing.T) {486 tb := test.NewTestingTB("TestError")487 td.CmpFalse(t, tdsuite.Run(tb, &FullBrokenHooks{}))488 td.CmpFalse(t, tb.IsFatal)489 name := "*tdsuite_test.FullBrokenHooks"490 td.Cmp(t, tb.Messages, []string{491 name + " suite has a Setup method but it does not match Setup(t *td.T) error",492 name + " suite has a Destroy method but it does not match Destroy(t *td.T) error",493 name + " suite has a PreTest method but it does not match PreTest(t *td.T, testName string) error",494 name + " suite has a PostTest method but it does not match PostTest(t *td.T, testName string) error",495 name + " suite has a BetweenTests method but it does not match BetweenTests(t *td.T, previousTestName, nextTestName string) error",496 "++++ Test1",497 })498 })499 t.Run("Stop_after_TestBool", func(t *testing.T) {500 t.Run("1 param", func(t *testing.T) {501 suite := Error{testBool: [2]bool{true, false}}502 tb := test.NewTestingTB("TestError")503 td.CmpTrue(t, tdsuite.Run(tb, &suite)) // returning false is not an error504 td.CmpFalse(t, tb.IsFatal)505 td.Cmp(t, tb.Messages, []string{506 "++++ Test1Bool",507 "Test1Bool required discontinuing suite tests",508 })509 })510 t.Run("2 params", func(t *testing.T) {511 suite := Error{testBool: [2]bool{false, true}}512 tb := test.NewTestingTB("TestError")513 td.CmpTrue(t, tdsuite.Run(tb, &suite)) // returning false is not an error514 td.CmpFalse(t, tb.IsFatal)515 td.Cmp(t, tb.Messages, []string{516 "++++ Test1Bool",517 "++++ Test1BoolError",518 "++++ Test1Error",519 "++++ Test1Z",520 //521 "++++ Test2Bool",522 "Test2Bool required discontinuing suite tests",523 })524 })525 })526 t.Run("TestBoolError", func(t *testing.T) {527 t.Run("Stop after", func(t *testing.T) {528 t.Run("1 param", func(t *testing.T) {529 suite := Error{testBoolErrorBool: [2]bool{true, false}}530 tb := test.NewTestingTB("TestError")531 td.CmpTrue(t, tdsuite.Run(tb, &suite)) // returning false is not an error532 td.CmpFalse(t, tb.IsFatal)533 td.Cmp(t, tb.Messages, []string{534 "++++ Test1Bool",535 "++++ Test1BoolError",536 "Test1BoolError required discontinuing suite tests",537 })538 })539 t.Run("2 params", func(t *testing.T) {540 suite := Error{testBoolErrorBool: [2]bool{false, true}}541 tb := test.NewTestingTB("TestError")542 td.CmpTrue(t, tdsuite.Run(tb, &suite)) // returning false is not an error543 td.CmpFalse(t, tb.IsFatal)544 td.Cmp(t, tb.Messages, []string{545 "++++ Test1Bool",546 "++++ Test1BoolError",547 "++++ Test1Error",548 "++++ Test1Z",549 //550 "++++ Test2Bool",551 "++++ Test2BoolError",552 "Test2BoolError required discontinuing suite tests",553 })554 })555 })556 t.Run("Error but continue", func(t *testing.T) {557 t.Run("1 param", func(t *testing.T) {558 suite := Error{testBoolErrorErr: [2]bool{true, false}}559 tb := test.NewTestingTB("TestError")560 td.CmpFalse(t, tdsuite.Run(tb, &suite))561 td.CmpFalse(t, tb.IsFatal)562 td.Cmp(t, tb.Messages, []string{563 "++++ Test1Bool",564 "++++ Test1BoolError",565 "Test1BoolError error: Test1BoolError error",566 "++++ Test1Error",567 "++++ Test1Z",568 //569 "++++ Test2Bool",570 "++++ Test2BoolError",571 "++++ Test2Error",572 "++++ Test2Z",573 })574 })575 t.Run("2 params", func(t *testing.T) {576 suite := Error{testBoolErrorErr: [2]bool{false, true}}577 tb := test.NewTestingTB("TestError")578 td.CmpFalse(t, tdsuite.Run(tb, &suite))579 td.CmpFalse(t, tb.IsFatal)580 td.Cmp(t, tb.Messages, []string{581 "++++ Test1Bool",582 "++++ Test1BoolError",583 "++++ Test1Error",584 "++++ Test1Z",585 //586 "++++ Test2Bool",587 "++++ Test2BoolError",588 "Test2BoolError error: Test2BoolError error",589 "++++ Test2Error",590 "++++ Test2Z",591 })592 })593 })594 t.Run("Error and stop after", func(t *testing.T) {595 t.Run("1 param", func(t *testing.T) {596 suite := Error{597 testBoolErrorBool: [2]bool{true, false},598 testBoolErrorErr: [2]bool{true, false},599 }600 tb := test.NewTestingTB("TestError")601 td.CmpFalse(t, tdsuite.Run(tb, &suite))602 td.CmpFalse(t, tb.IsFatal)603 td.Cmp(t, tb.Messages, []string{604 "++++ Test1Bool",605 "++++ Test1BoolError",606 "Test1BoolError error: Test1BoolError error",607 "Test1BoolError required discontinuing suite tests",608 })609 })610 t.Run("2 params", func(t *testing.T) {611 suite := Error{612 testBoolErrorBool: [2]bool{false, true},613 testBoolErrorErr: [2]bool{false, true},614 }615 tb := test.NewTestingTB("TestError")616 td.CmpFalse(t, tdsuite.Run(tb, &suite))617 td.CmpFalse(t, tb.IsFatal)618 td.Cmp(t, tb.Messages, []string{619 "++++ Test1Bool",620 "++++ Test1BoolError",621 "++++ Test1Error",622 "++++ Test1Z",623 //624 "++++ Test2Bool",625 "++++ Test2BoolError",626 "Test2BoolError error: Test2BoolError error",627 "Test2BoolError required discontinuing suite tests",628 })629 })630 })631 })632 t.Run("Error_for_TestError", func(t *testing.T) {633 t.Run("1 param", func(t *testing.T) {634 suite := Error{testError: [2]bool{true, false}}635 tb := test.NewTestingTB("TestError")636 td.CmpFalse(t, tdsuite.Run(tb, &suite))637 td.CmpFalse(t, tb.IsFatal)638 td.Cmp(t, tb.Messages, []string{639 "++++ Test1Bool",640 "++++ Test1BoolError",641 "++++ Test1Error",642 "Test1Error error: Test1Error error",643 "Test1Error required discontinuing suite tests",644 })645 })646 t.Run("2 params", func(t *testing.T) {647 suite := Error{testError: [2]bool{false, true}}648 tb := test.NewTestingTB("TestError")649 td.CmpFalse(t, tdsuite.Run(tb, &suite))650 td.CmpFalse(t, tb.IsFatal)651 td.Cmp(t, tb.Messages, []string{652 "++++ Test1Bool",653 "++++ Test1BoolError",654 "++++ Test1Error",655 "++++ Test1Z",656 //657 "++++ Test2Bool",658 "++++ Test2BoolError",659 "++++ Test2Error",660 "Test2Error error: Test2Error error",661 "Test2Error required discontinuing suite tests",662 })...

Full Screen

Full Screen

t_struct_test.go

Source:t_struct_test.go Github

copy

Full Screen

...63 })64 })65 //66 // Bad usages67 ttb := test.NewTestingTB("usage params")68 ttb.CatchFatal(func() {69 td.NewT(ttb, td.ContextConfig{}, td.ContextConfig{})70 })71 test.IsTrue(tt, ttb.IsFatal)72 test.IsTrue(tt, strings.Contains(ttb.Messages[0], "usage: NewT("))73 test.CheckPanic(tt, func() { td.NewT(nil) }, "usage: NewT")74}75func TestTCmp(tt *testing.T) {76 ttt := test.NewTestingTB(tt.Name())77 t := td.NewT(ttt)78 test.IsTrue(tt, t.Cmp(1, 1))79 test.IsFalse(tt, ttt.Failed())80 ttt = test.NewTestingTB(tt.Name())81 t = td.NewT(ttt)82 test.IsFalse(tt, t.Cmp(1, 2))83 test.IsTrue(tt, ttt.Failed())84}85func TestTCmpDeeply(tt *testing.T) {86 ttt := test.NewTestingTB(tt.Name())87 t := td.NewT(ttt)88 test.IsTrue(tt, t.CmpDeeply(1, 1))89 test.IsFalse(tt, ttt.Failed())90 ttt = test.NewTestingTB(tt.Name())91 t = td.NewT(ttt)92 test.IsFalse(tt, t.CmpDeeply(1, 2))93 test.IsTrue(tt, ttt.Failed())94}95func TestParallel(t *testing.T) {96 t.Run("without Parallel", func(tt *testing.T) {97 ttt := test.NewTestingTB(tt.Name())98 t := td.NewT(ttt)99 t.Parallel()100 // has no effect101 })102 t.Run("with Parallel", func(tt *testing.T) {103 ttt := test.NewParallelTestingTB(tt.Name())104 t := td.NewT(ttt)105 t.Parallel()106 test.IsTrue(tt, ttt.IsParallel)107 })108 t.Run("Run with Parallel", func(tt *testing.T) {109 // This test verifies that subtests with t.Parallel() are run110 // in parallel. We use a WaitGroup to make both subtests block111 // until they're both ready. This test will block forever if112 // the tests are not run together.113 var ready sync.WaitGroup114 ready.Add(2)115 t := td.NewT(tt)116 t.Run("level 1", func(t *td.T) {117 t.Parallel()118 ready.Done() // I'm ready.119 ready.Wait() // Are you?120 })121 t.Run("level 2", func(t *td.T) {122 t.Parallel()123 ready.Done() // I'm ready.124 ready.Wait() // Are you?125 })126 })127}128func TestRun(t *testing.T) {129 t.Run("test.TB with Run", func(tt *testing.T) {130 t := td.NewT(tt)131 runPassed := false132 nestedFailureIsFatal := false133 ok := t.Run("Test level1",134 func(t *td.T) {135 ok := t.FailureIsFatal().Run("Test level2",136 func(t *td.T) {137 runPassed = t.True(true) // test succeeds!138 // Check we inherit config from caller139 nestedFailureIsFatal = t.Config.FailureIsFatal140 })141 t.True(ok)142 })143 test.IsTrue(tt, ok)144 test.IsTrue(tt, runPassed)145 test.IsTrue(tt, nestedFailureIsFatal)146 })147 t.Run("test.TB without Run", func(tt *testing.T) {148 t := td.NewT(test.NewTestingTB("gg"))149 runPassed := false150 ok := t.Run("Test level1",151 func(t *td.T) {152 ok := t.Run("Test level2",153 func(t *td.T) {154 runPassed = t.True(true) // test succeeds!155 })156 t.True(ok)157 })158 t.True(ok)159 t.True(runPassed)160 })161}162func TestRunAssertRequire(t *testing.T) {163 t.Run("test.TB with Run", func(tt *testing.T) {164 t := td.NewT(tt)165 runPassed := false166 assertIsFatal := true167 requireIsFatal := false168 ok := t.RunAssertRequire("Test level1",169 func(assert, require *td.T) {170 assertIsFatal = assert.Config.FailureIsFatal171 requireIsFatal = require.Config.FailureIsFatal172 ok := assert.RunAssertRequire("Test level2",173 func(assert, require *td.T) {174 runPassed = assert.True(true) // test succeeds!175 runPassed = runPassed && require.True(true) // test succeeds!176 assertIsFatal = assertIsFatal || assert.Config.FailureIsFatal177 requireIsFatal = requireIsFatal && require.Config.FailureIsFatal178 })179 assert.True(ok)180 require.True(ok)181 ok = require.RunAssertRequire("Test level2",182 func(assert, require *td.T) {183 runPassed = runPassed && assert.True(true) // test succeeds!184 runPassed = runPassed && require.True(true) // test succeeds!185 assertIsFatal = assertIsFatal || assert.Config.FailureIsFatal186 requireIsFatal = requireIsFatal && require.Config.FailureIsFatal187 })188 assert.True(ok)189 require.True(ok)190 })191 test.IsTrue(tt, ok)192 test.IsTrue(tt, runPassed)193 test.IsFalse(tt, assertIsFatal)194 test.IsTrue(tt, requireIsFatal)195 })196 t.Run("test.TB without Run", func(tt *testing.T) {197 t := td.NewT(test.NewTestingTB("gg"))198 runPassed := false199 assertIsFatal := true200 requireIsFatal := false201 ok := t.RunAssertRequire("Test level1",202 func(assert, require *td.T) {203 assertIsFatal = assert.Config.FailureIsFatal204 requireIsFatal = require.Config.FailureIsFatal205 ok := assert.RunAssertRequire("Test level2",206 func(assert, require *td.T) {207 runPassed = assert.True(true) // test succeeds!208 runPassed = runPassed && require.True(true) // test succeeds!209 assertIsFatal = assertIsFatal || assert.Config.FailureIsFatal210 requireIsFatal = requireIsFatal && require.Config.FailureIsFatal211 })212 assert.True(ok)213 require.True(ok)214 ok = require.RunAssertRequire("Test level2",215 func(assert, require *td.T) {216 runPassed = runPassed && assert.True(true) // test succeeds!217 runPassed = runPassed && require.True(true) // test succeeds!218 assertIsFatal = assertIsFatal || assert.Config.FailureIsFatal219 requireIsFatal = requireIsFatal && require.Config.FailureIsFatal220 })221 assert.True(ok)222 require.True(ok)223 })224 test.IsTrue(tt, ok)225 test.IsTrue(tt, runPassed)226 test.IsFalse(tt, assertIsFatal)227 test.IsTrue(tt, requireIsFatal)228 })229}230// Deprecated RunT.231func TestRunT(t *testing.T) {232 t.Run("test.TB with Run", func(tt *testing.T) {233 t := td.NewT(tt)234 runPassed := false235 ok := t.RunT("Test level1", //nolint: staticcheck236 func(t *td.T) {237 ok := t.RunT("Test level2", //nolint: staticcheck238 func(t *td.T) {239 runPassed = t.True(true) // test succeeds!240 })241 t.True(ok)242 })243 test.IsTrue(tt, ok)244 test.IsTrue(tt, runPassed)245 })246 t.Run("test.TB without Run", func(tt *testing.T) {247 t := td.NewT(test.NewTestingTB("gg"))248 runPassed := false249 ok := t.RunT("Test level1", //nolint: staticcheck250 func(t *td.T) {251 ok := t.RunT("Test level2", //nolint: staticcheck252 func(t *td.T) {253 runPassed = t.True(true) // test succeeds!254 })255 t.True(ok)256 })257 test.IsTrue(tt, ok)258 test.IsTrue(tt, runPassed)259 })260}261func TestFailureIsFatal(tt *testing.T) {262 // All t.True(false) tests of course fail263 // Using default config264 ttt := test.NewTestingTB(tt.Name())265 t := td.NewT(ttt)266 t.True(false) // failure267 test.IsTrue(tt, ttt.LastMessage() != "")268 test.IsFalse(tt, ttt.IsFatal, "by default it is not fatal")269 // Using specific config270 ttt = test.NewTestingTB(tt.Name())271 t = td.NewT(ttt, td.ContextConfig{FailureIsFatal: true})272 ttt.CatchFatal(func() { t.True(false) }) // failure273 test.IsTrue(tt, ttt.LastMessage() != "")274 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")275 // Using FailureIsFatal()276 ttt = test.NewTestingTB(tt.Name())277 t = td.NewT(ttt).FailureIsFatal()278 ttt.CatchFatal(func() { t.True(false) }) // failure279 test.IsTrue(tt, ttt.LastMessage() != "")280 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")281 // Using FailureIsFatal(true)282 ttt = test.NewTestingTB(tt.Name())283 t = td.NewT(ttt).FailureIsFatal(true)284 ttt.CatchFatal(func() { t.True(false) }) // failure285 test.IsTrue(tt, ttt.LastMessage() != "")286 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")287 // Using T.Assert()288 ttt = test.NewTestingTB(tt.Name())289 t = td.NewT(ttt, td.ContextConfig{FailureIsFatal: true}).Assert()290 t.True(false) // failure291 test.IsTrue(tt, ttt.LastMessage() != "")292 test.IsFalse(tt, ttt.IsFatal, "by default it is not fatal")293 // Using T.Require()294 ttt = test.NewTestingTB(tt.Name())295 t = td.NewT(ttt).Require()296 ttt.CatchFatal(func() { t.True(false) }) // failure297 test.IsTrue(tt, ttt.LastMessage() != "")298 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")299 // Using Require()300 ttt = test.NewTestingTB(tt.Name())301 t = td.Require(ttt)302 ttt.CatchFatal(func() { t.True(false) }) // failure303 test.IsTrue(tt, ttt.LastMessage() != "")304 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")305 // Using Require() with specific config (cannot override FailureIsFatal)306 ttt = test.NewTestingTB(tt.Name())307 t = td.Require(ttt, td.ContextConfig{FailureIsFatal: false})308 ttt.CatchFatal(func() { t.True(false) }) // failure309 test.IsTrue(tt, ttt.LastMessage() != "")310 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")311 // Canceling specific config312 ttt = test.NewTestingTB(tt.Name())313 t = td.NewT(ttt, td.ContextConfig{FailureIsFatal: false}).314 FailureIsFatal(false)315 t.True(false) // failure316 test.IsTrue(tt, ttt.LastMessage() != "")317 test.IsFalse(tt, ttt.IsFatal, "it must be not fatal")318 // Using Assert()319 ttt = test.NewTestingTB(tt.Name())320 t = td.Assert(ttt)321 t.True(false) // failure322 test.IsTrue(tt, ttt.LastMessage() != "")323 test.IsFalse(tt, ttt.IsFatal, "it must be not fatal")324 // Using Assert() with specific config (cannot override FailureIsFatal)325 ttt = test.NewTestingTB(tt.Name())326 t = td.Assert(ttt, td.ContextConfig{FailureIsFatal: true})327 t.True(false) // failure328 test.IsTrue(tt, ttt.LastMessage() != "")329 test.IsFalse(tt, ttt.IsFatal, "it must be not fatal")330 // AssertRequire() / assert331 ttt = test.NewTestingTB(tt.Name())332 t, _ = td.AssertRequire(ttt)333 t.True(false) // failure334 test.IsTrue(tt, ttt.LastMessage() != "")335 test.IsFalse(tt, ttt.IsFatal, "it must be not fatal")336 // Using AssertRequire() / assert with specific config (cannot337 // override FailureIsFatal)338 ttt = test.NewTestingTB(tt.Name())339 t, _ = td.AssertRequire(ttt, td.ContextConfig{FailureIsFatal: true})340 t.True(false) // failure341 test.IsTrue(tt, ttt.LastMessage() != "")342 test.IsFalse(tt, ttt.IsFatal, "it must be not fatal")343 // AssertRequire() / require344 ttt = test.NewTestingTB(tt.Name())345 _, t = td.AssertRequire(ttt)346 ttt.CatchFatal(func() { t.True(false) }) // failure347 test.IsTrue(tt, ttt.LastMessage() != "")348 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")349 // Using AssertRequire() / require with specific config (cannot350 // override FailureIsFatal)351 ttt = test.NewTestingTB(tt.Name())352 _, t = td.AssertRequire(ttt, td.ContextConfig{FailureIsFatal: true})353 ttt.CatchFatal(func() { t.True(false) }) // failure354 test.IsTrue(tt, ttt.LastMessage() != "")355 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")356}357func TestUseEqual(tt *testing.T) {358 ttt := test.NewTestingTB(tt.Name())359 var time1, time2 time.Time360 for {361 time1 = time.Now()362 time2 = time1.Truncate(0)363 if !time1.Equal(time2) {364 tt.Fatal("time.Equal() does not work as expected")365 }366 if time1 != time2 { // to avoid the bad luck case where time1.wall=0367 break368 }369 }370 // Using default config371 t := td.NewT(ttt)372 test.IsFalse(tt, t.Cmp(time1, time2))373 // UseEqual374 t = td.NewT(ttt).UseEqual() // enable globally375 test.IsTrue(tt, t.Cmp(time1, time2))376 t = td.NewT(ttt).UseEqual(true) // enable globally377 test.IsTrue(tt, t.Cmp(time1, time2))378 t = td.NewT(ttt).UseEqual(false) // disable globally379 test.IsFalse(tt, t.Cmp(time1, time2))380 t = td.NewT(ttt).UseEqual(time.Time{}) // enable only for time.Time381 test.IsTrue(tt, t.Cmp(time1, time2))382 t = t.UseEqual().UseEqual(false) // enable then disable globally383 test.IsTrue(tt, t.Cmp(time1, time2)) // Equal() still used384 test.EqualStr(tt,385 ttt.CatchFatal(func() { td.NewT(ttt).UseEqual(42) }),386 "UseEqual expects type int owns an Equal method (@0)")387}388func TestBeLax(tt *testing.T) {389 ttt := test.NewTestingTB(tt.Name())390 // Using default config391 t := td.NewT(ttt)392 test.IsFalse(tt, t.Cmp(int64(123), 123))393 // BeLax394 t = td.NewT(ttt).BeLax()395 test.IsTrue(tt, t.Cmp(int64(123), 123))396 t = td.NewT(ttt).BeLax(true)397 test.IsTrue(tt, t.Cmp(int64(123), 123))398 t = td.NewT(ttt).BeLax(false)399 test.IsFalse(tt, t.Cmp(int64(123), 123))400}401func TestIgnoreUnexported(tt *testing.T) {402 ttt := test.NewTestingTB(tt.Name())403 type SType1 struct {404 Public int405 private string406 }407 a1, b1 := SType1{Public: 42, private: "test"}, SType1{Public: 42}408 type SType2 struct {409 Public int410 private string411 }412 a2, b2 := SType2{Public: 42, private: "test"}, SType2{Public: 42}413 // Using default config414 t := td.NewT(ttt)415 test.IsFalse(tt, t.Cmp(a1, b1))416 // IgnoreUnexported417 t = td.NewT(ttt).IgnoreUnexported() // ignore unexported globally418 test.IsTrue(tt, t.Cmp(a1, b1))419 test.IsTrue(tt, t.Cmp(a2, b2))420 t = td.NewT(ttt).IgnoreUnexported(true) // ignore unexported globally421 test.IsTrue(tt, t.Cmp(a1, b1))422 test.IsTrue(tt, t.Cmp(a2, b2))423 t = td.NewT(ttt).IgnoreUnexported(false) // handle unexported globally424 test.IsFalse(tt, t.Cmp(a1, b1))425 test.IsFalse(tt, t.Cmp(a2, b2))426 t = td.NewT(ttt).IgnoreUnexported(SType1{}) // ignore only for SType1427 test.IsTrue(tt, t.Cmp(a1, b1))428 test.IsFalse(tt, t.Cmp(a2, b2))429 t = t.UseEqual().UseEqual(false) // enable then disable globally430 test.IsTrue(tt, t.Cmp(a1, b1))431 test.IsFalse(tt, t.Cmp(a2, b2))432 t = td.NewT(ttt).IgnoreUnexported(SType1{}, SType2{}) // enable for both433 test.IsTrue(tt, t.Cmp(a1, b1))434 test.IsTrue(tt, t.Cmp(a2, b2))435 test.EqualStr(tt,436 ttt.CatchFatal(func() { td.NewT(ttt).IgnoreUnexported(42) }),437 "IgnoreUnexported expects type int be a struct, not a int (@0)")438}439func TestLogTrace(tt *testing.T) {440 ttt := test.NewTestingTB(tt.Name())441 t := td.NewT(ttt)442//line /t_struct_test.go:100443 t.LogTrace()444 test.EqualStr(tt, ttt.LastMessage(), `Stack trace:445 TestLogTrace() /t_struct_test.go:100`)446 test.IsFalse(tt, ttt.HasFailed)447 test.IsFalse(tt, ttt.IsFatal)448 ttt.ResetMessages()449//line /t_struct_test.go:110450 t.LogTrace("This is the %s:", "stack")451 test.EqualStr(tt, ttt.LastMessage(), `This is the stack:452 TestLogTrace() /t_struct_test.go:110`)453 ttt.ResetMessages()454//line /t_struct_test.go:120455 t.LogTrace("This is the %s:\n", "stack")456 test.EqualStr(tt, ttt.LastMessage(), `This is the stack:457 TestLogTrace() /t_struct_test.go:120`)458 ttt.ResetMessages()459//line /t_struct_test.go:130460 t.LogTrace("This is the ", "stack")461 test.EqualStr(tt, ttt.LastMessage(), `This is the stack462 TestLogTrace() /t_struct_test.go:130`)463 ttt.ResetMessages()464 trace.IgnorePackage()465 defer trace.UnignorePackage()466//line /t_struct_test.go:140467 t.LogTrace("Stack:\n")468 test.EqualStr(tt, ttt.LastMessage(), `Stack:469 Empty stack trace`)470}471func TestErrorTrace(tt *testing.T) {472 ttt := test.NewTestingTB(tt.Name())473 t := td.NewT(ttt)474//line /t_struct_test.go:200475 t.ErrorTrace()476 test.EqualStr(tt, ttt.LastMessage(), `Stack trace:477 TestErrorTrace() /t_struct_test.go:200`)478 test.IsTrue(tt, ttt.HasFailed)479 test.IsFalse(tt, ttt.IsFatal)480 ttt.ResetMessages()481//line /t_struct_test.go:210482 t.ErrorTrace("This is the %s:", "stack")483 test.EqualStr(tt, ttt.LastMessage(), `This is the stack:484 TestErrorTrace() /t_struct_test.go:210`)485 ttt.ResetMessages()486//line /t_struct_test.go:220487 t.ErrorTrace("This is the %s:\n", "stack")488 test.EqualStr(tt, ttt.LastMessage(), `This is the stack:489 TestErrorTrace() /t_struct_test.go:220`)490 ttt.ResetMessages()491//line /t_struct_test.go:230492 t.ErrorTrace("This is the ", "stack")493 test.EqualStr(tt, ttt.LastMessage(), `This is the stack494 TestErrorTrace() /t_struct_test.go:230`)495 ttt.ResetMessages()496 trace.IgnorePackage()497 defer trace.UnignorePackage()498//line /t_struct_test.go:240499 t.ErrorTrace("Stack:\n")500 test.EqualStr(tt, ttt.LastMessage(), `Stack:501 Empty stack trace`)502}503func TestFatalTrace(tt *testing.T) {504 ttt := test.NewTestingTB(tt.Name())505 t := td.NewT(ttt)506 match := func(got, expectedRe string) {507 tt.Helper()508 re := regexp.MustCompile(expectedRe)509 if !re.MatchString(got) {510 test.EqualErrorMessage(tt, got, expectedRe)511 }512 }513//line /t_struct_test.go:300514 match(ttt.CatchFatal(func() { t.FatalTrace() }), `Stack trace:515 TestFatalTrace\.func\d\(\) /t_struct_test\.go:300516 \(\*TestingT\)\.CatchFatal\(\) internal/test/types\.go:\d+517 TestFatalTrace\(\) /t_struct_test\.go:300`)518 test.IsTrue(tt, ttt.HasFailed)...

Full Screen

Full Screen

t_hooks_test.go

Source:t_hooks_test.go Github

copy

Full Screen

...65 expected: -1,66 },67 } {68 tt.Run(tst.name, func(tt *testing.T) {69 ttt := test.NewTestingTB(tt.Name())70 t := td.NewT(ttt)71 td.CmpFalse(tt, func() bool {72 // A panic can occur when -tags safe:73 // dark.GetInterface() does not handle private unsafe.Pointer kind74 defer func() { recover() }() //nolint: errcheck75 return t.Cmp(tst.got, tst.expected)76 }())77 t = t.WithCmpHooks(tst.cmp)78 td.CmpTrue(tt, t.Cmp(tst.got, tst.expected))79 })80 }81 tt.Run("Error", func(tt *testing.T) {82 ttt := test.NewTestingTB(tt.Name())83 t := td.NewT(ttt).84 WithCmpHooks(func(got, expected int) error {85 return errors.New("never equal")86 })87 td.CmpFalse(tt, t.Cmp(1, 1))88 if !strings.Contains(ttt.LastMessage(), "DATA: never equal\n") {89 tt.Errorf(`<%s> does not contain "DATA: never equal\n"`, ttt.LastMessage())90 }91 })92 for _, tst := range []struct {93 name string94 cmp any95 fatal string96 }{97 {98 name: "not a function",99 cmp: "Booh",100 fatal: "WithCmpHooks expects a function, not a string",101 },102 {103 name: "wrong signature",104 cmp: func(a []int, b ...int) bool { return false },105 fatal: "WithCmpHooks expects: func (T, T) bool|error not ",106 },107 } {108 tt.Run("panic: "+tst.name, func(tt *testing.T) {109 ttt := test.NewTestingTB(tt.Name())110 t := td.NewT(ttt)111 fatalMesg := ttt.CatchFatal(func() { t.WithCmpHooks(tst.cmp) })112 test.IsTrue(tt, ttt.IsFatal)113 if !strings.Contains(fatalMesg, tst.fatal) {114 tt.Errorf(`<%s> does not contain %q`, fatalMesg, tst.fatal)115 }116 })117 }118}119func TestWithSmuggleHooks(tt *testing.T) {120 for _, tst := range []struct {121 name string122 cmp any123 got, expected any124 }{125 {126 name: "abs",127 cmp: func(got int) int {128 if got < 0 {129 return -got130 }131 return got132 },133 got: -1234,134 expected: 1234,135 },136 {137 name: "int2bool",138 cmp: func(got int) bool { return got != 0 },139 got: 1,140 expected: true,141 },142 {143 name: "Atoi",144 cmp: strconv.Atoi,145 got: "1234",146 expected: 1234,147 },148 } {149 tt.Run(tst.name, func(tt *testing.T) {150 ttt := test.NewTestingTB(tt.Name())151 t := td.NewT(ttt)152 td.CmpFalse(tt, t.Cmp(tst.got, tst.expected))153 t = t.WithSmuggleHooks(tst.cmp)154 td.CmpTrue(tt, t.Cmp(tst.got, tst.expected))155 })156 }157 tt.Run("Error", func(tt *testing.T) {158 ttt := test.NewTestingTB(tt.Name())159 t := td.NewT(ttt).WithSmuggleHooks(func(got int) (int, error) {160 return 0, errors.New("never equal")161 })162 td.CmpFalse(tt, t.Cmp(1, 1))163 if !strings.Contains(ttt.LastMessage(), "DATA: never equal\n") {164 tt.Errorf(`<%s> does not contain "DATA: never equal\n"`, ttt.LastMessage())165 }166 })167 for _, tst := range []struct {168 name string169 cmp any170 fatal string171 }{172 {173 name: "not a function",174 cmp: "Booh",175 fatal: "WithSmuggleHooks expects a function, not a string",176 },177 {178 name: "wrong signature",179 cmp: func(a []int, b ...int) bool { return false },180 fatal: "WithSmuggleHooks expects: func (A) (B[, error]) not ",181 },182 } {183 tt.Run("panic: "+tst.name, func(tt *testing.T) {184 ttt := test.NewTestingTB(tt.Name())185 t := td.NewT(ttt)186 fatalMesg := ttt.CatchFatal(func() { t.WithSmuggleHooks(tst.cmp) })187 test.IsTrue(tt, ttt.IsFatal)188 if !strings.Contains(fatalMesg, tst.fatal) {189 tt.Errorf(`<%s> does not contain %q`, fatalMesg, tst.fatal)190 }191 })192 }193}...

Full Screen

Full Screen

NewTestingTB

Using AI Code Generation

copy

Full Screen

1import (2func TestNewTestingTB(t *testing.T) {3 t.Log("TestNewTestingTB")4}5import (6func TestNewTestingTB(t *testing.T) {7 t.Log("TestNewTestingTB")8}9import (10func TestNewTestingTB(t *testing.T) {11 t.Log("TestNewTestingTB")12}13import (14func TestNewTestingTB(t *testing.T) {15 t.Log("TestNewTestingTB")16}17import (18func TestNewTestingTB(t *testing.T) {19 t.Log("TestNewTestingTB")20}21import (22func TestNewTestingTB(t *testing.T) {23 t.Log("TestNewTestingTB")24}25import (26func TestNewTestingTB(t *testing.T) {27 t.Log("TestNewTestingTB")28}29import (30func TestNewTestingTB(t *testing.T) {31 t.Log("TestNewTestingTB")32}33import (34func TestNewTestingTB(t *testing.T) {35 t.Log("TestNewTestingTB")36}37import (38func TestNewTestingTB(t *testing.T) {39 t.Log("TestNewTestingTB")40}41import (42func TestNewTestingTB(t *testing.T

Full Screen

Full Screen

NewTestingTB

Using AI Code Generation

copy

Full Screen

1import (2func TestNewTestingTB(t *testing.T) {3 t.Log("TestNewTestingTB")4}5func main() {6 t := new(testing.T)7 t.Log("main")8}9--- FAIL: TestNewTestingTB (0.00s)10--- FAIL: TestNewTestingTB (0.00s)11--- FAIL: TestNewTestingTB (0.00s)12--- FAIL: TestNewTestingTB (0.00s)13--- FAIL: TestNewTestingTB (0.00s)14--- FAIL: TestNewTestingTB (0.00s)

Full Screen

Full Screen

NewTestingTB

Using AI Code Generation

copy

Full Screen

1func TestNewTestingTB(t *testing.T) {2 test.NewTestingTB(t)3}4func TestNewTestingTB(t *testing.T) {5 test.NewTestingTB(t)6}7func TestNewTestingTB(t *testing.T) {8 test.NewTestingTB(t)9}10func TestNewTestingTB(t *testing.T) {11 test.NewTestingTB(t)12}13func TestNewTestingTB(t *testing.T) {14 test.NewTestingTB(t)15}16func TestNewTestingTB(t *testing.T) {17 test.NewTestingTB(t)18}19func TestNewTestingTB(t *testing.T) {20 test.NewTestingTB(t)21}22func TestNewTestingTB(t *testing.T) {23 test.NewTestingTB(t)24}25func TestNewTestingTB(t *testing.T) {26 test.NewTestingTB(t)27}28func TestNewTestingTB(t *testing.T) {29 test.NewTestingTB(t)30}

Full Screen

Full Screen

NewTestingTB

Using AI Code Generation

copy

Full Screen

1import (2func TestNewTestingTB(t *testing.T) {3 fmt.Println("Hello, playground")4}5func main() {6 TestNewTestingTB(nil)7}8import (9func TestNewTestingTB(t *testing.T) {10 fmt.Println("Hello, playground")11}12func main() {13 TestNewTestingTB(nil)14}15import (16func TestNewTestingTB(t *testing.T) {17 fmt.Println("Hello, playground")18}19func main() {20 TestNewTestingTB(nil

Full Screen

Full Screen

NewTestingTB

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 test := testing.T{}4 test.NewTestingTB()5 fmt.Println("Test object created successfully")6}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful