junit 当在test.beforEach或全局设置代码中测试失败时,testInfo.annotations.push不工作

xkrw2x1b  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(64)

我们使用Playwright框架和TypeScript实现了很多测试,我们为每个测试添加了testInfo.annotations.push({ type: "test_key", description: "ABC-002" })代码,这样JUnit xml测试报告中的每个测试(<testcase/>)都会有一个额外的字段<property name="test_key" value="ABC-002">,我们可以将xml报告导入到Jira,每个测试结果都可以根据该字段的值链接到Jira ticket。
但是我发现,如果测试在非测试代码块失败,如global-setuptest.beforeEachtest.beforAll,测试将被跳过或失败,并且<property name="test_key" value="ABC-002">字段不会添加到XML报告中,当我们尝试导入并链接测试结果到Jira测试票时,这并不好。
有没有人可以解决这个问题,使JUnit的xml测试报告总是有代码testInfo.annotations.push({ type: "test_key", description: "ABC-002" });添加的字段?谢谢!
这是测试代码示例

test("has title", async ({ page }, testInfo) => {
  testInfo.annotations.push({ type: "test_key", description: "ABC-002" });
  await page.goto("https://playwright.dev/");

  // Expect a title "to contain" a substring.
  await expect(page).toHaveTitle(/Playwright/);
});

字符串
它是预期的JUnit XML测试报告文件

<testsuites id="" name="" tests="2" failures="0" skipped="0" errors="0" time="6.236571">
  <testsuite name="example.spec.ts" timestamp="2023-12-13T02:48:18.156Z" hostname="chromium" tests="2" failures="0" skipped="0" time="7.121" errors="0">
    <testcase name="has title" classname="example.spec.ts" time="2.696">
      <properties>
        <property name="test_key" value="ABC-001">
</property>
      </properties>
    </testcase>
    <testcase name="get started link" classname="example.spec.ts" time="4.425">
      <properties>
        <property name="test_key" value="ABC-002">
</property>
      </properties>
    </testcase>
  </testsuite>
</testsuites>

osh3o9ms

osh3o9ms1#

有几种方法可以实现这一点:

  • 使用夹具
  • 使用beforeAll/beforeEach钩子

我的意思是:选项1:fixtures https://playwright.dev/docs/test-fixtures
您可以创建https://playwright.dev/docs/test-fixtures#overriding-fixtures并为每个测试提供Map。
您还可以提取测试名称(标题)和/或套件名称,并创建外部Map来放置注解。示例:

page: async ({page}, use, testInfo) => {

    console.info(`Before fixture. Test: ${testInfo.title}`);

    await use(page);

    if (testInfo.title === 'should allow me to add todo items' && testInfo.status === 'failed') {
        testInfo.annotations.push({ type: 'test_key', description: 'failed'})
    }

    console.info(`After fixture. Test: ${testInfo.title}`);
},

字符串
这将导致为每个名称的测试添加注解。
缺点:它将需要每个测试名称和键的外部Map。
第二种方法是使用前钩子https://playwright.dev/docs/test-annotations#use-fixme-in-beforeeach-hook
这也提供了testInfo,你可以把一般注解所有以下测试.示例:

test.beforeEach(async ({}, testInfo) => {
    testInfo.annotations.push({type: 'test_key', description: 'something'});
});


在此之后,每个xml将包含注解。希望它有帮助。

相关问题