Testing Programs with Trdelnik
Trdelnik revolutionizes program testing by providing a creative, efficient, and reliable framework for Solana applications.
I. Introduction
A. The importance of program testing
Program testing is a crucial aspect of software development that ensures the reliability and functionality of applications. While traditional testing methods are well-established, adopting innovative approaches can enhance the efficiency and effectiveness of the testing process. In this article, we will explore in detail how to set up program testing using the Trdelnik methodology - a unique and unconventional testing framework. We will provide a step-by-step guide along with code examples to help you implement Trdelník effectively in your testing process.
B. Using Trdelnik as a creative approach to program testing
Anchor has the ability to generate client program code in JavaScript/TypeScript. However, it is not recommended to use JavaScript when security, performance, and reliability are top priorities. Additionally, the generated code does not facilitate testing the Solana validator, deploying the program, or running scripts effectively.
To address these concerns, the Rust Testing Framework for Solana (Trdelnik) is designed to handle all these tasks seamlessly. This testing client comprises a CLI tool and a library equipped with test helpers and APIs that can connect with various plugins. Notably, one of the plugins available is a fuzzer.
Nevertheless, the CLI tool and helper library are just the initial components of this project. The vision extends to generating test scenarios and schemas directly from the Anchor IDL and program code. Furthermore, there is a possibility of developing a Rust/Wasm GUI layer for the client - a powerful tool akin to Postman but specifically tailored for Anchor/Solana. By pursuing these ideas, we can eliminate the unnecessary JavaScript layer and enable the development of Rust-only apps using Anchor.
Security and reliability are increasingly prioritized in the blockchain industry. Trdelnik, coded in Rust, facilitates direct testing of code within the Rust language itself, which is also utilized for Solana programs, eliminating the need for JavaScript. By leveraging the safety features of Rust, Trdelnik aims to mitigate potential issues that might remain undetected when employing other programming languages for testing. Rust's inherent safety advantages contribute to the reduction of undiscovered issues that can occur when utilizing alternative languages in the testing process.
C. Trdelnik as a tool for setting up program testing
Trdelník, a traditional Czech pastry, may seem unrelated to program testing at first glance. However, it offers a creative and efficient way to structure and execute testing procedures. Trdelnik is a new Rust-based testing framework for Solana programs written in the Anchor framework. It is an open-source testing framework that provides a wide range of tools and utilities to streamline program testing. It is designed to be compatible with various programming languages and offers an intuitive and user-friendly interface for writing and executing tests. Trdelnik supports both unit testing and integration testing, making it a versatile choice for developers.
II. Understanding Program Testing
A. Different types of program testing
The 5 testing methods or levels of testing commonly referred to in software development are:
1. Unit Testing: Tests individual code units (functions, methods, classes) in isolation.
2. Integration Testing: Tests the interaction and communication between different software components.
3. System Testing: Tests the complete software system to ensure it meets specified requirements.
4. Acceptance Testing: Validates that the software satisfies user needs and acceptance criteria.
5. Regression Testing: Re-tests previously tested functionality to ensure new changes don't introduce issues.
These testing methods are often applied sequentially, with each level building upon the previous one. They collectively aim to ensure the quality, reliability, and functionality of the software product. However, it's important to note that different methodologies and practices may employ additional or alternative testing methods based on project requirements.
B. Benefits of implementing effective program testing
Effective program testing offers benefits such as: bug detection and prevention; Improved software quality and reliability; cost savings by addressing issues early; customer satisfaction and trust; enhanced code maintainability and scalability. By leveraging effective program testing, organizations can deliver high-quality software that meets user needs, reduces risks, and promotes long-term success in the software development lifecycle.
III. Trdelnik as a Testing Tool
A. Technology involved
- Rust Programming Language: Rust is a brand-new, contemporary programming language intended to ensure both safety and performance. Although there is no garbage collection, its design enables programmers to worry less about manual memory management. Rust is used throughout Solana's entire code base, all on-chain applications, and the majority of its toolings. Since others can simply use your work in their projects, it makes sense to keep doing things this way. Additionally, Rust must be used to develop the Explorer in order for it to be integrated with Trdelnik, making it the natural choice in this case.
- Tokio Runtime: The Tokio runtime is a key component of asynchronous programming in the Rust programming language. It provides an execution environment and coordination mechanisms for writing efficient and concurrent Rust applications.
- Solana Blockchain: Solana blockchain is used whenever the tool is used, mainly by connecting to one of the RPC listening nodes and talking to them via their JSON-RPC protocol or WebSocket endpoint.
- Solana Client: This is a handy wrapper for the WebSocket and JSON-RPC protocols that Solana nodes employ. Therefore, while utilizing Rust to construct their tools, developers are not obliged to write their own clients using HTTP libraries to interface with nodes.
- Solana SDK: Most of the significant type definitions, constants, and practical functions are contained in this crucial dependence of Solana Client.
B. Supported versions
We provide support for specific versions of Anchor and Solana as outlined in the table below:
C. Configuration options and settings
The configuration variables can be modified in the Trdelnik.toml file, which will be generated in the project's root directory.
IV. Setting Up Program Testing with Trdelník
A. Features that are currently being developed
Trdelnik Client
Trdelnik's main purpose is to facilitate the building, deployment, and testing of Anchor programs on local clusters, streamlining the test writing process and executing the test suite against the deployed program.
Trdelnik Explorer
A tool for inspecting transactions and the blockchain, assisting in analyzing alterations within a local cluster.
Trdelnik Fuzz
Introducing the inaugural built-in fuzzy tester for Solana programs, which automates the generation of random input data, relieving developers from the task of manual composition.
Trdelnik Console
Incorporating a built-in console, Trdelnik provides developers with a command prompt interface for seamless program interaction. This feature allows developers to invoke individual transactions or perform interactive deployments conveniently. For an illustrative example, refer to the picture below:
B. Dependencies needed
Install Rust (nightly release)
Install Solana tool suite (stable release)
Install Anchor
C. Installation
Execute the specified Cargo command within your project directory:
If you want to install the specific version, you can run this:
Other commands you might need:
To navigate to your project root directory:
Executing the command will result in the creation of two directories: .program_client and trdelnik-tests. These directories will contain all the essential files needed for the process.
To get some helps:
D. Testing Programs with Trdelnik
Create a Solana program using Rust and Anchor Framework.
Executing the trdelnik build command generates a crate named program_client, which includes automatically generated code for simplified invocation of program instructions.
Ensure that your program employs the correct program ID within the derive_id!(...)macro and in the Anchor.toml file. If the program ID is incorrect, retrieve the public key associated with the key pair being used and replace it in these two locations. You can obtain the program ID (which corresponds to the key pair's public key) using the trdelnik key-pair command. Here's an example:
Executing this command will display details regarding the key pair obtained from program_keypair(7)
Compose tests utilizing the aforementioned crate and employ the #[trdelnik_test] macro.
Execute the command trdelnik test to initiate an automated process that includes launching a local cluster, deploying the program, and running the test suite against it. Within your project directory, execute the provided Cargo command:
Alternatively, include the following line in your Cargo.toml file.
Trdelnik conveniently streamlines and manages the majority of the process, allowing developers to focus solely on their program and write tests in the same language as their programs, eliminating the necessity for JavaScript boilerplate as before.
Here is another example for testing program by Trdelnik, using Macro - a programming construct in the Rust programming language. The macro starts the Solana validator (localnet), runs your program test and then shuts down the validator. The Tokio library provides support for asynchronous operations. The macro used in this context accepts an optional argument called "root" with a default value of "../../". An example usage of the macro is #[trdelnik_test(root = "../../")]. You can observe the expanded form of the macro in the tests of the crate.
Skipping test: To exclude a test from running, you can include the #[ignore] macro.
E. Operations involving personalized structures
In case you desire to test an instruction that includes a custom structure as an argument:
It is necessary to include an import statement for the .program_client crate
While this file is generated automatically, it is important to note that the use statements within it will not be regenerated.
V. Case Study: Implementing Trdelnik Testing
Conducting tests on programs that involve associated token accounts
Trdelnik does not automatically export anchor-spl and spl-associated-token-account, necessitating manual addition.
The trdelnik init command creates a dummy test suite on your behalf.
VI. Tips and Best Practices for Trdelník Program Testing
- To make the most of Trdelnik and ensure effective testing, consider the following best practices:
a. Test Driven Development (TDD): Adopt a Test Driven Development approach, where you write tests before writing the actual code. This promotes a more structured and testable codebase.
b. Continuous Integration (CI): Integrate Trdelnik into your Continuous Integration (CI) pipeline to automate the execution of tests on every code change. This ensures that issues are identified and resolved promptly.
c. Regular Test Maintenance: Maintain your test suite by updating and expanding tests as your codebase evolves. This prevents tests from becoming outdated and ensures ongoing test coverage.
d. Collaborative Testing: Encourage collaboration between developers and testers to ensure a well-rounded testing strategy. Testers can provide valuable insights and help identify potential test scenarios.
- One thing about running the Trdelnik test is that it takes too long - five times more than running the Anchor tests, considering the same amount of code that the two cover. In order to resolve this and make the tests process quicker, it is suggested that one could parallelize them by either deploying the smart contract to different addresses on the same node or spinning up multiple test validator nodes.
VII. Conclusion
The ongoing development of new features like Trdelnik Explorer and Trdelnik Fuzz further expands the capabilities of the framework, enabling transaction inspection and automated input data generation. By embracing Trdelnik, developers can detect and prevent bugs early, improving software quality and reducing costs associated with issue resolution. Collaborative testing efforts are facilitated, allowing developers and testers to work together effectively. Trdelnik revolutionizes program testing by providing a creative, efficient, and reliable framework for Solana applications. Its integration with Rust and the Anchor framework empowers developers to deliver high-quality software, meet users’ needs, and ensure long-term success in the software development lifecycle.
All in all, the current state of development on Solana is improving unceasingly, however, some tedious, error-prone, and lengthy processes still exist. This is what frameworks like Trdelnik are trying to change. Integrated with security and reliability, Trdelnik is a promising tool for program testing in the future. We hope that this tutorial article provided the necessary information for you to revolutionize your program testing process. Break a leg!