Merge commit '581a1cb85c62292a5a20fc33bec04f035e2b806d' into ja

This commit is contained in:
Shin Kojima
2014-12-17 01:17:45 +09:00

View File

@@ -6,11 +6,11 @@ The Go language comes with a lightweight testing framework called `testing`, and
## How to write test cases
Since `go test` command can only be executed under a corresponding directory of all files, so we are going to create a new project directory `gotest`, so that all of our code and test code are in this directory.
Since the `go test` command can only be executed in a directory containing all corresponding files, we are going to create a new project directory `gotest` so that all of our code and test code are in the same directory.
Next, we create two files in the directory below: gotest.go and gotest_test.go
Let's go ahead and create two files in the directory called gotest.go and gotest_test.go
1. Gotest.go: The document which we have created a package, which has a function in a division operation:
1. Gotest.go: This file declares our package name and has a function that performs a division operation:
<pre>package gotest
@@ -25,15 +25,15 @@ Next, we create two files in the directory below: gotest.go and gotest_test.go
return a / b, nil
}</pre>
2. Gotest_test.go: This is our unit test files, but keep the following principles:
2. Gotest_test.go: This is our unit test file. Keep in mind the following principles for test files:
- File names must be `_test.go` end, so in the implementation of `go test` will be executed when the appropriate code
- You have to import `testing` this package
- All test cases functions must be the beginning of `Test`
- Test case will follow the source code written in the order execution
- Test function `TestXxx()` argument is `testing.T`, we can use this type to record errors or test status
- Test format: `func TestXxx(t * testing.T)`, `Xxx` section can be any combination of alphanumeric, but can not be the first letter lowercase letters [az], for example, `Testintdiv` wrong function name.
- Function by calling `testing.T` a `Error`, `Errorf`, `FailNow`, `Fatal`, `FatalIf` method, the test is not passed, calling `Log` test method is used to record the information.
- File names must end in `_test.go` so that `go test` can find and execute the appropriate code
- You have to import the `testing` package
- All test case functions begin with `Test`
- Test cases follow the source code order
- Test functions of the form `TestXxx()` take a `testing.T` argument; we can use this type to record errors or to get the testing status
- In functions of the form `func TestXxx(t * testing.T)`, the `Xxx` section can be any alphanumeric combination, but the first letter cannot be a lowercase letter [az]. For example, `Testintdiv` would be an invalid function name.
- By calling one of the `Error`, `Errorf`, `FailNow`, `Fatal` or `FatalIf` methods of `testing.T` on our testing functions, we can fail the test. In addition, we can call the `Log` method of `testing.T` to record the information in the error log.
Here is our test code:
@@ -46,20 +46,20 @@ Here is our test code:
func Test_Division_1(t *testing.T) {
// try a unit test on function
if i, e := Division(6, 2); i != 3 || e != nil {
// If it is not as expected, then the error
// If it is not as expected, then the test has failed
t.Error("division function tests do not pass ")
} else {
// record some of the information you expect to record
// record the expected information
t.Log("first test passed ")
}
}
func Test_Division_2(t *testing.T) {
t.Error("just do not pass")
t.Error("just does not pass")
}
We perform in the project directory `go test`, it will display the following information:
When executing `go test` in the project directory, it will display the following information:
--- FAIL: Test_Division_2 (0.00 seconds)
gotest_test.go: 16: is not passed
@@ -67,7 +67,7 @@ We perform in the project directory `go test`, it will display the following inf
exit status 1
FAIL gotest 0.013s
From this result shows the test does not pass, because in the second test function we wrote dead do not pass the test code `t.Error`, then our first case of how a function performs like it ? By default, execute `go test` is not displayed test information, we need to bring arguments `go test-v`, this will display the following information:
We can see from this result that the second test function does not pass since we wrote in a dead-end using `t.Error`. But what about the performance of our first test function? By default, executing `go test` does not display test results. We need to supply the verbose argument `-v` like `go test -v` to display the following output:
=== RUN Test_Division_1
--- PASS: Test_Division_1 (0.00 seconds)
@@ -79,7 +79,7 @@ From this result shows the test does not pass, because in the second test functi
exit status 1
FAIL gotest 0.012s
The above output shows in detail the process of this test, we see that the test function 1 `Test_Division_1` test, and the test function 2 `Test_Division_2` test fails, finally concluded that the test is not passed. Next we modified the test function 2 the following code:
The above output shows in detail the results of our test. We see that the test function 1 `Test_Division_1` passes, and the test function 2 `Test_Division_2` fails, finally concluding that our test suite does not pass. Next, we modify the test function 2 with the following code:
func Test_Division_2(t *testing.T) {
// try a unit test on function
@@ -92,7 +92,7 @@ The above output shows in detail the process of this test, we see that the test
}
}
Then we execute `go test-v`, the following information is displayed, the test passes:
We execute `go test-v` once again. The following information should now be displayed -the test suite has passed~:
=== RUN Test_Division_1
--- PASS: Test_Division_1 (0.00 seconds)