How to test Google Apps Script using Qunit
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 :
- Tests are run on Google’s servers, not in your browser. The browser only displays test results.
- Configuration is required by calling
QUnit.config(myConfig)
, wheremyConfig
is an object with your configuration settings. - 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)
, wheremyTests
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 :
- Go to script editor.
- Select “Resources” > “Libraries…” in the Google Apps Script editor.
- Enter this project key (MxL38OxqIK-B73jyDTvCe-OBao7QLBR4j) in the “Find a Library” field, and choose “Select”.
- Select version number
4
, and choose QUnit as the identifier. (Do not turn on Development Mode) - Press Save.
Here is the sample code:
[js]
function calculateAmountAndQty(quantity,amount){
if(typeof (quantity) == ‘undefined’ || quantity == ‘null’ || isNaN(quantity)) {
quantity=0;
}
if(typeof (amount) == ‘undefined’ || amount == ‘null’ || isNaN(amount)) {
amount=0;
}
return (quantity*amount);
}
[/js]
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.
[js]
QUnit.helpers( this );
function testFunctions() {
testingCalculateAmountAndQty();
}
function doGet( e ) {
QUnit.urlParams( e.parameter );
QUnit.config({
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() {
expect(7);
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" );
});
}
[/js]
Steps to Run Qunit Test Case :
- Click on Publish> Deploy as web app.
- Click on Deploy.
- Click on latest code. It redirects to Qunit page where all test case report is displayed .
Output :
Note: Qunit configuration is mandatory.
For more reference :
1. https://qunitjs.com
2. https://github.com/simula-innovation/qunit/tree/gas/gas