如何让Playwright先执行front目录测试再执行api目录测试?
实现Playwright中front测试先于api测试运行的方法
有两种简单直接的方式可以实现这个需求:
方法一:命令行指定测试路径顺序
直接在启动测试的命令里,先写front目录的路径,再写api目录的路径,Playwright会严格按照你指定的顺序执行测试:
npx playwright test ./front ./api
方法二:通过配置文件分组控制顺序
在你的playwright.config.ts中,利用projects配置项把front和api的测试分成两个独立项目,数组的顺序就是测试执行的顺序:
import { defineConfig, devices } from '@playwright/test'; export default defineConfig({ // 注释掉原来的全局testDir,改用projects指定不同测试目录 // testDir: './', projects: [ { name: '前端测试', testDir: './front', // 可按需配置测试环境,比如指定浏览器 use: { ...devices['Desktop Chrome'] }, }, { name: '接口测试', testDir: './api', // 接口测试可单独配置无界面模式 use: { ...devices['Desktop Chrome'], headless: true }, }, ], });
配置完成后,直接执行常规测试命令即可:
npx playwright test
Playwright会按照projects数组的顺序,先跑完front目录下的所有测试,再执行api目录的测试。
内容的提问来源于stack exchange,提问作者Stella Esparza
相关产品推荐
相关产品推荐

