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

Add test standards and test templates to CONTRIBUTING.md #1977

Merged
merged 5 commits into from
Jul 13, 2022
Merged

Conversation

AlpinYukseloglu
Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu commented Jul 6, 2022

Closes: #1954

What is the purpose of the change

This PR introduces testing standards and templates to our CONTRIBUTING.md to align expectations on testing while we continue to refactor and clean up our old tests.

Brief Changelog

Add section for writing tests to CONTRIBUTING.md that covers:

  1. A brief overview of table-driven development (with a link to a great intro resource)
  2. A few rules of thumb for test-writing in the Osmosis repo
  3. Three examples/templates (a message-related test, a keeper-related test, and a gamm-related test) with brief intros, permalinks, and code blocks for people to copy-paste from as needed

Testing and Verifying

This change is a trivial rework / code cleanup without any test coverage.

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes? (no)
  • Is a relevant changelog entry added to the Unreleased section in CHANGELOG.md? (no)
  • How is the feature or change documented? (not documented)

@AlpinYukseloglu AlpinYukseloglu changed the title Add test standards and test templates to CONTRIBUTING.mc Add test standards and test templates to CONTRIBUTING.md Jul 6, 2022
Copy link
Contributor

@xBalbinus xBalbinus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm! Great additions on testing!

p.s. be sure to fix the markdown lint errors :)

@stackman27
Copy link
Contributor

stackman27 commented Jul 6, 2022

Thoughts on making the examples collapsible, something like this:

Example #1: [Message-Related Test]
func (suite *KeeperTestSuite) TestBurnDenom() {
  var addr0bal int64
  // Create a denom.
  suite.CreateDefaultDenom()
  // mint 10 default token for testAcc[0]
  suite.msgServer.Mint(sdk.WrapSDKContext(suite.Ctx), types.NewMsgMint(suite.TestAccs[0].String(), sdk.NewInt64Coin(suite.defaultDenom, 10)))
  addr0bal += 10
  for _, tc := range []struct {
  	desc      string
  	amount    int64
  	burnDenom string
  	admin     string
  	valid     bool
  }{
  	{
  		desc:      "denom does not exist",
  		amount:    10,
  		burnDenom: "factory/osmo1t7egva48prqmzl59x5ngv4zx0dtrwewc9m7z44/evmos",
  		admin:     suite.TestAccs[0].String(),
  		valid:     false,
  	},
  	{
  		desc:      "burn is not by the admin",
  		amount:    10,
  		burnDenom: suite.defaultDenom,
  		admin:     suite.TestAccs[1].String(),
  		valid:     false,
  	},
  	{
  		desc:      "burn amount is bigger than minted amount",
  		amount:    1000,
  		burnDenom: suite.defaultDenom,
  		admin:     suite.TestAccs[1].String(),
  		valid:     false,
  	},
  	{
  		desc:      "success case",
  		amount:    10,
  		burnDenom: suite.defaultDenom,
  		admin:     suite.TestAccs[0].String(),
  		valid:     true,
  	},
  } {
  	suite.Run(fmt.Sprintf("Case %s", tc.desc), func() {
  		// Test minting to admins own account
  		_, err := suite.msgServer.Burn(sdk.WrapSDKContext(suite.Ctx), types.NewMsgBurn(tc.admin, sdk.NewInt64Coin(tc.burnDenom, 10)))
  		if tc.valid {
  			addr0bal -= 10
  			suite.Require().NoError(err)
  			suite.Require().True(suite.App.BankKeeper.GetBalance(suite.Ctx, suite.TestAccs[0], suite.defaultDenom).Amount.Int64() == addr0bal, suite.App.BankKeeper.GetBalance(suite.Ctx, suite.TestAccs[0], suite.defaultDenom))
  		} else {
  			suite.Require().Error(err)
  			suite.Require().True(suite.App.BankKeeper.GetBalance(suite.Ctx, suite.TestAccs[0], suite.defaultDenom).Amount.Int64() == addr0bal, suite.App.BankKeeper.GetBalance(suite.Ctx, suite.TestAccs[0], suite.defaultDenom))
  		}
  	})
  }
}

Copy link
Member

@p0mvn p0mvn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work on this. I really like the examples at multiple levels of abstraction

CONTRIBUTING.md Outdated
### Example #1: [Message-Related Test](https://github.com/osmosis-labs/osmosis/blob/f0270d04bd77cc5e1c23f7913118b3c2ba737e97/x/tokenfactory/keeper/admins_test.go#L122-L181)
This type of test is mainly for functions that would be triggered by incoming messages (we interact directly with the message server since all other metadata is stripped from a message by the time it hits the msg_server):

```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: might be just my personal view so please feel free to ignore it if you disagree but I feel like having a link is sufficient.

If there is a link, I usually skip code blocks in READMEs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since our goal is to make it as straightforward (and fast) as possible to copy-paste test templates, I personally prefer to keep the code blocks in there. This also lets us update/change/move the tests being linked to without having to worry about keeping the templates simple and relevant for copy-pasting

CONTRIBUTING.md Show resolved Hide resolved
@AlpinYukseloglu AlpinYukseloglu marked this pull request as ready for review July 7, 2022 08:23
@AlpinYukseloglu AlpinYukseloglu requested a review from a team July 7, 2022 08:23
AlpinYukseloglu and others added 2 commits July 11, 2022 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Start making a test standards / template doc
5 participants