一、安装mochawesome

    1. npm install --save-dev mochawesome mochawesome-merge mochawesome-report-generator

    二、配置cypress.json

    1. // cypress.json
    2. {
    3. ...
    4. "reporter": "mochawesome",
    5. "reporterOptions": {
    6. "reportDir": "tests/e2e/results/reports",
    7. "overwrite": false,
    8. "html": false,
    9. "json": true,
    10. "toConsole": true
    11. },
    12. ...
    13. }

    三、执行脚本
    创建 tests/e2e/report/index.js

    1. // eslint-disable-next-line @typescript-eslint/no-var-requires
    2. const fse = require('fs-extra');
    3. // eslint-disable-next-line @typescript-eslint/no-var-requires
    4. const { merge } = require('mochawesome-merge');
    5. // eslint-disable-next-line @typescript-eslint/no-var-requires
    6. const generator = require('mochawesome-report-generator');
    7. // const cypress = require('cypress')
    8. async function runTests () {
    9. await fse.remove('mochawesome-report');
    10. // await cypress.run({ config: { baseUrl: 'http://localhost:8080/' } })
    11. const options = {
    12. files: [
    13. // you can specify more files or globs if necessary:
    14. './tests/e2e/results/reports/*.json'
    15. ],
    16. reportDir: './tests/e2e/results/'
    17. };
    18. const jsonReport = await merge(options);
    19. // const totalFailed = jsonReport.stats.failures
    20. await generator.create(jsonReport, options);
    21. }
    22. runTests();

    四、 添加执行命令
    package.json 添加执行命令

    1. // package.json
    2. "scripts": {
    3. "buildReport": "node tests/e2e/report/index.js",
    4. // 注意:需要确保项目正在运行,并运行在https://localhost:8080/mp/
    5. "test.headless:e2e": "vue-cli-service test:e2e --headless --url https://localhost:8080/mp/",
    6. },

    五、执行命令

    1. # 运行项目
    2. npm run serve
    3. # 执行测试命令
    4. 测试项目:npm run test.headless:e2e // 注意运行项目时的端口变化,需要同步到package.json=》scripts=》“test.headless:e2e”命令中里面去,如项目运行在8080端口的话,需更改为:"test.headless:e2e": "vue-cli-service test:e2e --headless --url https://localhost:8080/mp/",
    5. # 执行报告构建命令
    6. 生成报告:npm run buildReport

    六、完成
    报告文件就生成在 tests/e2e/results/mochawesome.html

    参考文件:https://www.jianshu.com/p/459612488233