douliaotong4944 2017-03-01 07:51
浏览 479

golang测试退出状态为-1,不显示任何内容

Recently I've been working on a Restful app in golang, strange things happened when I try to write tests in different subdirectories. My project structure is:

├── handlers/
│   ├── defs.go
│   ├── hello_test.go
│   ├── hello.go
├── server/
│   ├── codes.go
│   ├── middlewares_test.go
│   ├── middlewares.go
├── utility/
│   ├── auth.go
│   ├── auth_test.go

All files in handlers/ are declared "package handlers", all files in server/ are declared "package server", and so on. When I run go test in utility/ and handlers/ everything is fine. But if I run go test in server/, it returns me nothing but just:

[likejun@localhost server]$ go test
exit status 1
FAIL    server_golang/server    0.003s

It seems that it exits with a code 1 before even run, could someone tells me why this happened? Thank you, I've been spent the whole afternoon on it.

the code in middleware_test.go

package server

import (
    "io"
    "net/http"
    "net/http/httptest"
    "testing"
)

func TestHello(t *testing.T) {
    req, err := http.NewRequest(http.MethodGet, "/", nil)
    if err != nil {
        t.Fatal(err)
    }

    rec := httptest.NewRecorder()

    func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
        w.Header().Set("Content-Type", "application/json")
        io.WriteString(w, `{"hello": "world"}`)
    }(rec, req)

    if status := rec.Code; status != http.StatusOK {
        t.Errorf("handler returned wrong status code: got %d want %d", status, http.StatusOK)
    }

    if rec.Header().Get("Content-Type") != "application/json" {
        t.Errorf("handler returned wrong content type header: got %s want %s", rec.Header().Get("Content-Type"), "application/json")
    }

    expected := `{"hello": "world"}`
    if rec.Body.String() != expected {
        t.Errorf("handler returned unexpected body: got %s want %s", rec.Body.String(), expected)
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 2024-五一综合模拟赛
    • ¥15 如何将下列的“无限压缩存储器”设计出来
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口