Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Solidity Programming Essentials

You're reading from  Solidity Programming Essentials

Product type Book
Published in Apr 2018
Publisher Packt
ISBN-13 9781788831383
Pages 222 pages
Edition 1st Edition
Languages

Table of Contents (17) Chapters

Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Introduction to Blockchain, Ethereum, and Smart Contracts Installing Ethereum and Solidity Introducing Solidity Global Variables and Functions Expressions and Control Structures Writing Smart Contracts Functions, Modifiers, and Fallbacks Exceptions, Events, and Logging Truffle Basics and Unit Testing Debugging Contracts Other Books You May Enjoy Index

Chapter 9. Truffle Basics and Unit Testing

Programming languages need a rich ecosystem of tools that eases development. Like any application, even blockchain based decentralized applications should have a minimal Application Lifecycle Management (ALM) process. It is important for any application to have a process of build, test, and deploy continuously. Solidity is a programming language and needs support from other tools to ensure that developers can develop, build, test, and deploy contracts with ease rather than going through the painful process of deploying and testing them. This improves their productivity and eventually helps bring the application to market faster, better, and cheaper. It is also possible to introduce DevOps for smart contracts with the help of such tools. Truffle is one such development, testing, and deployment utility that can make these activities a breeze.

This chapter covers the following topics: 

  • Application development life cycle management
  • Understanding and installing...

Application development life cycle management


As mentioned before, every serious application has some development process built around it. Typically, it involves designing, building, testing, and deploying. The contract ALM is no different from any other software or programming development life cycle. The first step in contract development is to get and finalize requirements about the problem under consideration. Requirements form the starting activity for any decentralized application. Requirements contain descriptions of problems, use cases, and detailed testing strategy.

Architects take functional and technical requirements as their inputs and create application architecture and design. They also document them using notations easily understandable by others. The project development team takes these architecture and design documents and breaks them down into features and sprints. The development team starts working on building contracts and other artifacts based on this documentation. The...

Truffle


Truffle is an accelerator that helps increase the speed of development, deployment and testing, and increases developer productivity. It is built specifically for Ethereum-based contract and application development. The latest Truffle version is 4. It is a node runtime-based framework that can help implement DevOps, continuous integration, continuous delivery, and continuous deployment with ease.

Installing Truffle is quite simple— a prerequisite for installing Truffle is Node.js, as it is deployed as a node package.

Truffle can be installed by executing the following npm command from the command line:

$ npm install -g truffle

Here npm refers to node package manager and the -g switch signifies installation at global scope. The following screenshot shows the installation of Truffle on Windows Server 2016. The command is the same for Linux distribution as well:

Running truffle --version shows the current version and all commands available with Truffle as shown in the following screenshot...

Development with Truffle


Using Truffle is quite simple. Truffle provides lots of scaffolding code and configuration by default. Developers need only to reconfigure some of the out-of-the-box configuration options and focus on writing their contracts. Let's take a look at the following steps:

  1. The first step is to create a project folder that will hold all projects- and Truffle-generated artifacts.
  1. Navigate to that folder and enter the init command. The init command refers to the initiation and initialization of Truffle within the folder. It will generate appropriate folders, code files, configuration, and linkage within the folder as shown in the following screenshot:

The preceding code results in a generated folder structure as shown in the following screenshot:

Let's take a look at the following folders shown in the preceding screenshot: 

  • The contracts folder contains a single file named migrations.sol. It contains a contract responsible for deploying custom contracts to an Ethereum network...

Testing with Truffle


Unit testing refers to a type of testing specific to a software unit and component in isolation. Unit tests help ensure that code in a contract is written according to functional and technical requirements. When each of the smallest components is tested under different scenarios and passes successfully, other important tests such as integration tests can be performed to test multiple components.

As mentioned before, Truffle generates a test folder and all test files should be placed in this folder. Tests can be written in JavaScript as well as Solidity. Since this is a book on Solidity, tests are focused on writing using Solidity.

Tests in Solidity are written by authoring contracts and saved as a Solidity file. The name of the contract should start with the Test prefix and each function within the contract should be prefixed with test. Please note the case sensitivity of the Test and the test prefix for both contracts as well as function names.

The following screenshot...

Summary


This chapter introduced Truffle as a utility for easing the processes of authoring, testing, and deploying Solidity contracts. Instead of typing and executing each step, Truffle provides easy commands for compiling, deploying, and testing contracts.

The following chapter will be the last chapter of this book and will focus on troubleshooting activities and tools related to Solidity. Debugging is an important aspect of troubleshooting and is an important skill for any contract developer and development. Remix debugging facilities will be discussed along with other mechanisms for debugging contracts.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Solidity Programming Essentials
Published in: Apr 2018 Publisher: Packt ISBN-13: 9781788831383
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}