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