You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行npx playwright test报错Error: No tests found的解决求助

解决Playwright终端执行测试提示"No tests found"及"test.describe()调用位置不符合预期"问题

1. 修正测试文件代码结构

终端报错test.describe()调用位置不符合预期是核心问题,Playwright要求test.describe必须放在文件顶级作用域,不能嵌套在函数、条件判断或其他代码块内。

错误示例:

// 错误:test.describe被嵌套在函数中
async function initTests() {
  test.describe('登录测试', () => {
    test('账号密码登录', async ({ page }) => {
      // 测试逻辑
    });
  });
}
initTests();

正确示例:

// 正确:直接在文件顶级定义测试套件
test.describe('登录测试', () => {
  test('账号密码登录', async ({ page }) => {
    // 测试逻辑
  });
});

2. 校验playwright.config.ts配置

  • 确认testDir指向正确的测试目录:
import { defineConfig } from '@playwright/test';

export default defineConfig({
  testDir: './tests', // 确保路径与你的测试文件目录一致
  // 检查是否存在exclude规则意外排除目标文件
  // exclude: [],
});
  • 若测试文件后缀不是.spec.js/ts,调整testMatch规则兼容:
export default defineConfig({
  testMatch: ['**/*.{spec,test}.{js,ts,mjs}'], // 同时支持.spec和.test后缀的测试文件
});

3. 确认终端执行路径

执行npx playwright test时,必须确保终端当前工作目录是项目根目录(即包含playwright.config.ts和tests文件夹的目录)。可通过pwd(Mac/Linux)或cd命令查看/切换路径。

4. 匹配文件扩展名与执行命令

如果你的测试文件是.ts格式(如example.spec.ts),执行命令时要指定正确的扩展名:

npx playwright test tests/example.spec.ts

5. 清理缓存并重装依赖

缓存或依赖损坏可能导致异常,执行以下命令修复:

# 强制重装Playwright浏览器
npx playwright install --force
# 删除旧依赖后重装
rm -rf node_modules package-lock.json
npm install

内容的提问来源于stack exchange,提问作者N K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:07:35