Testing
GOX includes a testing system to run applications against the application without starting an external HTTP server.
The tests are written in files .gox within tests/ and can validate HTTP routes, Actions, sessions and flows made up of several requests. The same system allows you to run Benchmarks on the actual handlers of the application.
First test
module indextest
import "github.com/gox-lang/gox/test"
func IndexTest() {
request := test.NewRequest{Method: test.GET, URL: "/index"}
expected := test.NewResponseExpected{
Status: 200,
Body: test.ExpectedBody.Contains("Hello Gox"),
}
result, match := test.Execute(test.Config{}, []test.ExecutePipeLine{
{
Request: request,
Expected: expected
},
})
if !match {
test.Fail("Index Test Fail")
}
test.Explain(result)
test.Save(result)
} The function creates a request, defines its expectations And gather them in a pipeline. test.Execute() returns the detailed result and a value that indicates if all expectations were met.
Execution
From the root of the project it executes:
gox test GOX recursively discovers the files .gox located in tests/, compiles its test functions and shows the result of each. Execution to select files, filter tests and save a global report.
Structure
tests/
├── index.gox
├── snapshots/
└── results/ Test files can be organized in subfolders. snapshots maintain expected bodies and test.Save() writes the detailed results within tests/results/.
Integration with Go
The tests use functions, structures, slices and maps with the same syntax used by the rest of GOX. github.com/gox-lang/gox/test provides the public types necessary to describe requests, expectations and pipelines.
Each file .gox is compiled as part of the execution of gox test. The application is loaded with its actual configuration, but the requests are processed in memory by an isolated runtime for the test.
Best practices
- Keep every test focused on observable behavior.
- It uses names that end in
Testand describe the validated case. - Group in a pipeline only requests belonging to the same flow.
- Save detailed results when they are useful for diagnosing failures.
- It uses benchmarking after checking the functional behavior.