Javier ⚛️ Profile picture
Jan 24, 2023 17 tweets 7 min read Read on X
Frustrated by verbose #ComsWasm smart contract tests?

Today I would like to introduce to the @cosmos community the Robot Pattern for stable, readable, and maintainable tests.

If you are a developer, you don't want to miss this one, I promise 😎

Let's dive in! 🧵

1/
From testing 101:

The QA team checks app functionality via the UI. The tester doesn’t care how the application is written as he just interfaces with the UI.

In case of smart contracts, the tester checks at the frontend to interact with the contracts. #smartcontract #testing

2/
So for the tester to check say an asset swap on a #DeFi protocol, it performs the following actions:

• Visit the frontend
• Go to swap page
• Connect wallet
• Select Asset X from the dropdown
• Enter amount of Asset X to swap
• Click Swap
• Verify swap was successful

3/
If we change something with the implementation of the contract, it really doesn't affect the tester as it needs to perform the same sequence of actions on the frontend.

How can we make the QA process more efficient?

Let's automate it! ⌨️

4/
Let's automate testing on the smart contract side so that the tester doesn’t need to perform this every time we do a new release.

We have done this before, for example by creating a CI pipeline that tests our code every time we push changes to GitHub.

5/
We end up writing a **ginormous** test explaining "How" the operation is done, instead of describing the "What".

The problem?

Test duplication is common: people copy-paste tests, tweak and repeat as the app grows. This leads to an increase in both test size and quantity.

6/
Updating an app with new features or refactoring code can be a headache, as we need to fix both the code and broken tests 😰

But there's a solution:

Meet the Robot Pattern for efficient code testing 🤖

#coding #testing

7/
The Robot Pattern involves creating a "robot" interface that simulates actions between tests & code, simplifying complex interactions.

Originally used in Android dev, this pattern is useful for testing applications with a lot of complex interactions, like smart contracts!

8/
The benefits of using the robot pattern are:

• Simplifies tests, they become easier to read.
• Faster and easier to write tests.
• Abstract test implementation, allowing you to make changes to your code without having to refactor your tests.

9/
Sounds good but how does it look in practice?

Here are two screenshots on how a test from @DA0_DA0 would look like without and with the robot pattern.

Let’s go through it and see how it works.

10/ Verbose CosmWasm test.CosmWasm test using the Rob...
To start, create the robot with the necessary dependencies, in this case the BaseApp for contract interaction.

Next, instantiate the contracts with required parameters.

Once set up, do the actions needed to perform the test.

Finally, make assertions to verify the results.

11/
Now it's more concise and easier to read 😏

The Robot Pattern allows for efficient code reuse. For example, if the distribution message changes, we only need to update the implementation of the distribute_rewards() function once, instead of 4 times on this single test!

12/
An additional benefit of using the Robot Pattern is that it makes it accessible to non-engineers in the team to understand the purpose of the test as the name of the functions within the robot are self explanatory!

13/
Want to write another test? Reuse the functions on robot, just pass different configurations and boom, there we have it 💥

You can take look at the full implementation of the testing robot on the following commit: github.com/javierjmc/dao-…

@JakeHartnell @0xekez

14/
If you like this thread follow me for more. Pls tag any dev you think will find this useful!

Now I am curious!

• what are your thoughts about this?
• would you use it?
• what can we improve it?

Let me know!

FIN
Btw nothing wrong with your test @vernonjohnson_ 😅 I just took a random test from DAO DAO and rewrote it using this pattern.
P.S. Here the source of inspiration in case you wonder where this all comes from @JakeWharton

Implementing a robot in Rust is not as clean as in Kotlin but it's still good!

jakewharton.com/testing-robots/ #Android #Kotlin

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Javier ⚛️

Javier ⚛️ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @jvr0x

May 7, 2023
Are you a #CosmWasm developer?

This is what happened this week in the Cosmos you should be aware of:

@axelarcore's General Message Passing (GMP) is now available between EVM and Cosmos-based chains.

#CosmWasm 1.3 is on its way and with it a few additions to our beloved structs Addr, Uint* and Decimal* 😎

The Atom Economic Zone is getting traction with 3 proposals:

#790 -> Adopt Liquid Staking Module on the Hub: PASSED
#792 -> Launch Neutron with Replicated Security: PASSED
#794 -> Launch Stride with Replicated Security: Voting (99% passed)

Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(