Closed aguzmans closed 2 years ago
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.
expectation := false
actual := palindrome.IsPalindrome("abc")
if actual != expectation {
t.Errorf("Expected %v but got %v", expectation, actual)
}
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
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.
go get github.com/stretchr/testify
The assert package provides some helpful methods that allow you to write better test code in Go.
assert.NotEqual(t, status, "down")
- assert.Equal(Calculate(test.input), test.expected)
The require package provides same global functions as the assert package, but instead of returning a boolean result they terminate current test.
The mock package provides a mechanism for easily writing mock objects that can be used in place of real objects when writing test code.
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))
}
go get golang.org/x/tools/cmd/cover
go test ./... --coverprofile=coverage.out
View the coverage profile in your browser - go tool cover --html=coverage.out
└─ README.md
├─ cmd
│ └─main.go
├─ go.mod
├─ go.sum
├─ internal
│ ├─user
│ │ ├─storage.go
│ │ ├─storage_test.go
│ │ ├─user.go
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.