第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在不使用詳細(xì)選項(xiàng)的情況下從 go test 打印到控制臺

如何在不使用詳細(xì)選項(xiàng)的情況下從 go test 打印到控制臺

Go
手掌心 2022-12-13 10:46:02
我想打印一條信息性消息以從測試中進(jìn)行控制臺,但沒有詳細(xì)的測試輸出。我嘗試使用以下方法從測試中打?。篺mt.Println("my message") // STDOUTfmt.Fprintln(os.Stderr, "my message") // STDERRt.Log("my message\n") // testing.T loggo test -v如果使用,它們都產(chǎn)生在控制臺中顯示的相同效果,但如果只是go test使用則不顯示。但是,使用go test -v,我還得到了所有詳細(xì)的測試輸出,例如:=== RUN   My_Test--- PASS: My_Test (0.07s)go help testflag說:-v    Verbose output: log all tests as they are run. Also print all    text from Log and Logf calls even if the test succeeds.但我想要的是不在所有測試運(yùn)行時記錄它們,但 即使測試成功,仍然打印來自 Log 和 Logf 調(diào)用的所有文本有沒有辦法從測試中打印可見消息,但看不到RUN和PASS消息?
查看完整描述

3 回答

?
嚕嚕噠

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個贊

出于顯而易見的原因,測試框架“劫持”了標(biāo)準(zhǔn)輸出和錯誤流。所以無論如何,對這些流的寫入是否出現(xiàn)在輸出中是由測試框架控制的,除了顯示或隱藏所有使用標(biāo)志之外,它沒有提供“自定義”它的方法-v。


你可以做的是使用-json測試標(biāo)志:


-json

    Log verbose output and test results in JSON. This presents the

    same information as the -v flag in a machine-readable format.

因此,您獲得了原本使用 獲得的所有輸出-v,但每一行都有一個單獨(dú)的 JSON 對象。


具有此測試功能:


func TestMy_Test(t *testing.T) {

    fmt.Println("[custom] my message from fmt.Println")

}

的輸出go test -v .


=== RUN   TestMy_Test

[custom] my message from fmt.Println

--- PASS: TestMy_Test (0.00s)

PASS

ok      github.com/icza/play    0.002s

的輸出go test -json .


{"Time":"2022-05-10T09:26:26.712800797+02:00","Action":"run","Package":"github.com/icza/play","Test":"TestMy_Test"}

{"Time":"2022-05-10T09:26:26.71293072+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"=== RUN   TestMy_Test\n"}

{"Time":"2022-05-10T09:26:26.712946548+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"[custom] my message from fmt.Println\n"}

{"Time":"2022-05-10T09:26:26.712954637+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"--- PASS: TestMy_Test (0.00s)\n"}

{"Time":"2022-05-10T09:26:26.712958774+02:00","Action":"pass","Package":"github.com/icza/play","Test":"TestMy_Test","Elapsed":0}

{"Time":"2022-05-10T09:26:26.712964812+02:00","Action":"output","Package":"github.com/icza/play","Output":"PASS\n"}

{"Time":"2022-05-10T09:26:26.713170439+02:00","Action":"output","Package":"github.com/icza/play","Output":"ok  \tgithub.com/icza/play\t0.002s\n"}

{"Time":"2022-05-10T09:26:26.713573313+02:00","Action":"pass","Package":"github.com/icza/play","Elapsed":0.003}

您可以編寫一個簡單的應(yīng)用程序來處理和過濾這些 JSON 對象。或者您可以像過濾任何其他輸出一樣過濾輸出。


的輸出go test -json . |grep '\[custom\]'


{"Time":"2022-05-10T09:28:24.197077681+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"[custom] my message from fmt.Println\n"}

如果您還想要“通過”或“失敗”消息,請運(yùn)行g(shù)o test -json . |grep '"pass"\|"fail"\|\[custom\]'


{"Time":"2022-05-10T09:29:26.069181336+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"[custom] my message from fmt.Println\n"}

{"Time":"2022-05-10T09:29:26.069189228+02:00","Action":"pass","Package":"github.com/icza/play","Test":"TestMy_Test","Elapsed":0}

{"Time":"2022-05-10T09:29:26.069199239+02:00","Action":"pass","Package":"github.com/icza/play","Elapsed":0}



查看完整回答
反對 回復(fù) 2022-12-13
?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個贊

雖然這在測試期間不打印,但它會在測試后立即打印,這比根本不打印要好。


在您的測試文件中定義一個os.File以將消息寫入:


var messagesFile *os.File


func messages() *os.File {

    if messagesFile == nil {

        messagesFile, _ = os.Create("tests.out")

    }

    return messagesFile

}

os.Create如果文件不存在則創(chuàng)建一個新文件,否則截?cái)喱F(xiàn)有文件。


在您的測試中,將消息寫入該文件:


messages().WriteString("my message")

使用 運(yùn)行測試go test,然后cat使用文件。就我而言,我使用make:


test:

    go test .

    @cat tests.out

輸出看起來像:


ok  <path to tests>

my message


查看完整回答
反對 回復(fù) 2022-12-13
?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個贊

您可以創(chuàng)建自己的日志功能并將其用于在屏幕上打印


func Log(args ...interface{}) {

    fmt.Fprintln(os.Stdout, args...)

}

您還可以根據(jù)通過標(biāo)志傳遞的條件打印日志


var p = flag.Bool("p", false, "Enable Local Logging")


func MyLog(args ...interface{}) {

  if *p {

    fmt.Fprintln(os.Stdout, args...)

  }

}

例子


package main


import (

    "fmt"

    "testing"

    "os"

    "flag"

)


var p = flag.Bool("p", false, "Enable Local Logging")


func Log(args ...interface{}) {

  if *p {

    fmt.Fprintln(os.Stdout, args...)

  }

}


func IntMin(a, b int) int {

    if a < b {

        return a

    }

    return b

}


func TestIntMinBasic(t *testing.T) {

    ans := IntMin(2, -2)

    if ans != -2 {

        t.Errorf("IntMin(2, -2) = %d; want -2", ans)

    }

}


func TestIntMinTableDriven(t *testing.T) {

    var tests = []struct {

        a, b int

        want int

    }{

        {0, 1, 0},

        {1, 0, 0},

        {2, -2, -2},

        {0, -1, -1},

        {-1, 0, -1},

    }


    Log("Print to Screen")


    for _, tt := range tests {


        testname := fmt.Sprintf("%d,%d", tt.a, tt.b)

        t.Run(testname, func(t *testing.T) {

            ans := IntMin(tt.a, tt.b)

            if ans != tt.want {

                t.Errorf("got %d, want %d", ans, tt.want)

            }

        })

    }

}


func BenchmarkIntMin(b *testing.B) {

    for i := 0; i < b.N; i++ {

        IntMin(1, 2)

    }

}

并傳遞您可以使用的標(biāo)志-args


-args將命令行的其余部分(-args 之后的所有內(nèi)容)傳遞給測試二進(jìn)制文件,不進(jìn)行解釋和更改。因?yàn)檫@個標(biāo)志占用了命令行的其余部分,所以包列表(如果存在)必須出現(xiàn)在這個標(biāo)志之前。


命令示例:


go test -args -p


查看完整回答
反對 回復(fù) 2022-12-13
  • 3 回答
  • 0 關(guān)注
  • 276 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號