{"id":33364,"date":"2016-04-26T15:57:34","date_gmt":"2016-04-26T10:27:34","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=33364"},"modified":"2016-04-27T10:01:06","modified_gmt":"2016-04-27T04:31:06","slug":"how-to-test-google-apps-script-using-qunit","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-test-google-apps-script-using-qunit\/","title":{"rendered":"How to test Google Apps Script using Qunit"},"content":{"rendered":"<p>In my <a href=\"http:\/\/www.tothenew.com\/blog\/introduction-to-google-apps-script-with-google-spreadsheet\/\" target=\"_blank\">previous blog<\/a>, we learnt how to use Google Apps Script in spreadsheets. Now we will take a look at\u00a0how we can test the code which we have\u00a0written in apps script.\u00a0For this purpose, we&#8217;ll use a tool called \u201c<em><strong>Qunit<\/strong><\/em>\u201d which helps us in writing and testing our apps script test cases.<\/p>\n<p><em><strong>QUnit<\/strong><\/em> is a powerful, easy-to-use, JavaScript unit testing framework. It&#8217;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.<\/p>\n<p><strong>Key Points QUnit for Google Apps Script :<\/strong><\/p>\n<ol>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important;\">Tests are run on Google&#8217;s servers, not in your browser. The browser only displays test results.<\/li>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important;\">Configuration is required by calling <code>QUnit.config(myConfig)<\/code>, where <code>myConfig<\/code> is an object with your configuration settings.<\/li>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important;\">The Google Apps Script service running the tests needs to implement a <code>doGet(e)<\/code> function that.<\/li>\n<\/ol>\n<ul>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important; margin-left: 50px;\">Calls <code>QUnit.urlParams(e.parameter)<\/code>, which ensures that QUnit-specific HTTP parameters are handled when you interact with the QUnit GUI;<\/li>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important; margin-left: 50px;\">Optionally, configures QUnit with the <code>Qunit.config()<\/code> function;<\/li>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important; margin-left: 50px;\">Loads tests with <code>QUnit.load(myTests)<\/code>, where <code>myTests<\/code> is a function containing your tests;<\/li>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important; margin-left: 50px;\">Returns the result of <code>Qunit.getHtml();<\/code><\/li>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important; margin-left: 50px;\">Some helper functions can be imported from QUnit to the test suite by calling <code>QUnit.helpers(this).<\/code><\/li>\n<\/ul>\n<p><strong>Steps for adding &#8216;<em>Qunit<\/em>&#8216; in project :<\/strong><\/p>\n<ol>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important;\">Go to script editor.<\/li>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important;\">Select &#8220;Resources&#8221; &gt; &#8220;Libraries&#8230;&#8221; in the Google Apps Script editor.<\/li>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important;\">Enter this project key (MxL38OxqIK-B73jyDTvCe-OBao7QLBR4j) in the &#8220;Find a Library&#8221; field, and choose &#8220;Select&#8221;.<\/li>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important;\">Select version number <code>4<\/code>, and choose QUnit as the identifier. (Do not turn on Development Mode)<\/li>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important;\">Press Save.<\/li>\n<\/ol>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-33517\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/Add-Qunit-in-Google-Apps-Script.png\" alt=\"Add Qunit in Google Apps Script\" width=\"506\" height=\"288\" \/><\/p>\n<p><strong>Here is the sample code:<\/strong><\/p>\n<p>[js]<br \/>\nfunction calculateAmountAndQty(quantity,amount){<br \/>\n if(typeof (quantity) == &#8216;undefined&#8217; || quantity == &#8216;null&#8217; || isNaN(quantity)) {<br \/>\n       quantity=0;<br \/>\n }<\/p>\n<p> if(typeof (amount) == &#8216;undefined&#8217; || amount == &#8216;null&#8217; || isNaN(amount)) {<br \/>\n       amount=0;<br \/>\n }<\/p>\n<p> return (quantity*amount);<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>We&#8217;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.<\/p>\n<p>[js]<br \/>\n QUnit.helpers( this );<br \/>\n function testFunctions() {<br \/>\n    testingCalculateAmountAndQty();<br \/>\n }<\/p>\n<p> function doGet( e ) {<br \/>\n      QUnit.urlParams( e.parameter );<br \/>\n      QUnit.config({<br \/>\n           title: &quot;QUnit for Google Apps Script &#8211; Test suite&quot; \/\/ Sets the title of the test page.<br \/>\n      });<br \/>\n      QUnit.load( testFunctions );<\/p>\n<p>      return QUnit.getHtml();<br \/>\n };<\/p>\n<p> function testingCalculateAmountAndQty(){<br \/>\n    QUnit.test( &quot;calculateAmountAndQty testing&quot;, function() {<br \/>\n       expect(7);<br \/>\n       equal( calculateAmountAndQty(10,2000), 20000, &quot;Test for quantity : 10 and amount : 2000 sp output is 20000&quot; );<br \/>\n       equal( calculateAmountAndQty(&quot;printer&quot;,2000), 0, &quot;Test for quantity : printer and amount : 2000 so output is 0&quot; );<br \/>\n       equal( calculateAmountAndQty(10,&quot;mouse&quot;), 0, &quot;Test for quantity : 10 and amount : mouse so output is 0&quot; );<br \/>\n       equal( calculateAmountAndQty(10,null), 0, &quot;Test for quantity : 10 and amount : null so output is 0&quot; );<br \/>\n       equal( calculateAmountAndQty(null,2000), 0, &quot;Test for quantity : null and amount : 2000 so output is 0&quot; );<br \/>\n       equal( calculateAmountAndQty(undefined,2000), 0, &quot;Test for quantity : undefined and amount : 2000 so output is 0&quot; );<br \/>\n       equal( calculateAmountAndQty(10,undefined), 0, &quot;Test for quantity : 10 and amount : undefined so output is 0&quot; );<br \/>\n    });<br \/>\n }<br \/>\n[\/js]<\/p>\n<p><strong>Steps to Run Qunit Test Case :<\/strong><\/p>\n<ol>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important;\">Click on Publish&gt; Deploy as web app.<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-33520\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/Deploy-as-web-app.png\" alt=\"Deploy as web app\" width=\"430\" height=\"260\" \/><\/li>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important;\">Click on Deploy.<\/li>\n<li style=\"font-family: 'open_sansregular'; font-size: 14px !important;\">Click on latest code. It redirects to Qunit page where all test case report is displayed .<\/li>\n<\/ol>\n<p><strong>Output<\/strong> :<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-33521\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/Qunit-Test-Cases-Executed.png\" alt=\"Qunit Test Cases Executed\" width=\"554\" height=\"382\" \/><\/p>\n<p><strong>Note: <\/strong>Qunit configuration is mandatory.<\/p>\n<p>For more reference :<br \/>\n1. https:\/\/qunitjs.com<br \/>\n2. https:\/\/github.com\/simula-innovation\/qunit\/tree\/gas\/gas<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous blog, we learnt how to use Google Apps Script in spreadsheets. Now we will take a look at\u00a0how we can test the code which we have\u00a0written in apps script.\u00a0For this purpose, we&#8217;ll use a tool called \u201cQunit\u201d which helps us in writing and testing our apps script test cases. QUnit is a [&hellip;]<\/p>\n","protected":false},"author":76,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":50},"categories":[1],"tags":[3173,3179],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/33364"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=33364"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/33364\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=33364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=33364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=33364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}