Getting Started with Jasmine Test Framework

Introduction to Jasmine framework and getting started with Behavior Driven Development (BDD) testing, along with Complete Source code and Video

Jasmine is a Javascript Testing framework which starts with behavioral specifications as a basis for writing test cases. To put it another way, you can consider user stories as a reference for write test cases. Each user story in your project can have one or more Jasmine test cases.

Majorly, Jasmine is made up of three functions

  1. describe
  2. it
  3. assertions (expect)

Like standard frameworks, it also has setup method and tear down methods

  • beforeEach() – Setup Method
  • afterEach() – Teardown Method

For the sake of simplicity of post, I would not discuss much in detail. However, you can navigate to official Jasmine guide for deep dive.

Download Jasmine

You can download latest version of Jasmine from here. Once downloaded, you can extract and save lib folder in your project.

Simple Javascript code for Jasmine Test Framework

Let’s consider below Javascript code. It simply declares a person object with few properties and method to get full name and change Age.

Its time to see how we can write a test method using Jasmine Framework.

Above code snippet shows how we are using describe, it and expect methods provided by Jasmine framework.

Let’s create a sample HTML page which will load Jasmine Framework, our Javascript code and Test method to Test our code.

Below is our files and folders structure

Jasmine Test Framework folder structure
Jasmine Test Framework folder structure

Below is the output of our example. As all test passed, we don’t see any error

Jasmine Test Framework Success Output
Jasmine Test Framework Success Output

In case, if some method fails test execution, then it would show error report like below image

Jasmine Test Framework Error Report
Jasmine Test Framework Error Report

First 25 minutes of below video is basics of Jasmine Test Framework with same example as of this post.

 

Related posts

One thought on “Getting Started with Jasmine Test Framework”

Leave a Reply