我想從測驗中向控制臺列印一條資訊性訊息,但沒有詳細的測驗輸出。
我嘗試使用以下方法從測驗中列印:
fmt.Println("my message") // STDOUT
fmt.Fprintln(os.Stderr, "my message") // STDERR
t.Log("my message\n") // testing.T log
如果使用,它們都會產生相同的效果,即在控制臺中顯示go test -v
,但如果只是go test
使用則不顯示。
但是,使用go test -v
,我還得到了所有詳細的測驗輸出,例如:
=== 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.
但我想要的是在運行時不記錄所有測驗,但 即使測驗成功,仍然列印 Log 和 Logf 呼叫中的所有文本
有沒有辦法從測驗中列印可見訊息,但看不到RUN
andPASS
訊息?
uj5u.com熱心網友回復:
出于顯而易見的原因,測驗框架“劫持”了標準輸出和錯誤流。所以無論如何,寫入這些流是否出現在輸出中是由測驗框架控制的,除了使用-v
標志顯示或隱藏所有內容之外,它沒有提供“自定義”它的方法。
你可以做的是使用-json
測驗標志:
-json
Log verbose output and test results in JSON. This presents the
same information as the -v flag in a machine-readable format.
所以你得到了所有你會得到的輸出-v
,但是你有一個單獨的 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}
您可以撰寫一個簡單的應用程式來處理和過濾這些 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"}
如果您還想要“通過”或“失敗”訊息,請運行go 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}
uj5u.com熱心網友回復:
您可以創建自己的日志功能并將其用于在螢屏上列印
func Log(args ...interface{}) {
fmt.Fprintln(os.Stdout, args...)
}
您還可以根據通過標志的條件使其列印日志
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)
}
}
并傳遞標志,您可以使用-args
-args
將命令列的其余部分(-args 之后的所有內容)傳遞給測驗二進制檔案,未經解釋且未更改。因為這個標志占用了命令列的剩余部分,所以包串列(如果存在)必須出現在這個標志之前。
命令示例:
go test -args -p
uj5u.com熱心網友回復:
雖然這在測驗期間不會列印,但它會在之后立即列印,這比根本不列印要好。
在您的測驗檔案中定義一個os.File
將訊息寫入:
var messagesFile *os.File
func messages() *os.File {
if messagesFile == nil {
messagesFile, _ = os.Create("tests.out")
}
return messagesFile
}
os.Create
如果一個檔案不存在,則創建一個新檔案,否則截斷現有檔案。
在您的測驗中,將訊息寫入該檔案:
messages().WriteString("my message")
使用 運行測驗go test
,然后cat
使用檔案。就我而言,我使用make
:
test:
go test .
@cat tests.out
輸出如下所示:
ok <path to tests>
my message
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/475366.html
上一篇:如何讓硒打開我的電子應用程式?