top of page
  • Writer's picturesandeepseeram

Smart Contracts Development & Testing with Brownie Framework

Brownie is a Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine (EVM).

Brownie is a Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine (EVM). So, when we are thinking about dApp development in Python, our choice of smart contract programming language will be vyper, which is similar to solidity in many aspects. Brownie fully supports both vyper and solidity.

Similarly, we can use Brownie as a testing framework, it provides developers with the ability to use and write pytest test cases for smart contracts.

Brownie takes care of managing contract artifacts, allowing us to write simple, manageable contracts and deployment scripts for custom deployments, library linking, and complex Ethereum applications.

Getting Started

Install Brownie - High-level framework for smart contracts development.

I will be using Ubuntu 22.04 - Jammy Jellyfish and make sure Python3 is installed.

The recommended way of installing brownie is through pipx.

To install pipx on Ubuntu:

# sudo apt install pipx

Next, Install Ganache (it's a framework to run a local blockchain - similar to Remix but in your local environment)

We will need NPM to install the ganache.

# npm install ganache –global

Once installed, if you launch ganache. You will be able to see a local blockchain network running & listening on RPC, you will also find the chain id, available accounts, private keys, mnemonic phase, gas fee etc…now, we have a full up-and-running local blockchain network.

Install Brownie

# pipx install eth-brownie

# brownie --version

When we Initiate the Brownie environment, Brownie scaffold will create various folders in the path, such as build, contracts, interfaces, scripts, tests, etc.

Compiling a Contract

Open the contract folder and create a new file called StoreValue.sol, this is going to be our first smart contract – which is storing a value in the blockchain.

// SPDX-License-Identifier: MIT

    This contract stores a value in the blockchain.

pragma solidity ^0.8.0;

contract StoreValue {
    uint256 myValue;

    function store(uint256 _myValue) public {
        myValue = _myValue;

    function retrieve() public view returns (uint256) {
        return myValue;

Once we have the contract file, we need to compile the contract.

# brownie compile

Now, let's set up our brownie environment.

Previously when we installed ganache, we have the private keys displayed for our local environment. We need the private key to create the brownie account.

# brownie accounts new my-goerli-account

Deploying the Smart Contract locally

Open the scripts folder and create a new file called

from brownie import accounts, StoreValue, network

def deploy_store_value():
    account = get_account()

    storeValue = StoreValue.deploy({"from": account})
    value = storeValue.retrieve()
    transaction =, {"from": account})
    value = storeValue.retrieve()

def get_account():
    if network.show_active() == "development":
        return accounts[0]
        return accounts.load("my-goerli-account")

def main():

Execute the code in your local environment (we are using Ganache behind the scenes)

# brownie run scripts/

Smart Contract Testing:

Open the test folder and create a new file called

We are testing two test cases here, the first being a test case for the actual smart contract deployment and the second being the actual value stored in the blockchain.

from brownie import StoreValue, accounts

def test_deploy():

    account = accounts[0]

    simple_storage = StoreValue.deploy({"from": account})
    value = simple_storage.retrieve()
    expected = 0

    assert value == expected

def test_store():

    account = accounts[0]
    store_value = StoreValue.deploy({"from": account})

    expected = 99
    txn =, {"from": account})

    assert expected == store_value.retrieve()

Execute the testing script

# brownie test -s


Recent Posts

See All


Commenting has been turned off.
bottom of page