Subroutines
Subroutines allow direct testing of internal contract logic without full application calls.
Overview
Section titled “Overview”The @algopy.subroutine decorator exposes contract methods for isolated testing within the Algorand Python Testing framework. This enables focused validation of core business logic without the overhead of full application deployment and execution.
@algopy.subroutine decorator is optional for the methods in a contract which are not callable externally.
- Decorate internal methods with
@algopy.subroutine:
from algopy import subroutine, UInt64
class MyContract: @subroutine # optional def calculate_value(self, input: UInt64) -> UInt64: return input * UInt64(2)- Test the subroutine directly:
import algopy_testing
def test_calculate_value(context: algopy_testing.AlgopyTestContext): contract = MyContract() result = contract.calculate_value(UInt64(5)) assert result == UInt64(10)Benefits
Section titled “Benefits”- Faster test execution
- Simplified debugging
- Focused unit testing of core logic
Best Practices
Section titled “Best Practices”- Use subroutines for complex internal calculations
- Prefer writing
puresubroutines in ARC4Contract classes - Combine with full application tests for comprehensive coverage
- Maintain realistic input and output types (e.g.,
UInt64,Bytes)
Example
Section titled “Example”For a complete example, see the simple_voting contract in the examples section.