如何禁用Playwright测试结果持久化以适配Azure Function
解决Azure Function中Playwright测试的只读文件系统错误
问题原因
即便已将Playwright的报告器配置为唯一的list,Playwright仍会默认启用内置的last-run报告器——它需要写入/home/site/wwwroot/test-results/.last-run.json来记录上次测试运行状态。但Azure Function的/home/site/wwwroot目录属于只读文件系统,因此触发了EROFS错误。
解决方案
方案1:完全禁用last-run报告(推荐)
在playwright.config.ts中添加lastRun: false配置项,彻底关闭这个自动启用的报告器,避免任何文件写入操作:
import { defineConfig } from '@playwright/test'; export default defineConfig({ reporter: [['list']], lastRun: false, // 禁用last-run报告器 // 其他原有配置... });
方案2:指定可写入的临时目录(可选)
如果需要保留last-run功能,可将测试结果目录指向Azure Function的临时可写目录/tmp,该目录在函数运行期间支持读写:
import { defineConfig } from '@playwright/test'; export default defineConfig({ reporter: [['list']], testResultsDir: '/tmp/test-results', // 切换到临时可写目录 // 其他原有配置... });
错误信息参考
{"success":false,"message":"Failed to run Playwright tests","error":"Command failed: node ./node_modules/@playwright/test/cli.js test\nError in reporter Error: EROFS: read-only file system, open '/home/site/wwwroot/test-results/.last-run.json'\n at async open (node:internal/fs/promises:641:25)\n at async Object.writeFile (node:internal/fs/promises:1215:14)\n at async LastRunReporter.onEnd (/home/site/wwwroot/node_modules/playwright/lib/runner/lastRun.js:71:5)\n at async wrapAsync (/home/site/wwwroot/node_modules/playwright/lib/reporters/multiplexer.js:89:12)\n at async Multiplexer.onEnd (/home/site/wwwroot/node_modules/playwright/lib/reporters/multiplexer.js:57:25)\n at async InternalReporter.onEnd (/home/site/wwwroot/node_modules/playwright/lib/reporters/internalReporter.js:77:12)\n at async finishTaskRun (/home/site/wwwroot/node_modules/playwright/lib/runner/tasks.js:94:26)\n at async runTasks (/home/site/wwwroot/node_modules/playwright/lib/runner/tasks.js:81:10)\n at async runAllTestsWithConfig (/home/site/wwwroot/node_modules/playwright/lib/runner/testRunner.js:379:18)\n at async runTests (/home/site/wwwroot/node_modules/playwright/lib/program.js:242:18) {\n errno: -30,\n code: 'EROFS',\n syscall: 'open',\n path: '/home/site/wwwroot/test-results/.last-run.json'\n}\n","output":"\nError: EROFS: read-only file system, rmdir '/home/site/wwwroot/test-results'\n\n\n","errors":"Error in reporter Error: EROFS: read-only file system, open '/home/site/wwwroot/test-results/.last-run.json'\n at async open (node:internal/fs/promises:641:25)\n at async Object.writeFile (node:internal/fs/promises:1215:14)\n at async LastRunReporter.onEnd (/home/site/wwwroot/node_modules/playwright/lib/runner/lastRun.js:71:5)\n at async wrapAsync (/home/site/wwwroot/node_modules/playwright/lib/reporters/multiplexer.js:89:12)\n at async Multiplexer.onEnd (/home/site/wwwroot/node_modules/playwright/lib/reporters/multiplexer.js:57:25)\n at async InternalReporter.onEnd (/home/site/wwwroot/node_modules/playwright/lib/reporters/internalReporter.js:77:12)\n at async finishTaskRun (/home/site/wwwroot/node_modules/playwright/lib/runner/tasks.js:94:26)\n at async runTasks (/home/site/wwwroot/node_modules/playwright/lib/runner/tasks.js:81:10)\n at async runAllTestsWithConfig (/home/site/wwwroot/node_modules/playwright/lib/runner/testRunner.js:379:18)\n at async runTests (/home/site/wwwroot/node_modules/playwright/lib/program.js:242:18) {\n errno: -30,\n code: 'EROFS',\n syscall: 'open',\n path: '/home/site/wwwroot/test-results/.last-run.json'\n}\n"}
内容的提问来源于stack exchange,提问作者Gregory Suvalian
相关产品推荐
相关产品推荐

