unit testing - How to test the main package functions in golang? -
i want test few functions included in main package, tests don't appear able access functions.
my sample main.go file looks like:
package main import ( "log" ) func main() { log.printf(foo()) } func foo() string { return "foo" }
and main_test.go file looks like:
package main import ( "testing" ) func foo(t testing.t) { t.error(foo()) }
when run go test main_test.go
get
# command-line-arguments .\main_test.go:8: undefined: foo fail command-line-arguments [build failed]
as understand, if moved test file elsewhere , tried importing main.go file, couldn't import it, since it's package main
.
what correct way of structuring such tests? should remove main
package asides simple main function run , test functions in own package, or there way me call functions main file during testing?
when specify files on command line, have specify of them
here's run:
$ ls main.go main_test.go $ go test *.go ok command-line-arguments 0.003s
note, in version, ran both main.go , main_test.go on command line
also, _test file not quite right, need test function called testxxx , take pointer testing.t
here's modified verison:
package main import ( "testing" ) func testfoo(t *testing.t) { t.error(foo()) }
and modified output:
$ go test *.go --- fail: testfoo (0.00s) main_test.go:8: foo fail fail command-line-arguments 0.003s
Comments
Post a Comment