Sesame-Disk / cool-storage-api

3 stars 0 forks source link

Tech to use for Unit testing and test coverage checks in Golang. #3

Closed aguzmans closed 2 years ago

aguzmans commented 2 years ago

Find a test coverage checks tool that tells us if everything has unit tests or not and help use write better tests.

Expected result: 1- Documentaiton of how to do unit testin (can a short explanaton with links to specifics written in other places or wrtten by your self) 2- Possible test coverage libraries/framewords to use. Recommend and explain one and test and how results; document same way as point 1.

ezielramos commented 2 years ago

Unit Test in Golang:

5 of the most popular library for unit testing are:

Go standard library: The "testing" package

Unlike most other languages, Go has a built-in test runner and framework for standard language tooling. The easiest way to run unit tests for your project is using a command like go test ./... from the command line. Running this command will discover and run any tests within your current directly or their subdirectories.

Tests must be in files separate from your main package code and end with the suffix _test.go. A test function is identified by starting with func Test .

to run the all tests : go test ./... to run some test : go test -v .\some_test.go

Golang test tool provides 3 functions :

a) Test function.

b) Benchmark function: In developing software, sometimes we have a different way with our partners or maybe we have many solutions to solve one problem. How can we measure which is the best solution? One of the answers is to test the performance from all of our solutions and chose the fastest.

c) Example function. Example function is a little same with the Test function, that is to make sure that our program runs as we expected. The Example function is intended for programmers who want to quickly create unit tests. The example unit test function automatically compares the standard output print (fmt) in golang with our output comment, so we don't have to create check expected logic. Because the syntax doesn’t require additional libraries and we don’t need to make logic to match the results obtained with expectations, making unit tests can be done faster. There are 2 advantages for creating example unit test : Faster to create a unit test. Because the code in the unit test is less than usual, so the programmer can faster to make a unit test. Easy to do experimentations

The package does not support assertions or other testing helper functions, as the development team believes implementing these features would add too much complexity. To counter the repetitiveness of writing tests without assertions and helper functions, they suggest using table-driven tests.

Improving Your Go Tests and Mocks With Testify

Get de library - go get github.com/stretchr/testify

Testify is an incredibly popular testing framework. It makes available a range of assertion functions for comparing and verifying values. It also offers mock functionality to easily mock resources. Testify is very user-friendly. It works well with the testing package and go test command. It does not offer its own custom test coverage reporting, unlike other packages. However, coverage can be obtained through the testing package. Its pkg documentation page: - https://github.com/stretchr/testify is thorough and offers numerous examples.

About testify from github:

Assert package

The assert package provides some helpful methods that allow you to write better test code in Go.

Require package

The require package provides same global functions as the assert package, but instead of returning a boolean result they terminate current test.

Mock package

The mock package provides a mechanism for easily writing mock objects that can be used in place of real objects when writing test code.

Suggestions

My suggestion is to use both libraries, specifically to check the result of a function, assert package is simpler, and mock package is very useful, but you can use Benchmark function from testing to compare several solutions. It is necessary to use testing for coverage checks; and every assert func takes the testing.T object as the first argument. An example off function look like this:

func TestUserStorage_EmptySearchUser(t *testing.T) {
    var userStorage user.UserStorage
    assert.Equal(t, 0, userStorage.Count())
    assert.Equal(t, user.User{}, userStorage.FindUserByID(1))
}

Reference:

ezielramos commented 2 years ago

Test coverage checks tool

Library

How to make Coverage in Golang?

Reference