AI Apps Script Unit Test Generator Code Examples 26

๐Ÿš€ Apps Script + Gemini Mastery โ€” Issue #26

AI Apps Script Unit Test Generator

https://github.com/lsvekis/Apps-Script-Code-Snippets

Write better Apps Script by automatically generating unit tests, mock objects, sample data, and edge-case scenarios using Gemini.


โญ What You Will Build

In this issue, you’ll build an AI Unit Test Generator that can:

๐Ÿงช Generate unit tests for Apps Script functions

๐ŸŽญ Create mock event objects (onEdit, onOpen, onFormSubmit)

๐Ÿ“Š Generate realistic test data

โš ๏ธ Suggest edge-case tests

๐Ÿ“ˆ Produce code coverage recommendations

๐Ÿ“„ Export a complete testing guide to Google Docs


๐Ÿง  Why This Project Matters

Testing is one of the biggest gaps in many Apps Script projects.

Developers often verify their code manually by:

  • editing spreadsheets
  • submitting forms
  • sending emails
  • clicking menus

That approach is slow, repetitive, and easy to miss important edge cases.

With AI, you can generate a solid starting point for testing in seconds.


๐Ÿงฉ Architecture

Apps Script code

โ†“

Apps Script sends code to Gemini

โ†“

Gemini generates:

  • unit tests
  • mock objects
  • sample inputs
  • expected outputs
  • edge cases

โ†“

Apps Script creates a Google Docs testing report


๐Ÿงฑ Step 1 โ€” Menu

Code.gs

function onOpen() {

SpreadsheetApp.getUi()
.createMenu("AI Tools")
.addItem(
"AI Unit Test Generator",
"showTestingSidebar"
)
.addToUi()

}

function showTestingSidebar(){

SpreadsheetApp.getUi().showSidebar(
HtmlService
.createHtmlOutputFromFile("Sidebar")
.setTitle("AI Unit Test Generator")
)

}

๐Ÿงฑ Step 2 โ€” Sidebar

Sidebar.html

<div style="font-family:Arial;padding:14px;">

<h2>AI Unit Test Generator</h2>

<textarea
id="code"
style="width:100%;height:250px;">
Paste Apps Script code here...
</textarea>

<button onclick="generate()">

Generate Tests

</button>

<pre id="output"></pre>

<script>

function generate(){

document.getElementById("output").textContent=
"Generating tests..."

google.script.run
.withSuccessHandler(res=>{

document.getElementById("output").textContent=
res

})
.generateTests(
document.getElementById("code").value
)

}

</script>

</div>

๐Ÿงฑ Step 3 โ€” Generate Tests

TestGenerator.gs

function generateTests(code){

const prompt = `
You are a Google Apps Script testing expert.

Analyze the following Apps Script code.

Return JSON.

{

"summary":"",

"unitTests":[...],

"mockObjects":[...],

"sampleData":[...],

"edgeCases":[...],

"coverage":[...],

"testCode":""

}

Code:

${code}

`

let result =
callGemini(prompt,"")

result=result
.replace(/```json/g,"")
.replace(/```/g,"")

const tests =
JSON.parse(result)

return createTestingGuide_(tests)

}

๐Ÿงฑ Step 4 โ€” Create Testing Guide

TestingWriter.gs

function createTestingGuide_(tests){

const doc=
DocumentApp.create(
"Apps Script Testing Guide"
)

const body=
doc.getBody()

body.appendParagraph(
"Testing Summary"
)

body.appendParagraph(
tests.summary
)

body.appendParagraph(
"Recommended Unit Tests"
)

tests.unitTests.forEach(item=>{

body.appendListItem(item)

})

body.appendParagraph(
"Mock Objects"
)

tests.mockObjects.forEach(item=>{

body.appendListItem(item)

})

body.appendParagraph(
"Edge Cases"
)

tests.edgeCases.forEach(item=>{

body.appendListItem(item)

})

body.appendParagraph(
"Coverage Recommendations"
)

tests.coverage.forEach(item=>{

body.appendListItem(item)

})

body.appendParagraph(
"Generated Test Code"
)

body.appendParagraph(
tests.testCode
)

return doc.getUrl()

}

๐Ÿงฑ Step 5 โ€” Gemini Helper

Reuse the helper from previous issues.

const GEMINI_API_KEY =
"YOUR_API_KEY"

const GEMINI_MODEL =
"gemini-2.5-flash"

๐Ÿงช Example

Paste:

function add(a,b){

return a+b

}

AI generates:

โœ… unit tests

assertEquals(5, add(2,3))

โœ… edge cases

null

undefined

negative values

large numbers

strings

โœ… mock objects

Spreadsheet events

Form events

Time triggers

๐Ÿ“„ Example Output

Summary

The function is simple but lacks validation.

Unit Tests

  • positive numbers
  • negative numbers
  • decimals
  • zero
  • invalid types

Mock Objects

  • mock event object
  • fake spreadsheet

Coverage

95%

Generated Test File

(complete Apps Script testing code)


๐Ÿ”ฅ Advanced Exercises

โœ… Generate Jest-compatible examples for clasp projects

โœ… Produce mock Spreadsheet services

โœ… Create regression tests

โœ… Generate performance benchmarks

โœ… Build fake Gmail and Drive services

โœ… Generate test fixtures


๐ŸŒŸ Why This Project Is Different

Instead of relying on manual testing, youโ€™ll learn how to use AI to:

  • think through edge cases
  • improve code quality
  • reduce regressions
  • create repeatable testing strategies

This project is especially useful for developers building larger Apps Script solutions where confidence in changes matters.


๐Ÿ”œ Next Issue (#27)

AI Apps Script Project Scaffolder

Describe an application such as:

“Build a CRM in Google Sheets”

or

“Create an expense tracker with email notifications”

The AI generates:

  • complete project structure
  • Apps Script files
  • HTML UI
  • menus
  • triggers
  • starter code
  • deployment checklist

This will be one of the most powerful projects in the entire Apps Script + Gemini Mastery series.