Tests Runner¶
Basic Example¶
This is the simplest way:
package tests
import (
"testing"
"github.com/componego/componego/tests/runner"
"secret.com/project-x/tests/mocks"
)
func TestExample(t *testing.T) {
env, cancelEnv := runner.CreateTestEnvironment(t, mocks.NewApplicationMock(), nil)
t.Cleanup(cancelEnv)
// ... here you can use application environment.
}
The last argument of the function accepts the test options, including driver options, that will be applied to the current test.
When the environment is canceled, all necessary functions will be called to stop the application.
The framework is thread-safe so you can run tests in parallel. However, your personal code or the code of third-party libraries you use may not be thread-safe.
Note
In the example above, you can see how to test an application.
To test component, you must create an application that depends only on that component. For example, this way you can configure a component, because this method is in any application.
Test Mode¶
In tests, the application should be launched in componego.TestMode. There are different application launch modes. However, for tests, it is recommended to use componego.TestMode.
The code above runs the application in this mode, so please consider this detail in your implementation.