-1

我继承了一个大型的、过时的 repo,其中包含一组 Cypress e2e 测试,这些测试累积需要数天才能运行。我需要运行测试,然后删除失败的测试。根据我目前的经验,大约 80% 的测试失败。因此,这项任务的规模很快就会变得难以手动操作。

理想情况下,解决方案是单个 bash 脚本。想到的另一种方法是以某种方式将失败测试列表导出到 CSV(我也无法弄清楚如何做),然后以某种方式以编程方式删除该列表中的每个文件。我正在使用 VSCode,以防有插件可以提供帮助。

还有一个次要问题,简单地快速运行所有测试会导致我内存不足。如果有某种方法可以随时删除测试以便我的整个任务由单个 bash 脚本完成,那就太棒了。但如果这是不可能的,我可以手动运行测试。

现在我通过将终端输出复制到文本文件来访问失败的测试列表。这很容易以编程方式完成,但输出甚至没有以易于提取的方式列出文件名。下面的示例(请忽略一些奇怪的格式更改,因为我匿名了这篇文章的文件名):

终端输出

做这个的最好方式是什么?

4

1 回答 1

1

将失败的测试列表导出到 CSV - 当您通过Cypress 模块 API运行测试时,您将在节点脚本中运行它们,并可以访问每个测试结果并 fs 将结果写出。

这是基本概念

// e2e-run-tests.js
const cypress = require('cypress')
const fs = require('fs')

cypress
  .run({
    // the path is relative to the current working directory
    spec: './cypress/integration/**/*.spec.js',
  })
  .then((results) => {
    console.log(results)
    const tests = results.runs[0].tests
    const fails = tests
      .filter(test => test.state === 'failed')
      .map(test => test.title.join(' - '))     // concat suite and test titles
    fs.writeFileSync('failed-tests.txt', fails)
  })
  .catch((err) => {
    console.error(err)
  })

自动删除测试就像玩上膛的枪一样。

更好的是,一旦有了列表,您就可以使用cypress-select-tests防止故障再次运行

// cypress/plugins/index.js

const selectTests = require('cypress-select-tests')

const failures = require('./failed-tests.txt')

// return test names you want to run
const pickTests = (filename, foundTests, cypressConfig) => {
  // found tests will be names of the tests found in "filename" spec
  // it is a list of names, each name an Array of strings
  // ['suite 1', 'suite 2', ..., 'test name']

  return foundTests.filter(fullTestName => {
    return !failures.includes(fullTestName)
  })
}

module.exports = (on, config) => {
  on('file:preprocessor', selectTests(config, pickTests))
}
于 2022-02-04T19:56:09.970 回答