Search code examples
gotestingfunctional-testing

Functional testing with Golang


I have a webservice written in Golang.

I need to refactor it because it is poorly written.

I have seen Golang works with the format TestMethodName(t *testing.T). This is great for unit testing, but in my case, methods will change a lot because of the refactoring.

That's why I want to write functional testing, so that I can test each endpoint, and check that output has correct format, without being dependant of functions

How should I do it with Golang ?

Is there any framework that helps me with functional tests ? In another stack, like PHP / Laravel for instance, I can swap PostgreSQL with a SQLite, or In Memory testing, which is very practical for this kind of tests.


Solution

  • You can test your API endpoints by using net/http/httptest package and mocking external dependencies.

    See example: https://golang.org/src/net/http/httptest/example_test.go