EOS 智能合约测试框架 eosfactory

最近笔者在写完智能合约,想要写一些测试案例,但是 eos 自带的单元测试用起来不是很方便。平常用 cleos 测试的体验感其实挺不错,所以笔者设想有一种是用 cleos 作为与 nodeos 端互动的测试框架,去 github 上找了找, 还真有。https://github.com/tokenika/e... 。 基本能满足大致的需求,但还不是很完善,例如笔者需要用到其他索引,不只是主键索引,但是该测试框架不支持,所以做些了改动,已经提交PR, 还有其他一些小细节,读者可以考虑从我先使用我改动的版本: https://github.com/superoneio...

安装步骤
1.首先 装完 python 3.6 版本的环境,git clone url, 将代码拉取到本地。
2.执行 install.sh 脚本。填入 eosio 源码目录,以及智能合约目录就行了。
本章以 dice 合约作为例子来介绍 eosfactory 在本地测试环境的使用。

eosfactory 是基于 python 的 EOS 智能合约测试框架,它的实现方式其实就是 python 去调用 cleos 来与 nodeos 端进行交互,利用 python 的 unittest 单元测试工具来做测试,unittest 的使用读者可以自行去了解哈,这里笔者就不赘述了。

测试智能合约所需要的几个基本功能:创建账号,执行action, 查看数据表。下面我们通过 dice 合约的单元测试代码来了解这一系列操作。

进行本地环境的初始化操作
# 重置链,即 delete-all-blocks
reset()
# 首先获取 eosio 账号的,这里的 master 指向的就是 eosio 账号。
 create_master_account("master”);
system_contract_path = "/Users/wuyuan/Documents/study/eos/build/contracts/“
# 创建系统账号
create_account("et",master,"eosio.token")
create_account("em",master,"eosio.msig")
create_account("er",master,"eosio.ram")
create_account("erf",master,"eosio.ramfee")
create_account("es",master,"eosio.stake”)
# 部署 token 和 msig 合约
et_contract = Contract(et,system_contract_path + "eosio.token","eosio.token.abi","eosio.token.wasm")
et_contract.deploy();
em_contract = Contract(em,system_contract_path + "eosio.msig","eosio.msig.abi","eosio.msig.wasm")
        em_contract.deploy();
// 创建 EOS 代币
et.push_action("create",{"issuer":"eosio","maximum_supply":"100000000000.0000 EOS"},permission=(et,Permission.ACTIVE))
et.push_action("issue",{"to":"eosio","quantity":"100000000.0000 EOS","memo":"haha"},permission=(master,Permission.ACTIVE))

# 部署系统合约
contract = Contract(master,"/Users/wuyuan/Documents/study/eos/build/contracts/eosio.system","eosio.system.abi","eosio.system.wasm”)
contract.deploy()

# 创建 dice 合约需要的账号
create_account("dice",master,"dice","","","1000","1000",None,"10000")
create_account("alice",master,"alice","","","1000","1000",None,"1000")
create_account("bob",master,"bob","","","1000","1000",None,"1000")

# 赋予代币
et.push_action("transfer",{"from":"eosio","to":"alice","quantity":"10000.0000 EOS","memo":"hi"},permission=("eosio",Permission.ACTIVE))
et.push_action("transfer",{"from":"eosio","to":"bob","quantity":"10000.0000 EOS","memo":"hi"},permission=("eosio",Permission.ACTIVE))
环境的初始化已经完成了,接下来部署 dice 合约以及进行游戏操作。
# init dice contract
self.deploy_contract()

# 将 alice 和 bob 账号的 eosio.code 权限付给 dice 账号
self.updateauth(alice.name)
self.updateauth(bob.name)

# 充值游戏币
self.deposit(alice.name,"100.0000 EOS")
self.deposit(bob.name,"100.0000 EOS”)

# 获取 account 表信息。
account = self.get_account()
print(account)

# 开始游戏
source1 = "28349b1d4bcdc9905e4ef9719019e55743c84efa0c5e9a0b077f0b54fcd84905"
commitment1 = "d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883"
source2 = "15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12"
commitment2 = "50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129"
self.offerbet("3.0000 EOS",alice.name,commitment1)
offer = self.get_offer_by_commitment(commitment1)
print(offer)

self.offerbet("3.0000 EOS",bob.name,commitment2)
offer2 = self.get_offer_by_commitment(commitment2)
print(offer2)

game = self.get_game_by_id(offer2["gameid"])
print(game)

self.reveal(commitment1,source1,alice.name)
game = self.get_game_by_id(offer2["gameid"])
print(game)

self.reveal(commitment2,source2,bob.name)

# 查看 account 表信息,看看是否赢了钱
account = self.get_account()
print(account)

# show global value
print(self.get_global_dice())
OK ,上面说了获取表信息,接下来介绍如何获取表数据
# account_object 账户类,无需传入, table_name 表名, scope , binary 是否显示二进制数据, limit 筛选条数, key 目前无作用, lower 筛选下限,upper 筛选上限, index 索引名 默认主键, key_type 索引类型
table(account_object, table_name, scope="", binary=False, limit=10, key="", lower="", upper="",index="first",key_type="i64")
1.直接获取, table( [ 表名 ],[ scope ],[ 条数,默认为10 ])
def get_account(self):
   try:
       account = dice.table("account”,dice.name,100).json["rows"]
       return account
   except errors.Error as e:
       print("except errors.Error as e: ",e)
   return None

2.通过 lower_bound 和主键索引筛选
def get_offer_by_id(self, id):
    try:
        offer = dice.table("offer",dice.name,False,1,"",id).json["rows"]
        if len(offer) != 0:
            self.assertEqual( offer[0]["id"] , id, "not this game id")
            return offer[0]
    except errors.Error as e:
        print("except errors.Error as e: ",e)
    return None

3.通过 lower_bound 和 非主键索引筛选
def get_offer_by_commitment(self, commitment):
    try:
        # ter 表示 第三个索引 索引类型为 sha256
        offer = dice.table("offer",dice.name,False,1,"",commitment,"","ter","sha256").json["rows"]
        if len(offer) != 0:
        self.assertEqual( offer[0]["commitment"] , commitment, "not this game id")
        return offer[0]
    except errors.Error as e:
        print("except errors.Error as e: ",e)
    return None

从代码我们可以看出,其实就是跟我们平时用 cleos 差不多,只是它将其封装起来供我们调用,这样可以节约我们不少时间,也方便我们调试,写自动化测试脚本。

文中的例子: https://github.com/superoneio...

大家如果以后遇到更好用的测试框架,请务必介绍给我哈。

转载请注明来源: https://eos.live/detail/17418

相关推荐