How to test the main package functions in golang?

后端 未结 4 1836
情书的邮戳
情书的邮戳 2021-01-03 17:43

I want to test a few functions that are included in my main package, but my tests don\'t appear to be able to access those functions.

My sample main.go file looks li

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-03 17:57

    when you specify files on the command line, you have to specify all of them

    Here's my run:

    $ ls
    main.go     main_test.go
    $ go test *.go
    ok      command-line-arguments  0.003s
    

    note, in my version, I ran with both main.go and main_test.go on the command line

    Also, your _test file is not quite right, you need your test function to be called TestXXX and take a pointer to testing.T

    Here's the modified verison:

    package main
    
    import (
        "testing"
    )
    
    func TestFoo(t *testing.T) {
        t.Error(foo())
    }
    

    and the modified output:

    $ go test *.go
    --- FAIL: TestFoo (0.00s)
        main_test.go:8: Foo
    FAIL
    FAIL    command-line-arguments  0.003s
    

提交回复
热议问题