๐ 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.