Test API

pab.test.log(msg: str)

Print message to screen during pytest execution.

pab.test.pytest_addoption(parser)
pab.test.pytest_configure(config)
pab.test._check_dependencies_installed(commands: list[str]) None

Checks if a list of commands are available on the system usin shutil.which.

class pab.test.ParsedGanacheOutput(accounts: list[str], endpoint: str)

Dataclass for ganache data.

accounts: list[str]
endpoint: str
pab.test._parse_ganache_output(proc: Popen) ParsedGanacheOutput

Parses ganache process stdout for accounts and endpoint.

class pab.test.PABPlugin(config)

This plugin will:

  • Verify that ganache-cli and truffle are installed.

  • Start a ganache-cli subprocess.

  • Collect the private keys of all test accounts in the local ganache network.

  • Build and deploy your contracts with truffle into the local ganache network.

  • Collect the addresses of the deployed contracts.

  • Install fixtures

GANACHE_CMD = 'ganache-cli'
TRUFFLE_CMD = 'truffle'
pytest_sessionstart(session)

Pytest start hook

pytest_sessionfinish(session, exitstatus)

Pytest cleanup hook

_start_ganache_process()

Starts ganache-cli process.

_collect_ganache_data()

Parses ganache process output and collects accounts and network data.

_deploy_contracts_with_truffle()

Test, build and deploy contracts with ganache.

class pab.test.Contract(name: 'str', source: 'str', address: 'str', abi: 'str')
name: str
source: str
address: str
abi: str
class pab.test.TruffleDeployer(sources: list[str], network: str)
CMD = 'truffle'
deploy() dict[str, pab.test.Contract]

Spawns a truffle deploy process for each source, parses process output and returns a mapping of contract_name: Contract.

_run_truffle(cwd: str)
_parse_data_and_validate(sources_data: dict[str, dict]) dict[str, pab.test.Contract]
_load_abis(contracts: dict[str, pab.test.Contract]) dict[str, pab.test.Contract]

Load the contract ABIs from the truffle sources ‘build’ directory.

class pab.test.TruffleOutputParser

Parser for truffle output. Currently only supports the deploy process and returns a mapping of contract_name: contract_data.

RE_START_CONTRACT = re.compile("^(Deploying|Replacing) '(.*)'$")
RE_CONTRACT_DATA = re.compile('^> ([a-zA-Z ]+):\\s+(.*)$')
class States(value)

An enumeration.

START_CONTRACT_DATA = 1
IN_CONTRACT_DATA = 2
END_CONTRACT_DATA = 3
OUTSIDE_CONTRACT_DATA = 4
in_state(states: States | list[States])
parse_contracts_data(proc: CompletedProcess) dict[str, dict]

Parses subprocess output. Returns a mapping of contract_name: contract_data.

pab.test._copy_project(dest: Path, ignored_patterns: Optional[list[str]] = None) Path

Copies CWD to dest.

pab.test._set_test_envfile(path: Path, extra_vars: dict[str, str], plugin: PABPlugin)

Creates ‘.env.test’ file in path. extra_vars is a dict of envvars to add to the file. Also adds envvars from plugin.envs and accounts from plugin.accounts.

pab.test._replace_contracts(pab: PAB, plugin: PABPlugin)

Replaces contracts in a PAB instance with the ones from plugin.contracts.

pab.test._temp_environ()

ContextManager that Saves a copy of the environment and restores the environment variables to their original state after exiting. New variables will be removed and value changes will be undone.

pab.test.setup_project(pytestconfig)

This fixture will:

  • Copy project to a temporary directory.

  • Change CWD to temporary directory.

  • Replace the contract adresses loaded from contracts.json with the local addresses.

  • Create a ‘.env.test’ environment file with local accounts and rpc configs.

  • Restore environment variables after execution.

  • Return a PAB instance.

pab.test.get_strat()

Fixture that returns an initialized strategy from a PAB instance.

exception pab.test.PABTestException
exception pab.test.MissingDependencies
exception pab.test.StrategyNotFound