异步动态生成Playwright测试,显示No test found

问题描述 投票:0回答:2

Playwright 文档有一个像这样的参数化示例。它有效:

const people = ['Alice', 'Bob'];
for (const name of people) {
  test(`testing with ${name}`, async () => {
    // ...
  });
  // You can also do it with test.describe() or with multiple tests as long the test name is unique.
}

但是,我想在每次测试之前获取数据数组,并生成测试循环

//data like data[] = [{name:'Alice',age:'20'},..]
let data: any[] = []; 

test.beforeEach(async ({ request }) => {
    
    data = await getData(request);
});

for (let i = 0; i < data.length; i++) {
  test(`testing with ${name}`, async (page) => {
    // ...
  });

}

在 Visual Studio 代码中,它显示错误:未找到测试

但是当我分配一个精确的值时它就起作用了

let data: any[] =[{name:'Alice',age:'20'}];

我测试过类似的情况 https://github.com/microsoft/playwright/issues/9916 如何从内置 url 的动态数组中正确运行单独的 Playwright 测试?

typescript e2e-testing playwright playwright-typescript parameterized-tests
2个回答
0
投票

我有一个糟糕的解决方案,那就是定义数组的长度

let data: any[] = new Array(10);

然后这个测试就可以通过VScode来确定了

for (let i = 0; i < data.length; i++) {
  test(`testing with ${name}`, async (page) => {
    if(data[i] !== undefined){
         //test
     }
  // ...
 });

}


0
投票

那是因为您正在使用 beforeEach 钩子,并且很可能

getData
方法将未定义的值分配给
data
data
长度为零。
我建议您调试 beforeEach 钩子中分配给
data
的内容。

更重要的事情是:beforEach 钩子在每个测试之前运行一次。就你而言,

  1. 您的测试从空数组开始
    data
    ;
  2. 你的钩子只能工作一次;
  3. 但是由于
    data
    为空,测试运行停止并且进程退出并出现错误:
    No Test found


解决方案:使用

test.beforeAll

© www.soinside.com 2019 - 2024. All rights reserved.