how to unit test google apps scripts?
Asked Answered
T

5

55

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:

How to get Cucumber/Capybara/Mechanize to work against external non-rails site 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?

Tiliaceous answered 28/3, 2013 at 12:53 Comment(0)
S
18

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.

Systematic answered 16/4, 2013 at 10:23 Comment(6)
Great instructions here, under 'Example use': github.com/simula-innovation/qunit/tree/gas/gasCoronet
Note that if you are deploying a webapp via google apps script, you will have to switch the doGet function names back and forth.Coronet
TypeError: Cannot read property "all" from undefined. (line 263, file "qunit.js", project "QUnit")Educate
I've found QUnit to be the best choice among those listed here. Here is another tutorial: tothenew.com/blog/how-to-test-google-apps-script-using-qunit I wanted jasmine/rajah to work but there are some problems with a dependency in gas-console which no longer works in a GAS environment.Bosomy
This hasn't been updated for the v8 engine, so you'll need to use QUnitGS2 after 2020.Sieber
This answer looks to be obsolete.Crutchfield
S
13

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

Smithsonite answered 1/12, 2015 at 18:15 Comment(1)
Yes, a super simple tool that will get you going. I favor QUnit for GAS but had a technical issue with it. In my mind, GAST is a good fallback.Bosomy
S
11

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.

Skintight answered 5/5, 2016 at 17:30 Comment(5)
clasp doesn't let you test.Apoloniaapolune
If you use clasp, you can test with Jasmine/Mocha locally because your files are written in JS and then translated to GAS with clasp push. The OAuth2 Apps Script library tests with Mocha: github.com/gsuitedevs/apps-script-oauth2Ailurophobe
@Brian, what is the advantage of separating tests from the platform?Chesterchesterfield
@contributorpw I've had mixed success with testing in the online editor. The V8 runtime also REALLY slowed down logging messages, which makes testing hard to do efficiently.Ailurophobe
@Apoloniaapolune clasp now supports the run command to run functions remotely.Pickering
D
2

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

Durrett answered 2/8, 2020 at 8:22 Comment(0)
T
1

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.

Trella answered 18/4, 2013 at 13:24 Comment(2)
I have moved my tests over from gas-unit to QUnit for GAS this evening which was relatively painless and I'm already getting benefit from the additional features of QUnit. Thanks Adam and the other contributors. Also found this jasmine runner link which seems to work but is possibly more experimental but one to watch.Trella
@Systematic See the issue I've raised regarding being unable to use the debugger hereTrella

© 2022 - 2024 — McMap. All rights reserved.