How to test Google Apps Script using Qunit

26 / Apr / 2016 by Heena Dhamija 0 comments

In my previous blog, we learnt how to use Google Apps Script in spreadsheets. Now we will take a look at how we can test the code which we have written in apps script. For this purpose, we’ll use a tool called “Qunit” which helps us in writing and testing our apps script test cases.

QUnit is a powerful, easy-to-use, JavaScript unit testing framework. It’s used by the jQuery project to test its code and plugins but is capable of testing any generic JavaScript code.It is made available as a script library for Google Apps Script.

Key Points QUnit for Google Apps Script :

  1. Tests are run on Google’s servers, not in your browser. The browser only displays test results.
  2. Configuration is required by calling QUnit.config(myConfig), where myConfig is an object with your configuration settings.
  3. The Google Apps Script service running the tests needs to implement a doGet(e) function that.
  • Calls QUnit.urlParams(e.parameter), which ensures that QUnit-specific HTTP parameters are handled when you interact with the QUnit GUI;
  • Optionally, configures QUnit with the Qunit.config() function;
  • Loads tests with QUnit.load(myTests), where myTests is a function containing your tests;
  • Returns the result of Qunit.getHtml();
  • Some helper functions can be imported from QUnit to the test suite by calling QUnit.helpers(this).

Steps for adding ‘Qunit‘ in project :

  1. Go to script editor.
  2. Select “Resources” > “Libraries…” in the Google Apps Script editor.
  3. Enter this project key (MxL38OxqIK-B73jyDTvCe-OBao7QLBR4j) in the “Find a Library” field, and choose “Select”.
  4. Select version number 4, and choose QUnit as the identifier. (Do not turn on Development Mode)
  5. Press Save.

Add Qunit in Google Apps Script

Here is the sample code:

function calculateAmountAndQty(quantity,amount){
if(typeof (quantity) == ‘undefined’ || quantity == ‘null’ || isNaN(quantity)) {

if(typeof (amount) == ‘undefined’ || amount == ‘null’ || isNaN(amount)) {

return (quantity*amount);

We’ll be writing test cases for the above code. Open up the scripts editor and paste the code given below and save the script afterwards.

QUnit.helpers( this );
function testFunctions() {

function doGet( e ) {
QUnit.urlParams( e.parameter );
title: "QUnit for Google Apps Script – Test suite" // Sets the title of the test page.
QUnit.load( testFunctions );

return QUnit.getHtml();

function testingCalculateAmountAndQty(){
QUnit.test( "calculateAmountAndQty testing", function() {
equal( calculateAmountAndQty(10,2000), 20000, "Test for quantity : 10 and amount : 2000 sp output is 20000" );
equal( calculateAmountAndQty("printer",2000), 0, "Test for quantity : printer and amount : 2000 so output is 0" );
equal( calculateAmountAndQty(10,"mouse"), 0, "Test for quantity : 10 and amount : mouse so output is 0" );
equal( calculateAmountAndQty(10,null), 0, "Test for quantity : 10 and amount : null so output is 0" );
equal( calculateAmountAndQty(null,2000), 0, "Test for quantity : null and amount : 2000 so output is 0" );
equal( calculateAmountAndQty(undefined,2000), 0, "Test for quantity : undefined and amount : 2000 so output is 0" );
equal( calculateAmountAndQty(10,undefined), 0, "Test for quantity : 10 and amount : undefined so output is 0" );

Steps to Run Qunit Test Case :

  1. Click on Publish> Deploy as web app.
    Deploy as web app
  2. Click on Deploy.
  3. Click on latest code. It redirects to Qunit page where all test case report is displayed .

Output :

Qunit Test Cases Executed

Note: Qunit configuration is mandatory.

For more reference :


Leave a Reply

Your email address will not be published. Required fields are marked *