Subroutines | Algorand Developer Portal

Subroutines

Subroutines allow direct testing of internal contract logic without full application calls.

import algopy

import algopy_testing

from algopy_testing import algopy_testing_context

# Create the context manager for snippets below

ctx_manager = algopy_testing_context()

# Enter the context

context = ctx_manager.__enter__()

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.

Usage

  1. Decorate internal methods with @algopy.subroutine:
from algopy import subroutine, UInt64

class MyContract:

@subroutine

def calculate_value(self, input: UInt64) -> UInt64:

return input * UInt64(2)
  1. Test the subroutine directly:
def test_calculate_value(context: algopy_testing.AlgopyTestContext):

contract = MyContract()

result = contract.calculate_value(UInt64(5))

assert result == UInt64(10)

Benefits

Best Practices

Example

For a complete example, see the simple_voting contract in the examples section.

ctx_manager.__exit__(None, None, None)