image - Go Code 在 go test 和 go run 中的行为不同

标签 image http get go jpeg

我在我的 Ubuntu 12.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())
}

他们调用的函数 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 文件。但是,当我从 htmlutil 包运行“go test”时,出现以下错误:

There was a problem "image: unknown format"

是什么导致问题只在单元测试中失败?我究竟做错了什么?

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

最佳答案

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

测试在控制台上使用/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())
    }

}

我刚刚复制了你的代码如下:

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

go测试的输出

PASS
ok      test    0.843s

我的 go 版本是 go version devel +87f67aadaed6 Sat Dec 22 17:41:00 2012 -0800 darwin/amd64

关于image - Go Code 在 go test 和 go run 中的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14087766/

相关文章:

javascript - 如何在 javascript 中使用本地镜像?

angularjs - HTTP post 方法在 IE11 中不起作用

php - 是否可以在执行 jquery GET 请求时将值发送到 php 文件

Python Requests 库不需要的 GET 请求冒号转义

database - 将图像作为 varbinary (silverlight) 保存到数据库

css - *已编辑*当图像具有最大宽度时对齐图像及其标题

css - Wordpress:调用特色图像作为具有自定义大小的背景图像

html - 请求错误 (invalid_request) - Android 浏览器

php - 检查 curl http 命令

java - 从我的 JButtons 获取颜色