How to use test-tools to build functional tests for hive

in #test3 years ago

image.png

Recently a new tool has been developed by @blocktrades' team: https://gitlab.syncad.com/hive/test-tools/

If you are working on hive core it's a really powerful tool to create tests. What is does basically is spin up a hived node using whatever configuration you write down in your code. And it also allows you to also seamlessly use the wallet binary and execute commands from it.

But setting it up can prove to be a slight challenge so I figured I'd provide a small guide to save you the time I spend figuring things out.

First you need to have a hived built somewhere, I won't cover how to do it here but you can dig through my history or what @gtg wrote there is plenty of documentation everywhere.

Installation

git clone [email protected]:hive/test-tools.git

Once you have the repository, you may be tempted to just execute ./install.py but this script requires test tools to be in your pythonpath already which is not the case. So you could provide it as params but I went ahead and added it in my .bashrc, along with a new var HIVE_BUILD_ROOT_PATH which is the directory in which you built hive. That way ./install.sh will detect the different binaries.

export PYTHONPATH="${PYTHONPATH}:/home/howo/projects/test-tools/package"
export HIVE_BUILD_ROOT_PATH="/home/howo/projects/hive/build"

then source your .bashrc or start a new shell and execute install.py

you will get something along those lines:

You have successfully installed test-tools.

Details:
  Symlink to [1] created in [2].
  [1] /home/howo/projects/test-tools/package/test_tools
  [2] /home/howo/.local/lib/python3.8/site-packages/test-tools

Paths are configured correctly:
hived: /home/howo/projects/hive/build/programs/hived/hived
cli_wallet: /home/howo/projects/hive/build/programs/cli_wallet/cli_wallet
get_dev_key: /home/howo/projects/hive/build/programs/util/get_dev_key

and at this point you are ready to execute scripts !

Test tools hello world

Here's a simple script to execute:

from test_tools import World, Asset, Wallet

if __name__ == '__main__':
    with World() as world:
        node = world.create_init_node()
        node.run()

        wallet = Wallet(attach_to=node)
        print(wallet.api.transfer('initminer', "hive.fund", Asset.Test(100), 'memo', 'true'))
        print(wallet.api.get_account("hive.fund"))

All of the cli wallet api is supported so feel free to play with it, there are also some more advanced options like replaying snapshots, specifying options to the node etc.

Side note: On my computer launching a node from scratch takes more than the default 20 seconds timeout. So you may want to locally update __DEFAULT_WAIT_FOR_LIVE_TIMEOUT in node.py, I have mine set to 120, I have raised an issue here: https://gitlab.syncad.com/hive/test-tools/-/issues/3 to make it more configurable.

Setting up test tools with pycharm

I had to setup a bunch of things in pycharm to get it to work so here's a small guide:

go to:

files -> settings -> project: yourprojectName -> python interpreter

there click on the cog next to the python interpreter, click "show all"

Then on the window that opens, click on the tree button (the last button on the right):
image.png

This will open a dialog "interpreter paths, on there click the little "+" icon and add the path to test tools:

for me that's /home/howo/projects/test-tools/package

I tried a bunch of solutions to get pycharm and test-tools to recognize HIVE_BUILD_ROOT_PATH but to no avail so I ended up just adding it to the environement variables in my run config:

image.png

And there you have it ! It should then work :)

Sort:  

Amazing!

Congratulations @howo! Your post has been a top performer on the Hive blockchain and you have been rewarded with the following badge:

Post with the highest payout of the day.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Nice job keep the good work

you are doing fantastic job @howo keep it up always....
thanyou!

great package. me likey.