-
Notifications
You must be signed in to change notification settings - Fork 39
Test
./godelw test
runs the Go tests in the project.
-
${GOPATH}/src/${PROJECT_PATH}
exists, is the working directory and is initialized as a Git repository and Go module - Project contains
godel
andgodelw
- Project contains
main.go
- Project contains
.gitignore
that ignores GoLand files - Project contains
echo/echo.go
andecho/echoer.go
We will now add some tests to our program. Run the following to add tests for the echo
package:
➜ SRC='package echo_test
import (
"testing"
"PROJECT_PATH/echo"
)
func TestEcho(t *testing.T) {
echoer := echo.NewEchoer()
for i, tc := range []struct {
in string
want string
}{
{"foo", "foo"},
{"foo bar", "foo bar"},
} {
if got := echoer.Echo(tc.in); got != tc.want {
t.Errorf("case %d failed: want %q, got %q", i, tc.want, got)
}
}
}' && SRC=${SRC//PROJECT_PATH/$PROJECT_PATH} && echo "$SRC" > echo/echo_test.go
Run ./godelw test
to run all of the Go tests in the project:
➜ ./godelw test
? github.com/nmiyake/echgo2 [no test files]
ok github.com/nmiyake/echgo2/echo 0.002s
Commit the test to the repository:
➜ git add echo
➜ git commit -m "Add tests for echo package"
[master 17a256c] Add tests for echo package
1 file changed, 22 insertions(+)
create mode 100644 echo/echo_test.go
-
${GOPATH}/src/${PROJECT_PATH}
exists, is the working directory and is initialized as a Git repository and Go module - Project contains
godel
andgodelw
- Project contains
main.go
- Project contains
.gitignore
that ignores GoLand files - Project contains
echo/echo.go
,echo/echo_test.go
andecho/echoer.go
./godelw test
has the following advantages over running go test ./...
:
- Aligns the output so that all of the test times line up
- Only runs tests for files that are part of the project (uses the exclude parameters)
The test task also allows tags to be specified, which allows tests such as integration tests to be treated separately. The integration tests section of the tutorial covers this in more detail.
The ./godelw test --junit-output=<file>
command can be used to generate a JUnit-style output XML file that summarizes
the results of running the tests (implemented using go-junit-report):
➜ ./godelw test --junit-output=output.xml
? github.com/nmiyake/echgo2 [no test files]
=== RUN TestEcho
--- PASS: TestEcho (0.00s)
PASS
ok github.com/nmiyake/echgo2/echo 0.002s
Verify that this operation wrote a JUnit report:
➜ cat output.xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite tests="1" failures="0" time="0.002" name="github.com/nmiyake/echgo2/echo">
<properties>
<property name="go.version" value="go1.16"></property>
</properties>
<testcase classname="echo" name="TestEcho" time="0.000"></testcase>
</testsuite>
</testsuites>
Remove the output by running the following:
➜ rm output.xml
In some instances, we may want to specify flags for the go test
operation -- for example, in the previous section, we
wanted to pass -count=1
to force the tests to run without using cache. Other common test flags include -timeout
to
specify a timeout, -p
to specify the number of test binaries that can be run in parallel, -json
to print the output
as JSON, etc.
The flags provided after the ./godelw test
command are passed directly to the underlying go test
invocation. The
--
separator should be used to ensure that the flags are not interpreted.
For example, the following command prints the output as JSON:
➜ ./godelw test -- -json
{"Time":"2021-04-02T05:57:03.8307725Z","Action":"output","Package":"github.com/nmiyake/echgo2","Output":"? \tgithub.com/nmiyake/echgo2\t[no test files]\n"}
{"Time":"2021-04-02T05:57:03.8309352Z","Action":"skip","Package":"github.com/nmiyake/echgo2","Elapsed":0}
{"Time":"2021-04-02T05:57:03.8892473Z","Action":"run","Package":"github.com/nmiyake/echgo2/echo","Test":"TestEcho"}
{"Time":"2021-04-02T05:57:03.8893115Z","Action":"output","Package":"github.com/nmiyake/echgo2/echo","Test":"TestEcho","Output":"=== RUN TestEcho\n"}
{"Time":"2021-04-02T05:57:03.8893335Z","Action":"output","Package":"github.com/nmiyake/echgo2/echo","Test":"TestEcho","Output":"--- PASS: TestEcho (0.00s)\n"}
{"Time":"2021-04-02T05:57:03.8893573Z","Action":"pass","Package":"github.com/nmiyake/echgo2/echo","Test":"TestEcho","Elapsed":0}
{"Time":"2021-04-02T05:57:03.8893753Z","Action":"output","Package":"github.com/nmiyake/echgo2/echo","Output":"PASS\n"}
{"Time":"2021-04-02T05:57:03.8900363Z","Action":"output","Package":"github.com/nmiyake/echgo2/echo","Output":"ok \tgithub.com/nmiyake/echgo2/echo\t0.003s\n"}
{"Time":"2021-04-02T05:57:03.8910913Z","Action":"pass","Package":"github.com/nmiyake/echgo2/echo","Elapsed":0.004}
- Home
-
Tutorial
- Add gödel to a project
- Add Git hooks to enforce formatting
- Generate IDE project for GoLand
- Format Go files
- Run static checks on code
- Run tests
- Build
- Run
- Dist
- Publish
- Build and push Docker images
- Generate license headers
- Go generate tasks
- Define excludes
- Write integration tests
- Sync a documentation directory with GitHub wiki
- Verify project
- Set up CI to run tasks
- Update gödel
- Update legacy gödel
- Other commands
- Conclusion
- Name
- Philosophy
- Architecture
- Plugins
- Configuration