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.