Skip to content
On this page

Test accounts

In tests, you can create and use test accounts to test more complex scenarios, as in this example:

smartpy
@sp.module
def main():

    class MyContract(sp.Contract):

        def __init__(self, admin):
            self.data.adminAccount = admin

        @sp.entrypoint
        def setAdmin(self, newAdmin):
            assert sp.sender == self.data.adminAccount, "Must be admin"
            self.data.adminAccount = newAdmin

@sp.add_test()
def test():
    # Create test accounts
    alice = sp.test_account("Alice")
    bob = sp.test_account("Bob")

    scenario = sp.test_scenario("Admin test", main)
    contract = main.MyContract(alice.address)
    scenario += contract

    # Verify that non-admin account can't change the admin
    contract.setAdmin(
        bob.address,
        _sender = bob,
        _valid = False
    )
    # Verify that the admin can change the admin
    contract.setAdmin(
        bob.address,
        _sender = alice,
    )
    scenario.verify(contract.data.adminAccount == bob.address)
sp.test_account(seed) → test_account

Create a deterministic keypair from a seed string. This function always creates the same keypair for the same seed.

python
# seed is `Alice`
alice = sp.test_account("Alice")

This command produces an object with the following properties:

PropertyTypeDescription
alice.addresssp.addressGives the public-key-hash
alice.public_key_hashsp.key_hashGives the public-key-hash
alice.public_keysp.keyGives the public-key
alice.secret_keysp.stringGives the secret-key
sp.make_signature(secret_key, message, message_format = 'Raw') → sp.signature

Creates a signature compatible with the sp.check_signature(...) function.

The message_format parameter accepts two values:

  • Hex, in which case the message is interpreted as a hexadecimal string
  • Raw (the default) in which case the message is a sp.bytes value, usually the result of an sp.pack call.

INFO

You can't use sp.test_account or sp.make_signature in smart contracts because Michelson can't manipulate secret keys.