Testing Go Applications Flashcards

1
Q

Does go provide out of the box tooling for unit test?

A

Yes. No need to install 3rd party packages to do that.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What about assertions, does it provide a good support?

A

Not much… that was intentional since the simplest assertion can be an if.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the three types of unit test in go? What each does?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the test tooling identify a test file in go? What about the package? Are these files included in production binaries?

A

They are suffixed with _test.go. Same thing for package. Not included in production binaries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does the test tooling identify a test method in go? What is the only parameter it requires?

A

They are PREFIXED with Test. A pointer to *testing.T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 4 packages that the standard library offers for testing?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the most popular community packages for testing in go and what they are used for?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to do a whitebox testing in go?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to declare a public function in go?

A

Start the function name with a Upper case letter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Should we put end-to-end tests or integration tests in the same folder as the production files that are going to be tested?

A

No. They should be placed somewhere else since they are testing multiple files at once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the two types of test failures reporting? What are the prefix of the first type?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to run all tests in the directory with verbose output?

A

got test -v

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to run test in specific packages?

A

go test pkgName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to run tests in the current and descendant directories?

A

go test ./…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to run tests that matches filenames that matches a regexp?

A

go test -run regexp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to print information about a command line verb?

A

e.g: go help test

17
Q

How is the test suite name printed in the go test command?

A

the name of module (and sub modules)

18
Q

How to generate coverage reports in html? How to read it?

A

go test -coverprofile count.out -covermode count

go tool cover -html count.out

19
Q

What is table driven tests and how is it supported on go?

A

is an array of scenarios with the inputs and expected output given the inputs. It’s simply an array of scenarios and a loop over the scenarios.. no special keywords.

20
Q

How can I log stuff during the tests?

A

log and logf

21
Q

How to skip tests in go?

A

Skip, SkipF and SkipNow

22
Q

What does the t.Run method does?

A

Allow running tests inside the test method. Useful to test different flavors of the same thing and grouping them in the same method.

23
Q

Can I run tests concurrently? What go does by default?

A

Yes, just call Parallel. Run sequentially.

24
Q

What is the 2 differences between the declaration of a regular unit test and benchmark tests? What does it provide?

A

The name of the test is Benchmark and the parameter is *testing.B. Provides property to run our tests N times, provides timers b.StartTimers and provide parallelism via b.RunParallel.

25
Q

How to run bench tests?

A

go test -bench .

26
Q

What is the default benchmark time provided by go, how to change that?

A

go bench . -benchtime 10s

27
Q

How does the benchmark testing in go works?

A

The benchmark is going to run as much operations as possible given the time frame provided by -benchtime (or 1s default).

28
Q

What are the resources mapped by the profiling tests of go?

A

block, cover, cpu, mem and mutex.

29
Q

How to run profiling tests?

A

go test run -benchmem (there are other types of profile).