doufeng9567 2012-12-30 02:24
浏览 13
已采纳

Go代码在go测试和go运行中的行为有所不同

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.

  • 写回答

2条回答 默认 最新

  • dongwei1855 2012-12-30 17:40
    关注

    I attempted rputikar's solution (use t.Fatal() instead of fmt.Println()), but that didn't help.

    I did notice that rputikar was doing something subtly different with his imports than I was. My imports in the htmlutil.go looked like:

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

    but both my main.go and rputikar's main_test.go contained an additional import, "image/jpeg". So, I added that into my htmlutil.go import list and that solved the problem. I think I'll add "_ image/png" and "_ image/gif" just for future-proofing

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?