如何单元测试谷歌应用程序脚本? [英] how to unit test google apps scripts?

查看:83
本文介绍了如何单元测试谷歌应用程序脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用谷歌应用程序脚本的单元测试进行设置,并且发现了两个项目: https://code.google.com/p/gas-unit/
https://code.google.com/p/gasunit/



所以我很困惑要使用哪一个: - )



我刚接触过未加脚本的gasunit,似乎期望脚本嵌入到电子表格中,我有点不清楚如何做...我想测试的脚本是基于web的脚本而不是电子表格的脚本



我有更多运气测试连字符gas-unit,它能够向我发送测试的电子邮件输出并在我的谷歌网站上生成一个结果页面: sites.google.com/site/testappscript2/TestResultsrel =noreferrer> https://sites.google.com/site/testappscript2/TestResults



<所以我走了暂时与燃气部门合作,但我真的很想看到谷歌合并的官方测试框架。特别是,我想找到一些方法让这些脚本以某种频率运行,并将结果发送给我。我也很想得到一些BDD,看到我的其他帖子:




如何使用capybara has_text



来吧Google,测试岩石,调试吮吸在你所有的浴室?如何更好地测试对Google Apps脚本的支持?

解决方案

您可以尝试 Google Apps脚本QUnit 。这是一个补丁,用于 QUnit 转换为Google Apps脚本库,其中 API文档



您需要的是一个脚本项目,它导入一个QUnit库(例如带有项目关键字 MxL38OxqIK-B73jyDTvCe-OBao7QLBR4j ),并且有一个doGet函数,可以使用URL参数配置QUnit,也可以选择使用自己的设置,加载运行测试的函数,最后返回QUnit.getHtml()。这里是一个例子:
$ b

function doGet(e){
QUnit.urlParams( e.parameter);
QUnit.config({title:为我的项目进行单元测试});
QUnit.load(myTests);
return QUnit.getHtml();
};

//导入以下函数:
// ok,equal,notEqual,deepEqual,notDeepEqual,strictEqual,
// notStrictEqual,throws,module,test,asyncTest,expect
QUnit.helpers(this);

函数myTests(){
module(dummy module);

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

然后授权脚本,保存它的一个版本,发布脚本项目部署为Web应用程序)并使用浏览器转到测试URL(最新的代码)。您的测试将运行并且结果将通过HtmlService显示。你可以单击它们来查看它们的断言,但是在写这篇文章的时候,由于 Caja issue 1688


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?

解决方案

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.

这篇关于如何单元测试谷歌应用程序脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆