doushang1890 2014-09-01 16:17
浏览 51

使用Go和银杏测试Stdout

here I am making my first steps in go trying to do BDD on a go command line app. I am using Ginkgo, which wraps testing.go and lets you do more expressive BDD. https://github.com/onsi/ginkgo

I am having issues in reading the stdout to do an assertion on it.

Found that on pkg/testing example do stub the output before running but I can not find the way to read that output: http://golang.org/src/pkg/testing/example.go

This is what I would like to do:

cli.go

 package cli

 import "fmt"

 func Run() {
        fmt.Println("Running cli")
 }

cli_test.go

package cli_test

import (
        . "github.com/altoros/bosh_deployer_cli/lib/cli"

        . "github.com/onsi/ginkgo"
        . "github.com/onsi/gomega"
)

var _ = Describe("Cli", func() {
        It("should parse update stemcell flag", func() {
                Run()
                Expect(stdout).To(Equal("running cli"))
        })
})
  • 写回答

2条回答 默认 最新

  • duangutian1426 2014-09-03 14:25
    关注

    Testing Stdout can be tricky. You have multiple choice.

    You can override os.Stdout during your test: (think to check the errors)

    var _ = Describe("Cli", func() {
            It("should parse update stemcell flag", func() {
                    r, w, _ := os.Pipe()
                    tmp := os.Stdout
                    defer func() {
                            os.Stdout = tmp
                    }()
                    os.Stdout = w
                    go func() {
                            Run()
                            w.Close()
                    }()
                    stdout, _ := ioutil.ReadAll(r)
                    Expect(string(stdout)).To(Equal("Running cli
    "))
            })
    })
    

    or you can pass a writer to your function:

    cli.go

    package cli
    
    import (
            "fmt"
            "io"
    )
    
    func Run(w io.Writer) {
            fmt.Fprintln(w, "Running cli")
    }
    

    cli_test.go

    package cli_test
    
    import (
            . "cli"
            "io"
            "io/ioutil"
    
            . "github.com/onsi/ginkgo"
            . "github.com/onsi/gomega"
    )
    
    var _ = Describe("Cli", func() {
            It("should parse update stemcell flag", func() {
                    r, w := io.Pipe()
                    go func() {
                            Run(w)
                            w.Close()
                    }()
                    stdout, _ := ioutil.ReadAll(r)
                    Expect(string(stdout)).To(Equal("Running cli
    "))
            })
    })
    

    main.go

    package main
    
    import (
            "cli"
            "os"
    )
    
    func main() {
            cli.Run(os.Stdout)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver