Learn how to write comprehensive tests for Neutron chain modules and features
This tutorial covers how to write integration tests for Neutron chain functionality, including testing custom modules, IBC interactions, and complex workflows.
# For Go-based chain testsgo mod init neutron-integration-testsgo get github.com/neutron-org/neutron/v4/cmd/neutrondgo get github.com/cosmos/cosmos-sdk/testutilgo get github.com/stretchr/testify# For Rust-based contract testscargo add cosmwasm-std --features testingcargo add cw-multi-test
func (s *IntegrationTestSuite) TestIBCTransfer() { val := s.network.Validators[0] // Create IBC transfer transferMsg := &ibctransfertypes.MsgTransfer{ SourcePort: "transfer", SourceChannel: "channel-0", Token: sdk.NewCoin("untrn", sdk.NewInt(1000)), Sender: val.Address.String(), Receiver: "cosmos1...", // Receiver on destination chain TimeoutHeight: clienttypes.NewHeight(1, 1000), } _, err := s.network.SendMsgs(val, transferMsg) s.Require().NoError(err) // Wait for packet to be sent s.Require().NoError(s.network.WaitForNextBlock()) // Verify packet was sent (check events) // Implementation depends on your specific test setup}
# Run all integration testsgo test -v ./...# Run specific test suitego test -v -run TestIntegrationTestSuite# Run with race detectiongo test -v -race ./...# Run with coveragego test -v -coverprofile=coverage.out ./...go tool cover -html=coverage.out
Check port availability and ensure no other test networks are running:
Copy
Ask AI
lsof -i :26657pkill -f neutrond
IBC Tests Timeout
Increase timeout values for IBC operations and ensure relayer is properly configured.
Flaky Tests
Add proper waiting mechanisms and avoid hard-coded delays. Use event-based waiting instead.
Integration testing ensures your Neutron applications work correctly in realistic blockchain environments. These tests catch issues that unit tests might miss and validate the entire system’s behavior.