0
0
Goprogramming~5 mins

Testing package overview in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the testing package in Go?
The testing package provides tools to write and run automated tests for Go code, helping ensure code correctness.
Click to reveal answer
beginner
How do you write a basic test function using the testing package?
Create a function starting with Test and accepting t *testing.T as a parameter. Example:<br>
func TestExample(t *testing.T) { /* test code */ }
Click to reveal answer
intermediate
What method do you use to report a test failure inside a test function?
Use t.Error() or t.Errorf() to report a failure but continue running the test, or t.Fatal() / t.Fatalf() to stop the test immediately.
Click to reveal answer
beginner
How do you run tests in a Go project from the command line?
Use the command go test in the directory containing the test files. It automatically finds and runs tests.
Click to reveal answer
advanced
What is the role of the TestMain function in Go testing?
TestMain(m *testing.M) allows setup and teardown before and after running tests. It controls the test execution flow.
Click to reveal answer
Which prefix must a function have to be recognized as a test by the Go testing package?
AVerify
BCheck
CRun
DTest
What parameter type does a Go test function receive?
Atesting.Test
B*testing.Test
C*testing.T
Dtesting.T
Which command runs all tests in the current directory in Go?
Ago test
Bgo build
Cgo run
Dgo check
What does t.Fatal() do inside a test?
AStops the test immediately
BLogs an error and continues
CSkips the test
DMarks the test as passed
What is the purpose of TestMain in Go tests?
ATo define a test case
BTo set up and tear down tests
CTo run benchmarks
DTo skip tests
Explain how to write and run a basic test using the Go testing package.
Think about the function name and parameter, then how to run tests.
You got /3 concepts.
    Describe the difference between t.Error() and t.Fatal() in Go tests.
    Consider what happens after reporting an error.
    You got /2 concepts.