how to unit test google apps scripts?

Unit TestingGoogle Apps-Script

Unit Testing Problem Overview


I'm trying to get set up with unit tests for google app scripts, and I found two projects:

https://code.google.com/p/gas-unit/ https://code.google.com/p/gasunit/

So I'm confused which to use :-)

I just had a go with the unhyphenated gasunit, which seems to expect that the script is embedded in a spreadsheet, which I am a little unclear on how to do ... and the scripts I want to test are web based scripts rather than spreadsheet ones

I had more luck testing the hyphenated gas-unit, which managed to send me both an email output of the test and generate a results page in my google site:

https://sites.google.com/site/testappscript2/TestResults

so I'm going to with gas-unit for the moment, but I'd really like to see some official testing framework incorporated by Google. In particular I'd like to find some way to get these scripts to be run with some frequency to send me the results. Also I'd love to get some BDD going; see my other posts:

https://stackoverflow.com/questions/15665106/how-to-get-cucumber-capybara-mechanize-to-work-against-external-non-rails-site https://stackoverflow.com/questions/15681615/how-to-use-capybara-has-text

Come on Google, you famously have "Testing Rocks, Debugging Sucks" in all your bathrooms? How about better testing support for Google Apps Scripts?

Unit Testing Solutions


Solution 1 - Unit Testing

You can try out QUnit for Google Apps Script. It is a patch for QUnit turned into a Google Apps Script library with API docs.

All you need is a script project that imports a QUnit library (for example the one with the project key MxL38OxqIK-B73jyDTvCe-OBao7QLBR4j) and has a doGet function that configures QUnit using URL parameters and optionally also with your own settings, loads a function that runs your tests, and finally returns QUnit.getHtml(). Here is an example:

function doGet( e ) {
  QUnit.urlParams( e.parameter );
  QUnit.config({ title: "Unit tests for my project" });
  QUnit.load( myTests );
  return QUnit.getHtml();
};

// Imports the following functions:
// ok, equal, notEqual, deepEqual, notDeepEqual, strictEqual,
// notStrictEqual, throws, module, test, asyncTest, expect
QUnit.helpers(this);

function myTests() {
  module("dummy module");

  test("dummy test", 1, function() {
    ok(true);
  });
}

Then authorize the script, save a version of it, publish the script project ("Deploy as web app") and go to the test URL ("latest code") with your browser. Your tests will be run and results will be displayed via HtmlService. You can single-click on them to see their assertions, but as of writing this, you will probably not be able to do so in Firefox 20 and 21 due to Caja issue 1688.

Solution 2 - Unit Testing

I just wrote another testing framework named GasT for my google spreadsheet add-on development & testing.

GasT is a TAP-compliant testing framework for Google Apps Script. It provides a simple way to verify that the GAS programs you write behave as expected. https://github.com/huan/gast

My goal is to get a simple tap tool like tape(for javascript) or bats(for bash). the test suite format is quite clear:

var gastLibUrl = 'https://raw.githubusercontent.com/zixia/gast/master/src/gas-tap-lib.js'
eval(UrlFetchApp.fetch(gastLibUrl).getContentText())

var test = GasTap.setPrintDriver('Logger') 

function gast() {

  test('do calculation right', function (t) {    
    var i = 3 + 4
    t.equal(i, 7, 'I can calc 3 + 4 = 7')
  })

  test('Spreadsheet exist', function (t) {
    var ss = SpreadsheetApp.openById('1TBJpvlW3WWney4rk1yW5N9bAP8dOMkWxI97dOtco-fc')
    t.ok(ss, 'I can open spreadsheet')
  })

  test.finish()
}

Hope someone will like it. :)

there's a online version, you can go to have a look on it here: https://docs.google.com/spreadsheets/d/19M2DY3hunU6tDQFX5buJmZ_f3E8VFmlqAtodyC-J8Ag/edit#gid=0&vpid=A1

GasT - Google Apps Script Testing-framework, Test Anything Protocol compatible

Solution 3 - Unit Testing

The clasp tool provides the ability to develop and deploy Apps Script projects locally from the command-line.

From the clasp repo:

  1. npm install -g @google/clasp
  2. enable Apps Script API: https://script.google.com/home/usersettings
  3. Develop locally and use the clasp tool to deploy.

Edit the node-google-apps-script project has been deprecated in favor of clasp

There is the node-google-apps-script package to allow using standard JavaScript packages and automated testing tooling.

  1. npm install -g node-google-apps-script.
  2. Go through the authorization steps to provide client secrets to allow uploading and importing Apps Script projects.
  3. Use gulp or grunt or whatever you use for test running normal JavaScript projects.

There is an official Google sample available that uses this workflow.

See Google Apps Developer Blog post announcement for more details.

Once the files are downloaded, convert them to TypeScript by renaming them to end with .ts instead of .js. Once they are TypeScript, ava can be used to test them. Converting them to TypeScript also lets one use ES6 language features.

Solution 4 - Unit Testing

I created gas-unit (https://code.google.com/p/gas-unit/) and have spent a bit of time over the last few days tidying up the examples and adding a HTML test runner.

I have been using it myself for some spreadsheet manipulation I've been doing with reasonable success. I've also been using Jasmine for non-GAS client side js work and have really enjoyed that. I miss the ability in gas-unit to easily create spys and I favour the BDD style of specification writing.

gas-unit has been a great learning exercise for me and it does work although there may be undiscovered issues with scope and closure - this is my first significant js exercise outside of DOM manipulation.

I think the future for testing in GAS has to be with a port of QUnit (as Adam suggests) or Jasmine. I had a quick look at what it would take to port Jasmine but as yet have not been able to find the time to tackle it.

Solution 5 - Unit Testing

Check out QUnitGS2 - a new Apps Script library using the latest version of QUnit (v2.10.1).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSam JosephView Question on Stackoverflow
Solution 1 - Unit TestingadamView Answer on Stackoverflow
Solution 2 - Unit TestingHuanView Answer on Stackoverflow
Solution 3 - Unit TestingTim SwastView Answer on Stackoverflow
Solution 4 - Unit TestingNeil MclaughlinView Answer on Stackoverflow
Solution 5 - Unit TestingAndrew RobertsView Answer on Stackoverflow