使用Golang进行功能测试

使用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.

我有一个用Golang编写的Web服务。 p>

我需要对其进行重构 因为它写得不好。 p>

我已经看到Golang使用 TestMethodName(t * testing.T) code>格式工作。 这对单元测试非常有用,但就我而言,由于重构,方法将发生很大变化。 p>

这就是为什么我要编写功能测试,以便可以测试每个端点, 并检查输出是否具有正确的格式,而不依赖于功能 p>

我应该如何使用Golang进行操作? p>

是否有任何可以帮助我的框架 进行功能测试? 在另一个堆栈中,例如在PHP / Laravel中,我可以将PostgreSQL与SQLite交换,或者在内存中进行测试,这对于这种测试非常实用。 p> div>

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