2012-12-30 11 views
6

私はUbuntu 12.04.1ラップトップで1.0.3を実行していますが、main()でいくつかのコードを実行すると、私はそれをgo testで実行するよりも異なっています。goテストとgo runの動作が異なっている

ここに私の例です:に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()) 
} 

と彼らが呼ぶ機能、GetResizedImageFromWeb()から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()) 
} 

から
は、に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 
} 

「go run main.go」をコマンドラインでは、URLからの画像の境界を確認し、main.goの関数を使用したい場合は、ディスク上のjpgファイルとして保存することができます。私はhtmlutilパッケージから「テストを行く」を実行するときしかし、私は次のエラーを取得する:

There was a problem "image: unknown format" 

問題がユニットテストに失敗する原因は何?私は間違って何をしていますか?

これまでの理由で、html.Get()はテストシナリオのすべてのデータを返すわけではありませんが、それがどうして起こるのかはまだ分かりません。

答えて

2

である私は、(代わりにfmt.Printlnのt.Fatalを(使用)())rputikarのソリューションを試みたが、それは助けにはなりませんでした。

I rputikarが私のものよりも微妙に何かをしていたことに気づいてください。 htmlutil.goでの私の輸入は、ように見えた:

package htmlutil  

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

が、私のmain.goとrputikarのmain_test.go両方は、"画像/ JPEG"を追加輸入を含んでいました。だから、私はそれを私のhtmlutil.goインポートリストに追加し、それが問題を解決しました。私は"_ image/png""_ image/gif"を将来のために追加すると思います

4

テストでは、実際に関数呼び出しの結果を確認する必要があります。

コンソールで/ dev/nullを指定してテストを実行します。したがって、fmt/log出力は表示されません。あなたは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()) 
    } 

} 
次のように私はあなたのコードをコピーし

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

ゴーテストの出力

PASS 
ok  test 0.843s 

私の行くのバージョンがgo version devel +87f67aadaed6 Sat Dec 22 17:41:00 2012 -0800 darwin/amd64

関連する問題