【问题标题】:How to use variables in tests that were assigned with values at beforeAll() hook如何在测试中使用在 beforeAll() 钩子中分配了值的变量
【发布时间】:2023-04-06 01:05:02
【问题描述】:

我想在beforeAll() 钩子上分配一组具有值的变量,然后在test.each([]) 中使用它们,但我得到的只是未定义而不是实际值。

但是当我尝试在普通test() 中访问相同的变量时,事实证明,我的变量具有实际值并且不是未定义的。

有没有办法给变量赋值,然后在test.each([])中使用?

let movieId: string;
let serialId: string;

describe('Video page tests', () => {

  beforeAll(async () => {
    movieId = await videos.CreateMovie();
    serialId = await videos.CreateSerial();
  });

test.each([
    ['Deletes movie', movieId],
    ['Deletes serial', serialId],
  ])('%s', async(caseTitle, entityId) => {
    reporter.description(`${caseTitle}`);

    await videos.DeleteContent(entityId); //entityId is undefined
  })

test('Deletes movie', async() => {
    reporter.description(`Deletes movie`);

    await videos.DeleteContent(movieId); //movieId is correct
  })

【问题讨论】:

  • 你不能。 beforeAll 仍然在测试 execution 时间运行,而不是测试 discovery 时间(在评估 test.each 时)。这就是为什么 ID 在测试中是正确的,它在 beforeAll. 之后运行

标签: typescript jestjs automated-tests each playwright


【解决方案1】:

事实证明,我不能在 test.each 使用变量,在 beforeAll 钩子上分配的变量,所以我想出了一个解决我的问题的方法

let content: string[] = [];
describe('Video page tests', () => {

  beforeAll(async () => {
    content.push(await videos.CreateMovie());
    content.push(await videos.CreateSerial());
  });

test.each([
    ['Deletes movie', 0],
    ['Deletes serial', 1],
  ])('%s', async(caseTitle, entityId) => {
    reporter.description(`${caseTitle}`);

    await videos.DeleteContent(content[entityId]);
  })

【讨论】:

    猜你喜欢
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多