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

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

问题描述

我正在尝试为 google 应用程序脚本设置单元测试,我发现了两个项目:

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 :-)

我刚刚尝试了不带连字符的gasunit,它似乎希望脚本嵌入到电子表格中,我有点不清楚该怎么做......我想要测试的脚本是基于网络的脚本而不是电子表格

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

我更幸运地测试了带连字符的gas-unit,它成功地向我发送了测试的电子邮件输出并在我的谷歌网站中生成了一个结果页面:

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

所以我现在打算使用gas-unit,但我真的很想看到谷歌合并的一些官方测试框架.特别是我想找到某种方法让这些脚本以某种频率运行以向我发送结果.我也很想开始一些 BDD;查看我的其他帖子:

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:

如何获得Cucumber/Capybara/Mechanize 对抗外部非轨道站点如何使用水豚has_text

来吧谷歌,你的所有浴室里都有著名的测试摇滚,调试很烂"?如何更好地支持 Google Apps 脚本?

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

推荐答案

你可以试试 用于 Google Apps 脚本的 QUnit.这是一个补丁,用于QUnit 变成了带有 API 文档.

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.

您所需要的只是一个脚本项目,该项目导入一个 QUnit 库(例如,项目密钥为 MxL38OxqIK-B73jyDTvCe-OBao7QLBR4j 的那个)并具有一个 doGet 函数,该函数使用 URL 参数和可选的配置 QUnit也使用您自己的设置,加载一个运行您的测试的函数,最后返回 QUnit.getHtml().下面是一个例子:

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);
  });
}

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

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天全站免登陆