Integration tests | Public function response

Hi Sui Devs,

I’m trying to write integration tests for my contract and can’t find an easy solution to test public functions. How do I read bool output in my pytests for a function like this:

public fun state_is_correct(arg0: address) : bool {
  //verify and return state
}

I’m experimenting with JSON RPC, GraphQl and Sui Cli but they seem to be returning transactions and don’t include the return value of the function. Can anyone guide me in the right direction? I’d much appreciate it.

1 Like

You’ve got two questions:

Testing
What test strategy? Unit testing with sui move test or other?

Return values
Executing a transaction does not reveal return values from any composible functions invoked on your, or other, contracts.
However, depending on the SDK you are using there is some form of “Dry Run Transaction” which does.

2 Likes

Hi Fastfrank,

Thank you it was helpful. I have unit tests done separately with sui move tests. This part is for testing the live contract with pytests. I made it work with the code below that uses DryRunTransaction as you suggested. Although it looks ugly to me and I also get warnings that it is depreciated. I think I’m going to have to read more about GraphQl. If you had any examples of code it would be great.

    # Create transaction
    txn = SuiTransaction(client=pysui_client)
    
    # Set up move call
    txn.move_call(
        target=f"{deployment.package_id}::test_object::state_is_correct",
        arguments=[
            test_config.CLOCK_ID
        ],
        type_arguments=[]
    )
    
    # Get the transaction bytes without gas parameters for dry run
    tx_bytes_base_64 = txn.build()
    
   
    # Create the GraphQL query for dry run
    query = gql('''
        query($tx_bytes: String!) {
            dryRunTransactionBlock(txBytes: $tx_bytes) {
                error
                results {
                    returnValues {
                        type {
                            repr
                        }
                        bcs
                    }
                }
                transaction {
                    digest
                }
            }
        }
    ''')
    
    # Execute the dry run query
    try:
        result = gql_client.execute(query, variable_values={"tx_bytes": tx_bytes_base64})
1 Like

So, if you have separate move unit tests, which I also do, we can discuss calling and getting results although you’ve probably figured that out.

The deprecation warning you would only be coming from using the JSON RPC pysui clients/builders.

If you are using GraphQL client, however, there is a prebuilt DryRun query node (instead of doing it via string as you have).

2 Likes