How example can be run in test?

82 views
Skip to first unread message

Harry

unread,
Sep 12, 2016, 7:46:24 PM9/12/16
to golang-nuts
Hi there,

I'm trying to run example in test.
However after running, warning is shown.
```
testing: warning: no tests to run
```

This example code include // Output comment.

```
func ExampleMethod1() {
fmt.Printf("result: %x", Method1())
// Output: xxxx
}
```

What is wrong? When running test, I exec like this.

```go test -v xxxx_test.go```


Thanks.

Harry

nic...@toothfairy.com

unread,
Sep 16, 2016, 1:09:17 PM9/16/16
to golang-nuts
you are using the testing framework incorrectly- https://golang.org/pkg/testing/

your function is incompatible with the test harness. The function signature must follow a few rules.

Your test file does not have any functions matching the required signature.

What you are doing is running an example. The test harness ran your function with no problems so you got no output.

// This Example function fails in the test harness as the fmt.Println output does not match the expected output conveyed within the Output tag. The test harness throws a fit.
func ExampleMethod1 ( ) {
 fmt.Println("hi")
// Output: hello
}

// This Example function passes in the test harness as the fmt.Println output does match the expected output conveyed within the Output tag. The test harness is silent.
func ExampleMethod1 ( ) {
 fmt.Println("hi")
// Output: hi

James Bardin

unread,
Sep 16, 2016, 5:52:30 PM9/16/16
to golang-nuts
This is fixed in master. In go1.7 the Examples and Benchmarks aren't run if there are no Test functions.

Harry

unread,
Sep 17, 2016, 2:19:17 AM9/17/16
to golang-nuts
I got it.
In my case, there was only Example func.
When adding something Testxxx func, it worked well.


2016年9月17日土曜日 6時52分30秒 UTC+9 James Bardin:
Reply all
Reply to author
Forward
0 new messages