Writing tests
The GOX tests are functions without parameters declared in files .gox inside the folder tests/.
GOX recognizes as a test any normal function whose name ends in Test. Pages, Actions and functions with parameters are not recorded as tests.
Test file
module indextest
import "github.com/gox-lang/gox/test"
func IndexTest() {
request := test.NewRequest{
Method: test.GET,
URL: "/index"
}
expected := test.NewResponseExpected{
Status: 200
}
result, match := test.Execute(test.Config{}, []test.ExecutePipeLine{
{
Request: request,
Expected: expected
},
})
if !match {
test.Fail("Index debe responder con 200")
}
test.Explain(result)
} The file declares its module, matters gox/test and defines a function without parameters. The test can contain any GOX logic necessary to prepare data or interpret the result.
Names
The suffix Test it is mandatory to:
func IndexTest() {}
func LoginTest() {}
func SessionPipelineTest() {} The names participate in the filters of executionand so they must clearly express the validated behavior.
Anatomy
A test usually contains four parts:
- One request HTTP or an request for Action.
- The contract of expectations.
- One pipeline sent to
test.Execute(). - The assessment and explanation of the outcome.
test.Execute() can be called more than once within the same function. Each execution produces a separate result and appears separately in the detailed output.
Pass and Fail
test.Fail() Mark the function as failed and record the message received. test.Pass() allows to add information messages to a correct test.
if !match {
test.Fail("No se recibió la respuesta esperada")
}
test.Pass("La ruta respondió correctamente") test.Explain(result) adds a diagnosis when there are differences. Results to know the inspection and storage tools.
Integration with Go
The test functions maintain the model of normal Go functions: they do not receive a special object and do not depend on an exclusive syntax for assertions. test returns values that can be assessed with ordinary flow control.
The selection of functions occurs during file compilation .gox. Only normal functions are exported to the runner, without parameters and finished in Test.
Best practices
- Use a module name related to the file.
- Finish all test names in
Test. - Write error messages that identify the expected behavior.
- Avoid mixing independent cases within the same function.
- Use
test.Explain()to keep useful information on differences.