spn icon indicating copy to clipboard operation
spn copied to clipboard

Normalize test cases format in unit tests

Open lumtis opened this issue 3 years ago • 0 comments

1- Normalize test cases

We currently don't have a standard for the name of the test cases in table tests Most of the time, the test case contains as a name the initial state for the specific test case. This is an example for MsgTriggerLaunch:

{
	name: "launch chain not launched",
	msg:  sample.MsgTriggerLaunch(coordAddress, chainID),
},
{
	name: "non existent chain id",
	msg:  sample.MsgTriggerLaunch(coordAddress, chainIDNoExist),
	err:  types.ErrChainNotFound,
},
{
	name: "non existent coordinator",
	msg:  sample.MsgTriggerLaunch(coordNoExist, chainID2),
	err:  profiletypes.ErrCoordAddressNotFound,
},

In my opinion, the unit tests would be more visual, if we can describe as a name, the actual purpose of the test, what the test must assert. Example of a standard:

  • successful case: should allow <action> [when/if <state>]
  • failure case should prevent <action> [when/if <state>]
  • some other case should ... is sufficient

Validate method:

  • should validate
  • should prevent validate
{
	name: "should allow a chain launch to be triggered when the chain launch is not yet triggered",
	msg:  sample.MsgTriggerLaunch(coordAddress, chainID),
},
{
	name: "should prevent triggering the launch of a non existent chain",
	msg:  sample.MsgTriggerLaunch(coordAddress, chainIDNoExist),
	err:  types.ErrChainNotFound,
},
{
	name: "should prevent triggering the launch of a chain from a non existent coordinator",
	msg:  sample.MsgTriggerLaunch(coordNoExist, chainID2),
	err:  profiletypes.ErrCoordAddressNotFound,
},

2- Add test description for non TDT tests

For tests where we don't use TDT format. We should include the assertion in sub-tests and add descriptions for better readability

Example:

func TestGenesis(t *testing.T) {
	ctx, tk, _ := testkeeper.NewTestSetup(t)
	r := sample.Rand()

	genesisState := sample.ProfileGenesisState(r)
	profile.InitGenesis(ctx, *tk.ProfileKeeper, genesisState)
	got := profile.ExportGenesis(ctx, *tk.ProfileKeeper)

	// Compare lists
	require.ElementsMatch(t, genesisState.ValidatorList, got.ValidatorList)
	require.ElementsMatch(t, genesisState.ValidatorByOperatorAddressList, got.ValidatorByOperatorAddressList)
	require.ElementsMatch(t, genesisState.CoordinatorList, got.CoordinatorList)
	require.ElementsMatch(t, genesisState.CoordinatorByAddressList, got.CoordinatorByAddressList)
	require.Equal(t, genesisState.CoordinatorCounter, got.CoordinatorCounter)
}

to

func TestGenesis(t *testing.T) {
	ctx, tk, _ := testkeeper.NewTestSetup(t)
	r := sample.Rand()

	t.Run("should allow importing and exporting genesis", func(t *testing.T) {
		genesisState := sample.ProfileGenesisState(r)
		profile.InitGenesis(ctx, *tk.ProfileKeeper, genesisState)
		got := profile.ExportGenesis(ctx, *tk.ProfileKeeper)

		// Compare lists
		require.ElementsMatch(t, genesisState.ValidatorList, got.ValidatorList)
		require.ElementsMatch(t, genesisState.ValidatorByOperatorAddressList, got.ValidatorByOperatorAddressList)
		require.ElementsMatch(t, genesisState.CoordinatorList, got.CoordinatorList)
		require.ElementsMatch(t, genesisState.CoordinatorByAddressList, got.CoordinatorByAddressList)
		require.Equal(t, genesisState.CoordinatorCounter, got.CoordinatorCounter)
	})
}

Example

Normalize test cases for launch module https://github.com/tendermint/spn/pull/842

Tasks

  • [x] https://github.com/tendermint/spn/issues/843
  • [ ] https://github.com/tendermint/spn/issues/844
  • [ ] https://github.com/tendermint/spn/issues/845
  • [ ] https://github.com/tendermint/spn/issues/846
  • [ ] https://github.com/tendermint/spn/issues/847
  • [x] https://github.com/tendermint/spn/issues/848
  • [ ] https://github.com/tendermint/spn/issues/849
  • [x] https://github.com/tendermint/spn/issues/850

mint should be handled in the future, we should first adapt the tests of the module with our testkeeper utility suite

lumtis avatar Mar 02 '22 08:03 lumtis