1

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.

7
  • 3
    I don't see how the testing package "being great for unit testing" makes it unsuitable for functional tests. Commented May 2, 2019 at 13:58
  • 1
    as long as the input and output of functions does not change you can keep using the same tests Commented May 2, 2019 at 14:00
  • 3
    Just because the functions are often named TestMethodName, doesn't mean you must use that naming scheme, or can't test whatever you want. Look at any large go project, and you'll see tests of all sorts, at all levels of integration. Commented May 2, 2019 at 14:03
  • 2
    @JuliatzindelToro questions asking for framework/library recommendations are off-topic (stackoverflow.com/help/on-topic). If you need help with something show us the code you've tried and explain what issues you've encountered (stackoverflow.com/help/how-to-ask). Commented May 2, 2019 at 14:20
  • 1
    Check this article out to get you started: blog.questionable.services/article/testing-http-handlers-go Commented May 2, 2019 at 15:30

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.