Understanding how to use testnets is essential for developing smart contracts on Ethereum. Here is a basic introduction to what testnets are and how to use them.

What is a testnet?

Testnets simulate the Ethereum network and EVM. They allow developers to upload and interact with smart contracts without paying the cost of gas.

Smart contracts must pay gas for their computations on the Ethereum network. If you rent the Ethereum network to run a contract, you have to pay. However, testnets provide free or unlimited gas. That allows developers to test contracts without having to pay real money for their execution.

Two Testnet Node Flavors

Testnet nodes come in two main flavors:

  1. lightweight Ethereum nodes used for small scale local testnets.

    Ex: ethereumjs-testrpc - Useful for early stage contract development. This is what you will be using most of the time. Calls to lightweight testnet nodes complete very quickly and provide good error messages.

  2. heavyweight Ethereum nodes used for large scale networked testnets.

    Ex: Geth - Useful for connecting to public networked testnets. The most popular public testnet is called Ropsten which is useful during later stage contract development.

    Ropsten is essentially the Ethereum network with free ETH and poor secrutiy.

    Connecting to Ropsten through Geth simulates the real Ethereum network. That makes it appealing for mature contracts that you want to battle test. You can also run your own private testnet cluster with Geth, instead of connecting to the public network Ropsten.

Lightweight Heavyweight
Example ethereumjs-testrpc Geth
Use Case Early Stage Dev Late Stage Dev
Speed Fast Slow
Reliability Mostly OK Very Good

Getting started with ethereumjs-testrpc

See official docs on GitHub

Ethereumjs-testrpc is extremely easy to use. To install testrpc just run:

$ npm install -g ethereumjs-testrpc

and then to start your local Ethereum node run:

$ testrpc

You will see the following output:

Available Accounts  
==================
(0) 0x72cf3d2a2d1bafee28d30a6bd72a6d30b325a7f1
(1) 0x5d236d1e2bb5504c935ac69ed58a36947bb76268
(2...)

Private Keys  
==================
(0) c8b3e209c1a268cf498eb9ee94f227ee10353b29bde109f00dd204ee0539690e
(1) fd52fb33e028cfd61cd8231821d1e93fc7efbae04e06c0b82f067b70df7edcc7
(2...)

HD Wallet  
==================
Mnemonic:      river wood roast damage black creek potato region mesh emotion inherit tilt  
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545  

testrpc includes a few accounts/private keys which you can use to test your contracts. Dapp frameworks like Truffle will take care of account management for you so you can focus on developing your awesome contracts.

Now that your Ethereum node is running in the background, you can begin interacting with it using a library like web3

Notes on using Geth to connect to Ropsten

  • Using Ropsten is only required for more advanced stages of development. It's a pain to set up and Geth is slow. While you can avoid using Geth/Ropsten, avoid it!
  • If you were wondering how you get the free ETH on Ropsten, head to this site.
  • If you want to run a private testnet, you can start Geth behind a firewall.

Connecting to Ropsten using MetaMask

[Update] The easiest way to connect to Ropsten these days is using MetaMask. This browser extension provides easy access to Ropsten using the INFURA public Ethereum nodes. This means you do not have to run your local Geth node. MetaMask injects a web3 object into your frontend which is preconfigured to use the public Ropsten network.

Next Steps

Once you become a testnet ninja, you'll need to master compiling, deploying, and interacting with your contracts. For a high level overview, you might want read 5 Essential Ethereum Dapp Tools. Or if you want to jump into deploying contracts, take a look at Deploying a Contract with MetaMask. Good luck!

Please leave questions or suggestions in the comments! ❤︎

Updates

Success! Check your email, verify the address, and get your guide!