Json 测试API请求是否正在运行golang

Json 测试API请求是否正在运行golang,json,csv,go,testing,Json,Csv,Go,Testing,我正在尝试将测试添加到我的golang项目中。我的程序正在将JSON数据保存到csv文件中。我要做的是在屏幕上显示我的数据(请参阅注释//STDOUT),并使用管道自动创建我想要的格式文件:go run main.go>file.csv,例如,这里我将数据保存到名为file.csv的csv文件中 更清楚地说,以下是我的代码: package main import ( "encoding/json" "fmt" "io/ioutil" "net/http"

我正在尝试将测试添加到我的golang项目中。我的程序正在将JSON数据保存到csv文件中。我要做的是在屏幕上显示我的数据(请参阅注释
//STDOUT
),并使用管道自动创建我想要的格式文件:
go run main.go>file.csv
,例如,这里我将数据保存到名为file.csv的csv文件中

更清楚地说,以下是我的代码:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "strconv"
    "strings"
)

type People struct {
    Name string
    Craft string
}

type General struct {
    People []People
    Number int
    Message string
}

func main() {
    // Reading data from JSON File  
    response, err := http.Get("http://api.open-notify.org/astros.json")
    if err != nil {
        fmt.Printf("The Http request failed with error %s\n", err)
    }

    data,_ := ioutil.ReadAll(response.Body)
    //fmt.Println(string(data))
    // Unmarshal JSON data
    var general General
    json.Unmarshal([]byte(data), &general)
    var header []string
    header = append(header, "Number")
    header = append(header, "Message")
    header = append(header, "Name")
    header = append(header, "Craft")
    fmt.Println(strings.Join(header, ","))
    for _, obj := range general.People {    
        var record []string
        record = append(record, strconv.Itoa(general.Number), general.Message)
        record = append(record, obj.Name, obj.Craft)
        fmt.Println(strings.Join(record, ",")) // STDOUT 
        record = nil
    }
}
因此,在我的屏幕上,我获得了预期的输出:

Number,Message,Name,Craft
6,success,Christina Koch,ISS
6,success,Alexander Skvortsov,ISS
6,success,Luca Parmitano,ISS
6,success,Andrew Morgan,ISS
6,success,Oleg Skripochka,ISS
6,success,Jessica Meir,ISS
所以我想知道如何测试这个输出是否正确显示了我期望的数据

我试图编写一个
main\u test.go
文件,但我找不到如何正确编码

package main

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

func TestGetEntries(t *testing.T) {
    response, err := http.Get("http://api.open-notify.org/astros.json")
    if err != nil {
        t.Fatal(err)
    }
    // I don't know how to deal with this part:
    rr := httptest.NewRecorder()
    handler := http.HandlerFunc(main)
    handler.ServeHTTP(rr, req)
    if status := rr.Code; status != http.StatusOK {
        t.Errorf("handler returned wrong status code: got %v want %v",
            status, http.StatusOK)
    }

    // Check the response body is what we expect.
    // For example I want to test the first line:
    expected := `6,success,Christina Koch,ISS`
    if rr.Body.String() != expected {
        t.Errorf("handler returned unexpected body: got %v want %v",
            rr.Body.String(), expected)
    }
}

让我警告一下,我可能有点太喜欢考试了,正常人通常不会做得太过分

测试将直接打印到标准输出中的内容是我通常试图避免的事情,在将内容打印到标准输出之前测试内容是否从函数返回会更容易

有时,我真的希望我的测试覆盖率,在这种情况下,也可以使用前缀为“Example”的测试函数。这是一个糟糕的解决方案,但使用起来非常简单。

main.go

package main
import "fmt"
func main(){
   fmt.Println("Hello World")
}
package main
func ExampleMain(){
   main()
   // Output:
   // Hello World
}
package main

import "fmt"

func main() {

    txt := helloWorld()

    fmt.Println(txt)
}

func helloWorld() string {
    return "Hello World"
}
package main

import "testing"

func Test_helloWorld(t *testing.T) {
    tests := []struct {
        name string
        want string
    }{
        {
            name: "A simple Test",
            want: "Hello World",
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := helloWorld(); got != tt.want {
                t.Errorf("helloWorld() = %v, want %v", got, tt.want)
            }
        })
    }
}
main\u测试开始

package main
import "fmt"
func main(){
   fmt.Println("Hello World")
}
package main
func ExampleMain(){
   main()
   // Output:
   // Hello World
}
package main

import "fmt"

func main() {

    txt := helloWorld()

    fmt.Println(txt)
}

func helloWorld() string {
    return "Hello World"
}
package main

import "testing"

func Test_helloWorld(t *testing.T) {
    tests := []struct {
        name string
        want string
    }{
        {
            name: "A simple Test",
            want: "Hello World",
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := helloWorld(); got != tt.want {
                t.Errorf("helloWorld() = %v, want %v", got, tt.want)
            }
        })
    }
}
对于您的代码,“示例”应该是:

package main
func ExampleMain(){
   main()
   // Output:
   // Number,Message,Name,Craft
   // 6,success,Christina Koch,ISS
   // 6,success,Alexander Skvortsov,ISS
   // 6,success,Luca Parmitano,ISS
   // 6,success,Andrew Morgan,ISS
   // 6,success,Oleg Skripochka,ISS
   // 6,success,Jessica Meir,ISS
}
这是一个简单的测试方法,但有点难以保持更新,所以我不太喜欢它

我更喜欢这样更灵活的方式:
main.go

package main
import "fmt"
func main(){
   fmt.Println("Hello World")
}
package main
func ExampleMain(){
   main()
   // Output:
   // Hello World
}
package main

import "fmt"

func main() {

    txt := helloWorld()

    fmt.Println(txt)
}

func helloWorld() string {
    return "Hello World"
}
package main

import "testing"

func Test_helloWorld(t *testing.T) {
    tests := []struct {
        name string
        want string
    }{
        {
            name: "A simple Test",
            want: "Hello World",
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := helloWorld(); got != tt.want {
                t.Errorf("helloWorld() = %v, want %v", got, tt.want)
            }
        })
    }
}
main\u测试开始

package main
import "fmt"
func main(){
   fmt.Println("Hello World")
}
package main
func ExampleMain(){
   main()
   // Output:
   // Hello World
}
package main

import "fmt"

func main() {

    txt := helloWorld()

    fmt.Println(txt)
}

func helloWorld() string {
    return "Hello World"
}
package main

import "testing"

func Test_helloWorld(t *testing.T) {
    tests := []struct {
        name string
        want string
    }{
        {
            name: "A simple Test",
            want: "Hello World",
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := helloWorld(); got != tt.want {
                t.Errorf("helloWorld() = %v, want %v", got, tt.want)
            }
        })
    }
}

如果可能的话,最好不要在测试中请求外部服务,当然,如果您觉得出于您知道的任何原因,最好在测试中请求外部服务,那么也没关系,但是,如果你觉得这最好是一个模拟的it请求,并且不知道我会如何高兴地提出一些方法来完成它。

让我警告一下,我可能有点太喜欢测试了,正常人通常不会做得太过分

测试将直接打印到标准输出中的内容是我通常试图避免的事情,在将内容打印到标准输出之前测试内容是否从函数返回会更容易

有时,我真的希望我的测试覆盖率,在这种情况下,也可以使用前缀为“Example”的测试函数。这是一个糟糕的解决方案,但使用起来非常简单。

main.go

package main
import "fmt"
func main(){
   fmt.Println("Hello World")
}
package main
func ExampleMain(){
   main()
   // Output:
   // Hello World
}
package main

import "fmt"

func main() {

    txt := helloWorld()

    fmt.Println(txt)
}

func helloWorld() string {
    return "Hello World"
}
package main

import "testing"

func Test_helloWorld(t *testing.T) {
    tests := []struct {
        name string
        want string
    }{
        {
            name: "A simple Test",
            want: "Hello World",
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := helloWorld(); got != tt.want {
                t.Errorf("helloWorld() = %v, want %v", got, tt.want)
            }
        })
    }
}
main\u测试开始

package main
import "fmt"
func main(){
   fmt.Println("Hello World")
}
package main
func ExampleMain(){
   main()
   // Output:
   // Hello World
}
package main

import "fmt"

func main() {

    txt := helloWorld()

    fmt.Println(txt)
}

func helloWorld() string {
    return "Hello World"
}
package main

import "testing"

func Test_helloWorld(t *testing.T) {
    tests := []struct {
        name string
        want string
    }{
        {
            name: "A simple Test",
            want: "Hello World",
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := helloWorld(); got != tt.want {
                t.Errorf("helloWorld() = %v, want %v", got, tt.want)
            }
        })
    }
}
对于您的代码,“示例”应该是:

package main
func ExampleMain(){
   main()
   // Output:
   // Number,Message,Name,Craft
   // 6,success,Christina Koch,ISS
   // 6,success,Alexander Skvortsov,ISS
   // 6,success,Luca Parmitano,ISS
   // 6,success,Andrew Morgan,ISS
   // 6,success,Oleg Skripochka,ISS
   // 6,success,Jessica Meir,ISS
}
这是一个简单的测试方法,但有点难以保持更新,所以我不太喜欢它

我更喜欢这样更灵活的方式:
main.go

package main
import "fmt"
func main(){
   fmt.Println("Hello World")
}
package main
func ExampleMain(){
   main()
   // Output:
   // Hello World
}
package main

import "fmt"

func main() {

    txt := helloWorld()

    fmt.Println(txt)
}

func helloWorld() string {
    return "Hello World"
}
package main

import "testing"

func Test_helloWorld(t *testing.T) {
    tests := []struct {
        name string
        want string
    }{
        {
            name: "A simple Test",
            want: "Hello World",
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := helloWorld(); got != tt.want {
                t.Errorf("helloWorld() = %v, want %v", got, tt.want)
            }
        })
    }
}
main\u测试开始

package main
import "fmt"
func main(){
   fmt.Println("Hello World")
}
package main
func ExampleMain(){
   main()
   // Output:
   // Hello World
}
package main

import "fmt"

func main() {

    txt := helloWorld()

    fmt.Println(txt)
}

func helloWorld() string {
    return "Hello World"
}
package main

import "testing"

func Test_helloWorld(t *testing.T) {
    tests := []struct {
        name string
        want string
    }{
        {
            name: "A simple Test",
            want: "Hello World",
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := helloWorld(); got != tt.want {
                t.Errorf("helloWorld() = %v, want %v", got, tt.want)
            }
        })
    }
}

如果可能的话,最好不要在测试中请求外部服务,当然,如果您觉得出于您知道的任何原因,最好在测试中请求外部服务,那么也没关系,但是,如果您觉得这最好是一个模拟it请求,并且不知道我将如何建议一些方法来实现它。

旁注:将对象写入“主列表”,然后将整个列表打印到标准输出。然后您可以根据列表内容进行测试。因此,测试不应该依赖于外部服务。使用真实数据+一些预期的HTTP错误响应创建假响应。单元测试不应依赖外部服务。集成测试应该是。OP没有指定这是哪一个。我不明白你怎么做。旁注:将对象写入“主列表”,然后将整个列表打印到标准输出。然后您可以根据列表内容进行测试。因此,测试不应该依赖于外部服务。使用真实数据+一些预期的HTTP错误响应创建假响应。单元测试不应依赖外部服务。集成测试应该是。OP没有指定这是哪一个。我不明白你怎么能这么做。