Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize test cases format in unit tests #583

Closed
8 tasks done
lumtis opened this issue Mar 2, 2022 · 1 comment
Closed
8 tasks done

Normalize test cases format in unit tests #583

lumtis opened this issue Mar 2, 2022 · 1 comment
Assignees
Labels
Epic Epic issue test Improve tests

Comments

@lumtis
Copy link
Contributor

lumtis commented Mar 2, 2022

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 #842

Tasks

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

@lumtis lumtis added the test Improve tests label Mar 2, 2022
@lumtis lumtis modified the milestones: Milestone B, Test Refactoring May 26, 2022
@lumtis lumtis changed the title Normalize table test case names in unit tests Normalize test cases format in unit tests Jun 9, 2022
@lumtis lumtis mentioned this issue Jun 9, 2022
4 tasks
@lumtis lumtis added the Epic Epic issue label Jun 9, 2022
@lumtis lumtis removed this from the Test Refactoring milestone Aug 22, 2022
@aljo242 aljo242 self-assigned this Sep 28, 2022
@lumtis
Copy link
Contributor Author

lumtis commented Oct 3, 2022

Great work @aljo242 🙏

@lumtis lumtis closed this as completed Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Epic Epic issue test Improve tests
Projects
None yet
Development

No branches or pull requests

2 participants