Unit Tests
Unit tests are the lowest test category of the test pyramid. All packages and modules should have unit test coverage. Modules should have their dependencies mocked: this means mocking keepers. The SDK usesmockgen
to generate mocks for keepers:
scripts/mockgen.sh
Example
As an example, we will walkthrough the keeper tests of thex/gov
module.
The x/gov
module has a Keeper
type requires a few external dependencies (ie. imports outside x/gov
to work properly).
x/gov/keeper/keeper.go
x/gov
, we mock the expected keepers and instantiate the Keeper
with the mocked dependencies. Note that we may need to configure the mocked dependencies to return the expected values:
x/gov/keeper/common_test.go
x/gov
module without having to import other modules.
x/gov/keeper/keeper_test.go
Keeper
instance.
x/gov/keeper/keeper_test.go
Integration Tests
Integration tests are at the second level of the test pyramid. In the SDK, we locate our integration tests under/tests/integrations
.
The goal of these integration tests is to test how a component interacts with other dependencies. Compared to unit tests, integration tests do not mock dependencies. Instead, they use the direct dependencies of the component. This differs as well from end-to-end tests, which test the component with a full application.
Integration tests interact with the tested module via the defined Msg
and Query
services. The result of the test can be verified by checking the state of the application, by checking the emitted events or the response. It is adviced to combine two of these methods to verify the result of the test.
The SDK provides small helpers for quickly setting up an integration tests. These helpers can be found at https://github.com/cosmos/cosmos-sdk/blob/main/testutil/integration.
Example
testutil/integration/example_test.goDeterministic and Regression tests
Tests are written for queries in the Cosmos SDK which havemodule_query_safe
Protobuf annotation.
Each query is tested using 2 methods:
- Use property-based testing with the
rapid
library. The property that is tested is that the query response and gas consumption are the same upon 1000 query calls. - Regression tests are written with hardcoded responses and gas, and verify they don’t change upon 1000 calls and between SDK patch versions.
Simulations
Simulations uses as well a minimal application, built withdepinject
:
You can as well use the
AppConfig
configurator
for creating an AppConfig
inline. There is no difference between those two ways, use whichever you prefer.x/gov/
simulations:
x/gov/simulation/operations_test.go
End-to-end Tests
End-to-end tests are at the top of the test pyramid. They must test the whole application flow, from the user perspective (for instance, CLI tests). They are located under/tests/e2e
.
For that, the SDK is using simapp
but you should use your own application (appd
). Here are some examples:
- SDK E2E tests: https://github.com/cosmos/cosmos-sdk/tree/main/tests/e2e.
- Cosmos Hub E2E tests: https://github.com/cosmos/gaia/tree/main/tests/e2e.
- Osmosis E2E tests: https://github.com/osmosis-labs/osmosis/tree/main/tests/e2e.
The SDK is in the process of creating its E2E tests, as defined in ADR-59. This page will eventually be updated with better examples.