I'm running go 1.0.3 on my Ubuntu 12.04.1 Laptop and I've stumbled upon a problem where if I run some code in main(), it behaves much more differently than if I run it with go test.
Here's my example:
From main.go
package main
import (
"image"
"image/jpeg"
"fmt"
"myproj/htmlutil"
[some imports removed]
)
func main() {
img, err := htmlutil.GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg")
if err != nil {
fmt.Println("There was a problem ",err)
}
fmt.Println("Bounds were ",img.Bounds())
}
From myproj/htmlutil_test.go
package htmlutil
import (
"image"
"fmt"
"testing"
[some imports removed]
)
func TestGetImageFromURL(t *testing.T){
img, err := GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg")
if err != nil {
t.Fatalf("There was a problem %q",err)
}
fmt.Println("Bounds were ",img.Bounds())
}
and the function that they call, GetResizedImageFromWeb(), is in myproj/htmlutil.go:
package htmlutil
import (
"errors"
"fmt"
"image"
"io/ioutil"
"net/http"
[some imports removed]
)
func GetResizedImageFromWeb(imageURL string) (image.Image, error) {
resp, err := http.Get(imageURL)
if err != nil {
return nil, errors.New(fmt.Sprint("There was a problem reading the site %q Debug[%s]",imageURL, err))
}
defer resp.Body.Close()
//Decode the image using image's general purpose decoder
image, s, err := image.Decode(resp.Body)
if err != nil {
return nil, err
}
return resizeImage(image), nil
}
When I run "go run main.go" from the command line, I see the bounds of the image from the url and can save it as a jpg file on disk if I want using a function in main.go. However, when I run "go test" from the htmlutil package, I get the following error:
There was a problem "image: unknown format"
What is causing the problem to only fail in the unit tests? What am I doing wrong?
My only guess is that for what ever reason, the html.Get() isn't returning all the data in the testing scenario, but I'm still baffled as to why that happens.
The 'go test' command may run tests for different packages in parallel as well, according to the setting of the -p flag (see 'go help build'). Allow parallel execution of test functions that call t. Parallel . The value of this flag is the maximum number of tests to run simultaneously.
At the command line in the greetings directory, run the go test command to execute the test. The go test command executes test functions (whose names begin with Test ) in test files (whose names end with _test.go). You can add the -v flag to get verbose output that lists all of the tests and their results.
Go provides the go test command for running the unit tests in a project. Running this command will ordinarily run the whole test suite, but you can limit it to only a specific file or test. If you run go test , it will run both the TestFactorial and TestSquare functions.
In tests you should really check for the results of your function calls.
Tests run with /dev/null on console. Therefore fmt / log outputs are not visible. You should do something like the following in htmlutil_test.go
func TestMain(t *testing.T) {
img, err := GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg")
if err != nil {
t.Error("There was a problem ", err)
}
bounds := image.Rectangle{
image.Point{0, 0},
image.Point{616, 462}}
if img.Bounds() != bounds {
t.Error("Incorrect Bounds were ", img.Bounds())
}
}
I just copied your code as follows:
main.go
package main
import (
"errors"
"fmt"
"image"
_ "image/jpeg"
"net/http"
)
func GetResizedImageFromWeb(imageURL string) (image.Image, error) {
resp, err := http.Get(imageURL)
if err != nil {
return nil, errors.New(fmt.Sprint("There was a problem reading the site %q Debug[%s]", imageURL, err))
}
defer resp.Body.Close()
//Decode the image using image's general purpose decoder
image, _, err := image.Decode(resp.Body)
if err != nil {
return nil, err
}
return image, nil
}
func main() {
img, err := GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg")
if err != nil {
fmt.Println("There was a problem ", err)
}
fmt.Println("Bounds were ", img.Bounds())
}
main_test.go
package main
import (
"image"
"log"
"testing"
)
func TestMain(t *testing.T) {
img, err := GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg")
if err != nil {
t.Error("There was a problem ", err)
}
bounds := image.Rectangle{
image.Point{0, 0},
image.Point{616, 462}}
if img.Bounds() != bounds {
t.Error("Incorrect Bounds were ", img.Bounds())
}
}
The output of go test
PASS
ok test 0.843s
my go version is go version devel +87f67aadaed6 Sat Dec 22 17:41:00 2012 -0800 darwin/amd64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With