Testing Go Applications Flashcards
Does go provide out of the box tooling for unit test?
Yes. No need to install 3rd party packages to do that.
What about assertions, does it provide a good support?
Not much… that was intentional since the simplest assertion can be an if.
What are the three types of unit test in go? What each does?
Test, Benchmark and example.
Test = unit, integration and end to end
Benchmark = performance
Example = Documentation (it runs the test and include in the go docs).
How does the test tooling identify a test file in go? What about the package? Are these files included in production binaries?
They are suffixed with _test.go. Same thing for package. Not included in production binaries.
How does the test tooling identify a test method in go? What is the only parameter it requires?
They are PREFIXED with Test. A pointer to *testing.T
What are the 4 packages that the standard library offers for testing?
testing (the most common one), testing/quick (black box testing, e.g testing a package without knowing its internals, testing/iotest (helpers for readers and writers) and net/http/httptest (simulate request, responde recorder, test servers for real http requests).
What are the most popular community packages for testing in go and what they are used for?
- Testify (has better assertion mechanisms)
- Ginko (bdd tests for go)
- GoConvey (browser-based test results viewer)
- httpexcept (helps with end-to-end tests for rest apis)
- gomock (allow mocking in go)
- go-sqlmock mockable in-memory database
How to do a whitebox testing in go?
Use the same module name as the production code (so you can access internals of the package). Also you don’t need to import the under-test package since the prod code and the tests are under the same package.
How to declare a public function in go?
Start the function name with a Upper case letter.
Should we put end-to-end tests or integration tests in the same folder as the production files that are going to be tested?
No. They should be placed somewhere else since they are testing multiple files at once.
What are the two types of test failures reporting? What are the prefix of the first type?
Immediate (no code run after this) and non-immediate failure (reports a failure at the end but the test execution in that file goes on)
Immediate failures are prefixed with Fatal (and the exception is FailNow()).
How to run all tests in the directory with verbose output?
got test -v
How to run test in specific packages?
go test pkgName
How to run tests in the current and descendant directories?
go test ./…
How to run tests that matches filenames that matches a regexp?
go test -run regexp