What are Assertions?

Course Home | Code Example(s)

How do we know if our test was successful? This is where assertions come in.

Assertions are important. Very important when it comes to automated checking. This is our attempt at replacing ourselves. When we run the scenario we are automating, how are we deciding if what the application is presenting us is acceptable? We need to work out what this is and instruct our code to do the assertions that meet that acceptable criterion. Fortunately, they give us many options for doing this.

Mocha itself doesn’t come with assertion libraries, but there are many assertion libraries that you can choose from. For now we are going to use Chai, which is a very popular assertion library for Node. To install it, simply run:

npm install chai

With Chai installed we can then add assertions into our code.

// First of all we need to import the chai library to use in our
// tests. Chai comes in three different flavours should, expect and
// assert, so we are going to use the expect library.
var expect = require('chai').expect;

describe('Assertion demonstration', function() {

  it('should tells us whether our test has passed or failed', function() {
    var result = 10 / 5;

    // We pass our object or value that we want to assert into the
    // expect function and then call a chain of functions that allow
    // us to write our assertions out in plain language so we know
    // exactly what we are asserting.

    // We can assert to see if our value exists

    // We can assert to see if our value is of a specific data type

    // We can assert that the value meets certain expectations


Important note to add here, designing good assertions is a skill, a skill that doesn’t require coding knowledge. So if you are struggling to design acceptable assertions, involve the team and seek help. As always stated, these assertions are very important.

Next Lesson ➔


Chai - Expect library

Mark Winteringham's photo

Mark Winteringham


Software Tester, speaker and trainer at Hindsight Software. Founder of the Software Testing Clinic. Music fan.