Javier ⚛ priv/acc 🌒 Profile picture
Smart Contract Engineer. DeFi, security, privacy and AI. Cooking @odin_scan. Ex-@spotify et al. Follow my newsletter for cosmwasm content 👇

Jan 24, 2023, 17 tweets

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/

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

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling