You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Package A's test (aka a/*_test.go) should be able to see and import exported variables, functions and structs defined in package B's test (aka b/*_test.go). This would make mock a lot easier, and "DRY" test code.
Example and Detail
Suppose package weather wraps a weather API:
// weather/temperature.gopackage weather
// apiEndpoint is where the weather API livesvarapiEndpoint="https://example.com"// TemperatureNow fetch current temperature of specified cityfuncTemperatureNow(ctx context.Context, citystring) (temperaturefloat64, errerror) {
query:= url.Values{
"city": []string{city},
}
varb strings.Builderb.WriteString(apiEndpoint)
b.WriteString("/cgi-bin/temperature?")
b.WriteString(query.Encode())
req, err:=http.NewRequest(http.MethodGet, b.String(), nil)
// do the request, parse the result...return
}
With Go's fabulous net/http/httptest, I can mock the weather API backend easily, and do the test:
Now suppose I am writing a business package farm, in which, a function waterVolume() calls weather.TemperatureNow() and many other methods from weather and calculates how much water for watering. Things get a little awkward if I want to test waterVolume():
when calling waterVolume(), weather.TemperatureNow() gets called, and an API call goes to apiEndpoint (https://example.com) rather than a mock(for consistent response).
weather.SetAPIEndpoint() is not available in farm's test. I could export weather.apiEndpoint (i.e weather.APIEndpoint, which does not sounds like a good idea) and change it to a mock during test of package farm, but I have to repeat code of weather.BackendMock() since I can not call it from farm's test.
it may not be a good idea to move weather.BackendMock() to non-test part of package farm.
moving weather.BackendMock() to a special package for testing works, but a little bothering.
moving weather.SetAPIEndpoint to a special package for testing does not work, since the package private variable apiEndpoint is involved.
The text was updated successfully, but these errors were encountered:
I definitely feel the problem that there isn't a good place to put shared test fixtures, i.e. test code you don't want production code to depend on, nor you want to include in a release build, but you want other test packages to be able to import. IMO this proposal is not bad. Another possibility would be a way to mark a package "test only".
Per discussion with @golang/proposal-review, it would be weird to introduce such a major change to the behavior of packages just for tests. If tests have become so large that they start accumulating their own types/helpers/etc. then they should be factored out in to their own package. Sorry.
Abstract
Package A's test (aka
a/*_test.go
) should be able to see and import exported variables, functions and structs defined in package B's test (akab/*_test.go
). This would make mock a lot easier, and "DRY" test code.Example and Detail
Suppose package
weather
wraps a weather API:With Go's fabulous
net/http/httptest
, I can mock the weather API backend easily, and do the test:Now suppose I am writing a business package
farm
, in which, a functionwaterVolume()
callsweather.TemperatureNow()
and many other methods fromweather
and calculates how much water for watering. Things get a little awkward if I want to testwaterVolume()
:waterVolume()
,weather.TemperatureNow()
gets called, and an API call goes toapiEndpoint
(https://example.com
) rather than a mock(for consistent response).weather.SetAPIEndpoint()
is not available infarm
's test. I could exportweather.apiEndpoint
(i.eweather.APIEndpoint
, which does not sounds like a good idea) and change it to a mock during test of packagefarm
, but I have to repeat code ofweather.BackendMock()
since I can not call it fromfarm
's test.weather.BackendMock()
to non-test part of packagefarm
.weather.BackendMock()
to a special package for testing works, but a little bothering.weather.SetAPIEndpoint
to a special package for testing does not work, since the package private variableapiEndpoint
is involved.The text was updated successfully, but these errors were encountered: