Solidity

布朗尼測試跳過一個函式

  • February 2, 2022

所以我想測試我的彩票合約和 python 腳本。我有三個功能,test_get_entrance_fee()test_cant_enter_unless_started()``can_start_and_enter_lottery()

出了這三個,只有test_get_entrance_fee()test_cant_enter_unless_started()執行正常並通過。但是,第三個函式can_start_and_enter_lottery()沒有執行,並且每次都被取消選擇。

以下是我的測試文件程式碼:

from brownie import Lottery, accounts, config, network, web3, exceptions
import pytest
from scripts.deploy_lottery import deploy_lottery
from web3 import Web3

from scripts.helpful_scripts import LOCAL_BLOCKCHAIN_ENVIRONMENTS, get_account


def test_get_entrance_fee():
   if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
       pytest.skip()
   
   lottery = deploy_lottery()
   
   
   expected_entry_fee = Web3.toWei(0.025, "ether")
   entrance_fee = lottery.getEntranceFee()
   # assert
   assert expected_entry_fee == entrance_fee


def test_cant_enter_unless_started():
   
   account = get_account()
   if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
       pytest.skip()

   lottery = deploy_lottery()
   fee = lottery.getEntranceFee() + 200
   

   
   with pytest.raises(AttributeError):
       lottery.enter({"from": account, "value": fee})


def can_start_and_enter_lottery():
   
   if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
       pytest.skip()
   lottery = deploy_lottery()
   account = get_account()
   fee = lottery.getEntranceFee() + 200

   lottery.startLottery({"from": account})
   
   lottery.enter({"from": account, "value": fee})
   
   assert lottery.players(0) == account

執行後終端顯示的資訊brownie test如下:

======================================== 2 passed in 5.45s =========================================

執行後終端中的資訊brownie test -k can_start_and_enter_lottery()如下:

collected 2 items / 2 deselected

====================================== 2 deselected in 0.02s ======================================= 

您的函式必須以test前綴開頭。

can_start_and_enter_lottery->test_can_start_and_enter_lottery

引用自:https://ethereum.stackexchange.com/questions/120499