Solana: Is there a way to run my tests on the same deployed program instead of setting up a new version with a different ID every time I use `test`

February 3, 2025 2:35 pm Published by

Optimizing Test Execution in Solana: The Challenge of Running Tests Against Deployed Apps

As developers working on decentralized applications (dApps) based on Solana, we often encounter issues when testing our applications. One common issue is running tests multiple times against the same deployed app without changing its ID. This can lead to inconsistent results and wasted resources.

In this article, we explore the issue of running tests against deployed apps and propose a solution using Anchor, a popular testing framework for Solana dApps.

Problem: Running Tests Against Deployed Apps

When you deploy an app to Solana, it creates a new blockchain location. If you want to run tests against that deployed app, you need to use a test (or test anchor) and specify the deployment ID. However, the default behavior of the anchor is to create a new deployment with each test run.

This can lead to inconsistent test results across environments, as the same application can be deployed multiple times for testing. For example:

  • Test 1: Deployments 1-5
  • Test 2: Deployments 6-10
  • Test 3: Deployments 11-15

Each deployment is executed independently, which can cause issues with test results and code coverage.

Solution: Using a “test” with a single deployment ID

To solve this problem, we propose an approach that uses Anchor’s “test” feature. The idea is to use a single deployment ID across multiple tests, allowing the same application to be efficiently executed without changing its ID.

Here’s how to do it:

Step 1. Create a custom test wrapper

Create a new file “custom-test-wrapper.sol” with the following code:

pragma strength ^0.8.0;

import "

contract CustomTestWrapper {

uint256 public testID;

function customTest() public {

// Run your tests here...

// Run your program without changing its ID

solana.test(testID, "ProgramName");

}

}

Step 2: Configure Anchor to use the custom test wrapper

Add a new entry to the custom test wrapper file “anchore.config.json”.

{

// Other configurations...

"testWrappers": {

"customTestWrapper": {

"id": "1", // Your activation ID (e.g. 1234567890abcdef)

"name": "Custom Test Wrapper"

}

}

}

Step 3. Use your custom test wrapper for anchor tests

Add a new entry to the custom test wrapper file “anchore-test.json”:

{

// Other configurations...

"testWrappers": {

"customTestWrapper": {

"id": "1", // Your activation ID (e.g. 1234567890abcdef)

"name": "Custom Test Wrapper"

}

}

}

Now when running tests with “anchor test”, Anchor will use the same deployed application without changing its ID.

Conclusion

Testing against deployed applications can be difficult due to inconsistent identifiers. By implementing a custom test wrapper that uses the same deployment ID across multiple tests, we have solved this issue and optimized Solana test operations. This approach is demonstrated using Anchor as the test framework of choice.

Use Case Example:

Let’s say you have a simple program that performs some mathematical operations:

pragma strength ^0.8.0;

contract MathProgram {

function add(uint256 x, uint256 y) public returns (uint256) {

return x + y;

}

}

You can create a custom test wrapper using the above code and configure it in the “anchore.config.json” file.

“` json

{

// Other configurations…

ETHEREUM INDIVISIBLE FROM

Categorised in:

This post was written by Munna

Comments are closed here.