2012-12-30 23 views
6

Sto correndo su 1.0.3 sul mio computer portatile Ubuntu 12.04.1 e mi sono imbattuto in un problema in cui se eseguo del codice in main(), si comporta molto più diversamente che se lo eseguissi con go test.Go Code si comporta in modo diverso in go test vs go run

Ecco il mio esempio:
Da 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()) 
} 

Da 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()) 
} 

e la funzione che essi chiamano, GetResizedImageFromWeb(), è 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 
} 

Quando si esegue "go run main.go" dal riga di comando, vedo i limiti dell'immagine dall'url e posso salvarla come un file jpg sul disco se voglio usare una funzione in main.go. Tuttavia, quando si esegue "go test" dal pacchetto htmlutil, ottengo il seguente errore:

There was a problem "image: unknown format" 

cosa sta causando il problema a fallire solo nei test di unità? Che cosa sto facendo di sbagliato?

La mia unica ipotesi è che per qualsiasi motivo, il html.Get() non restituisce tutti i dati nello scenario di test, ma sono ancora sconcertato sul motivo per cui ciò accade.

risposta

2

ho cercato la soluzione di rputikar (utilizzare t.Fatal() al posto di fmt.Println()), ma che non ha aiutato.

I ha fatto avviso che rputikar stava facendo qualcosa sottilmente diverso con le sue importazioni di me. I miei importazioni nella htmlutil.go sembravano:

package htmlutil  

import (
    "errors" 
    "fmt" 
    "image" 
    "io/ioutil" 
    [some imports removed] 
    "net/http" 
) 

ma sia il mio main.go e main_test.go di rputikar conteneva un'importazione addizionale, "image/jpeg". Così, l'ho aggiunto alla mia lista di importazione htmlutil.go e questo ha risolto il problema. Penso che aggiungerò "_ image/png" e "_ image/gif" solo per prove future

4

Nei test è necessario verificare i risultati delle chiamate di funzione.

I test vengono eseguiti con/dev/null sulla console. Pertanto le uscite fmt/log non sono visibili. Si dovrebbe fare qualcosa di simile a quanto segue 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()) 
    } 

} 

Io ho semplicemente copiato il codice come segue:

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()) 
    } 
} 

L'output di go test

PASS 
ok  test 0.843s 

mia versione go è go version devel +87f67aadaed6 Sat Dec 22 17:41:00 2012 -0800 darwin/amd64