亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

TestMain - 沒有要運行的測試

TestMain - 沒有要運行的測試

Go
月關寶盒 2023-06-05 18:17:57
我正在編寫一個編譯 C 源文件并將輸出寫入另一個文件的包。我正在為這個包編寫測試,我需要創建一個臨時目錄來寫入輸出文件。我正在使用TestMain函數來做到這一點。出于某種原因,當我剛剛運行測試時,我總是收到“沒有要運行的測試”的警告TestMain。我嘗試調試該TestMain函數,我可以看到確實創建了臨時目錄。當我手動創建testoutput目錄時,所有測試都通過了。我正在從目錄加載兩個 C 源文件testdata,其中一個是故意錯誤的。gcc.go:package gccimport (    "os/exec")func Compile(inPath, outPath string) error {    cmd := exec.Command("gcc", inPath, "-o", outPath)    return cmd.Run()}gcc_test.go:package gccimport (    "os"    "path/filepath"    "testing")func TestOuputFileCreated(t *testing.T) {    var inFile = filepath.Join("testdata", "correct.c")    var outFile = filepath.Join("testoutput", "correct_out")    if err := Compile(inFile, outFile); err != nil {        t.Errorf("Expected err to be nil, got %s", err.Error())    }    if _, err := os.Stat(outFile); os.IsNotExist(err) {        t.Error("Expected output file to be created")    }}func TestOuputFileNotCreatedForIncorrectSource(t *testing.T) {    var inFile = filepath.Join("testdata", "wrong.c")    var outFile = filepath.Join("testoutput", "wrong_out")    if err := Compile(inFile, outFile); err == nil {        t.Errorf("Expected err to be nil, got %v", err)    }    if _, err := os.Stat(outFile); os.IsExist(err) {        t.Error("Expected output file to not be created")    }}func TestMain(m *testing.M) {    os.Mkdir("testoutput", 666)    code := m.Run()    os.RemoveAll("testoutput")    os.Exit(code)}go test輸出:sriram@sriram-Vostro-15:~/go/src/github.com/sriram-kailasam/relab/pkg/gcc$ go test--- FAIL: TestOuputFileCreated (0.22s)        gcc_test.go:14: Expected err to be nil, got exit status 1FAILFAIL    github.com/sriram-kailasam/relab/pkg/gcc        0.257s運行TestMain:Running tool: /usr/bin/go test -timeout 30s github.com/sriram-kailasam/relab/pkg/gcc -run ^(TestMain)$ok      github.com/sriram-kailasam/relab/pkg/gcc    0.001s [no tests to run]Success: Tests passed.
查看完整描述

2 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

#1

嘗試運行TestMain()就像嘗試運行main()。你不這樣做,操作系統為你做這件事。

TestMain是在 Go 1.4 中引入的,用于幫助設置/拆卸測試環境,它被調用而不是運行測試;引用發行說明:

如果測試代碼包含函數

func?TestMain(m?*testing.M)

該函數將被調用而不是直接運行測試。M 結構包含訪問和運行測試的方法。


#2

用于ioutil.TempDir()創建臨時目錄。

tmpDir, err := ioutil.TempDir("", "test_output")

if err != nil {

? ? // handle err

}

它將負責創建目錄。您稍后應該使用os.Remove(tmpDir)刪除臨時目錄。


您可以將它與Tim Peoples的建議稍微修改后的版本一起使用,例如:


func TestCompile(t *testing.T) {

? ? tmpDir, err := ioutil.TempDir("", "testdata")

? ? if err != nil {

? ? ? ? t.Error(err)

? ? }

? ? defer os.Remove(tmpDir)


? ? tests := []struct {

? ? ? ? name, inFile, outFile string

? ? ? ? err? ? ? ? ? ? ? ? ? ?error

? ? }{

? ? ? ? {"OutputFileCreated", "correct.c", "correct_out", nil},

? ? ? ? {"OutputFileNotCreatedForIncorrectSource", "wrong.c", "wrong_out", someErr},

? ? }


? ? for _, test := range tests {

? ? ? ? var (

? ? ? ? ? ? in? = filepath.Join("testdata", test.inFile)

? ? ? ? ? ? out = filepath.Join(tmpDir, test.outFile)

? ? ? ? )


? ? ? ? t.Run(test.name, func(t *testing.T) {

? ? ? ? ? ? err = Compile(in, out)

? ? ? ? ? ? if err != test.err {

? ? ? ? ? ? ? ? t.Errorf("Compile(%q, %q) == %v; Wanted %v", in, out, err, test.err)

? ? ? ? ? ? }

? ? ? ? })

? ? }

}


查看完整回答
反對 回復 2023-06-05
?
米脂

TA貢獻1836條經驗 獲得超3個贊

很可能,您的問題與您傳遞給的模式os.Mkdir(...)值有關。您提供的是666十進制,它是01232八進制的(或者,如果您愿意,權限字符串為d-w--wx-wT),我認為這并不是您真正想要的。


而不是666,您應該指定0666-- 前導 0 表示您的值是八進制表示法。


另外,您的兩個測試實際上是相同的;與其使用 aTestMain(...)來執行設置,我建議使用*T.Run(...)從單個頂級Test*函數執行測試。是這樣的:


gcc_test.go:


package gcc


import (

  "testing"

  "path/filepath"

  "os"

)


const testoutput = "testoutput"


type testcase struct {

  inFile  string

  outFile string

  err     error

}


func (tc *testcase) test(t *testing.T) {

  var (

    in  = filepath.Join("testdata", tc.inFile)

    out = filepath.Join(testoutput, tc.outFile)

  )


  if err := Compile(in, out); err != tc.err {

    t.Errorf("Compile(%q, %q) == %v; Wanted %v", in, out, err, tc.err)

  }

}


func TestCompile(t *testing.T) {

  os.Mkdir(testoutput, 0666)


  tests := map[string]*testcase{

    "correct": &testcase{"correct.c", "correct_out", nil},

    "wrong":   &testcase{"wrong.c", "wrong_out", expectedError},

  }


  for name, tc := range tests {

    t.Run(name, tc.test)

  }

}


查看完整回答
反對 回復 2023-06-05
  • 2 回答
  • 0 關注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號