Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image Go代码在Go测试和Go运行中的行为不同_Image_Http_Get_Go_Jpeg - Fatal编程技术网

Image Go代码在Go测试和Go运行中的行为不同

Image Go代码在Go测试和Go运行中的行为不同,image,http,get,go,jpeg,Image,Http,Get,Go,Jpeg,我在我的Ubuntu12.04.1笔记本电脑上运行go 1.0.3,我偶然发现了一个问题,如果我在main()中运行一些代码,它的行为将与使用go test运行时大不相同 下面是我的例子: 从main.go package main import ( "image" "image/jpeg" "fmt" "myproj/htmlutil" [some imports removed] ) func main() { img, err := ht

我在我的Ubuntu12.04.1笔记本电脑上运行go 1.0.3,我偶然发现了一个问题,如果我在main()中运行一些代码,它的行为将与使用go test运行时大不相同

下面是我的例子:
从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_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())
}
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())
    }

}
他们调用的函数GetResizedImageFromWeb()位于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文件。但是,当我从HtmlTil包运行“go test”时,我得到以下错误:

There was a problem "image: unknown format"
是什么导致问题只在单元测试中失败?我做错了什么


我唯一的猜测是,无论出于什么原因,html.Get()都没有返回测试场景中的所有数据,但我仍然不明白为什么会发生这种情况。

在测试中,您应该真正检查函数调用的结果

在控制台上使用/dev/null运行测试。因此,fmt/日志输出不可见。您应该在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())
}
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())
    }

}
我刚才复制了您的代码,如下所示:

梅因,加油

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())
    }
}
go测试的输出

PASS
ok      test    0.843s
我的go版本是
go版本devel+87f67aadaed6 Sat Dec 22 17:41:00 2012-0800 darwin/amd64

我尝试了rputikar的解决方案(使用t.Fatal()而不是fmt.Println()),但没有帮助

我确实注意到,rputikar在进口商品方面做的事情与我有着微妙的不同。我在htmlutil.go中的导入看起来像:

package htmlutil    

import (
    "errors"
    "fmt"
    "image"
    "io/ioutil"
    [some imports removed]
    "net/http"
)
但是mymain.go和rputikar的main_test.go都包含了一个额外的导入,“image/jpeg”。所以,我把它添加到我的htmlutil.go导入列表中,解决了这个问题。我想我会添加“\image/png”“\image/gif”,只是为了将来的校对