当前位置: 首页 > 知识库问答 >
问题:

无法在mocha options/mocharc/etc中指定自定义报告程序

符俊材
2023-03-14
require:
  - '@babel/polyfill'
  - '@babel/register'
reporter: './mocha-reporter'
spec: '_src/js/tests/unit/**/*.spec.js'
{
  "name": "box",
  "version": "1.0.0",
  "description": "boom!",
  "main": "index.js",
  "scripts": {
    "mocha": "mocha",
    "mocha-custom": "mocha -O outputDir=_src/js/tests/reports,testDir=_src/js/tests/unit --reporter mocha-reporter",
    "mochawesonme": "mocha --reporter mochawesome --reporter-options reportDir=_src/js/tests/reports,reportFilename=PCMS_unit_test_results",
    "check-types": "tsc",
    "clean-selenium": "webdriver-manager clean",
    "update-selenium": "webdriver-manager update --standalone --versions.standalone=3.8.0",
    "start-selenium": "webdriver-manager start --versions.standalone=3.8.0",
    "integration-tests": "protractor protractor.conf.js"
  },
  "devDependencies": {
    "@babel/cli": "~7.4.3",
    "@babel/core": "~7.4.3",
    "@babel/plugin-proposal-class-properties": "7.4.0",
    "@babel/plugin-proposal-object-rest-spread": "~7.4.3",
    "@babel/plugin-transform-destructuring": "~7.4.3",
    "@babel/polyfill": "~7.4.3",
    "@babel/preset-env": "~7.4.3",
    "@babel/preset-typescript": "~7.3.3",
    "@babel/register": "~7.4.0",
    "@fortawesome/fontawesome-free": "5.8.1",
    "@types/bluebird": "3.5.26",
    "@types/jquery": "3.3.29",
    "@types/knockout": "~3.4.65",
    "@typescript-eslint/eslint-plugin": "~1.7.0",
    "@typescript-eslint/parser": "~1.7.0",
    "appcache-webpack-plugin": "~1.4.0",
    "autoprefixer": "~9.5.1",
    "babel-loader": "~8.0.5",
    "chai": "~4.2.0",
    "chai-as-promised": "7.1.1",
    "copy-webpack-plugin": "~5.0.3",
    "css-loader": "~2.1.1",
    "eslint": "~5.16.0",
    "eslint-config-airbnb-base": "~13.1.0",
    "eslint-config-airbnb-typescript": "~3.0.0",
    "eslint-plugin-import": "~2.17.2",
    "file-loader": "~3.0.1",
    "html-loader": "~0.5.5",
    "html-webpack-plugin": "3.2.0",
    "js-yaml": "~3.13.1",
    "json-loader": "~0.5.7",
    "jszip": "~3.2.1",
    "karma": "~4.1.0",
    "karma-chai": "~0.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-firefox-launcher": "~1.1.0",
    "karma-mocha": "~1.3.0",
    "karma-sinon": "~1.0.5",
    "karma-webpack": "~3.0.5",
    "mini-css-extract-plugin": "~0.6.0",
    "mocha": "~6.1.4",
    "mocha-reporter": "file:mocha-reporter",
    "mochawesome": "~3.1.2",
    "mochawesome-report-generator": "3.1.5",
    "mochawesome-screenshots": "1.6.0",
    "node-sass": "^4.12.0",
    "popper.js": "~1.15.0",
    "postcss-loader": "~3.0.0",
    "protractor": "5.4.2",
    "protractor-image-comparison": "3.1.0",
    "sass-loader": "~7.1.0",
    "sinon": "~7.3.2",
    "style-loader": "~0.23.1",
    "typescript": "~3.4.5",
    "url-loader": "~1.1.2",
    "webpack": "~4.30.0",
    "webpack-cli": "~3.3.1",
    "webpack-dev-server": "~3.3.1"
  },
  "dependencies": {
    "bluebird": "~3.5.4",
    "bootstrap": "3.3.7",
    "d3": "~5.9.2",
    "isomorphic-fetch": "2.2.1",
    "jquery": "^3.4.0",
    "jquery-ui": "~1.12.1",
    "knockout": "~3.5.0",
    "knockout-mapping": "~2.6.0",
    "lodash": "~4.17.11",
    "numeral": "~2.0.6",
    "page": "~1.11.4"
  }
}
import mochaBaseReporter from 'mocha/lib/reporters/base';
import { takeScreenShot } from './javascript/screenShots';
import { populateTestResults } from './javascript/testTree';
import {
  getFileContents,
  writeToOutputFile,
} from './javascript/fileSystemAccess';
import {
  getTemplate,
  parseTestsIntoOutput,
  addValuesToTemplate,
} from './javascript/templating';
import {
  SUCCESS,
  FAILURE,
  FINISHED,
} from './constants';

const addStyle = template => getFileContents('styles.css')
  .then(styles => addValuesToTemplate(template, { styles }))
  .catch(error => console.log('file read of styles.css failed', error));

const createReport = (outputDirectory, fileName, data) => getTemplate('report')
  .then(template => addValuesToTemplate(template, { 'test-suites': data }))
  .then(template => writeToOutputFile(outputDirectory, `${fileName}.html`, template))
  .catch(error => console.log('file read of template.html failed', error));

function mochaReporter(runner, environment) {
  const tests = {};
  const fileName = 'testfile';
  const { outputDir, testDir, takeScreenShotOnFailure } = environment.reporterOptions || {};
  const outputDirectory = outputDir && `${process.cwd()}/${outputDir}`;
  const accumulateTestResults = (test, image) => populateTestResults(test, testDir, tests, image);

  mochaBaseReporter.call(this, runner);

  runner.on(SUCCESS, accumulateTestResults);

  runner.on(FAILURE, test => (
    takeScreenShotOnFailure && window
      ? takeScreenShot()
      : Promise.resolve()
  ).then(image => accumulateTestResults(test, image)));

  runner.on(FINISHED, () => {
    parseTestsIntoOutput(tests)
      .then(addStyle)
      .then(template => addValuesToTemplate(template, runner.stats))
      .then(html => createReport(outputDirectory, fileName, html))
      .then(() => writeToOutputFile(
        `${outputDirectory}/history`,
        `test-run-${Date.now()}.json`,
        JSON.stringify(tests),
      ));
  });

  return runner;
}

module.exports = mochaReporter;
/* eslint-disable global-require */
/* eslint-disable @typescript-eslint/no-var-requires */
const protractor = require('protractor');
const { join } = require('path');

const testDirectory = '_src/js/tests';
const baseDirectory = `${testDirectory}/integration/`;

// specifies whether tests will be run in parralel or not
const shardTestFiles = true;

// specifies how many browsers/drivers may be run in parralel
const maxInstances = 4;

function onPrepare() {
  // register typescript file extensions with the babel compiler
  require('@babel/register')({ extensions: ['.js', '.ts'] });
  require('@babel/polyfill');

  // don't wait for angular (since our app is currently not angular)
  protractor.browser.waitForAngularEnabled(false);

  // hot fix for protractor strange map behavior
  // found here: https://github.com/angular/protractor/issues/2227#issuecomment-337249891
  protractor.ElementArrayFinder.prototype.map = function mapHotFix(mapFn) {
    return this.reduce((arr, el) => arr.concat(mapFn(el, arr.length)), []);
  };
}

exports.config = {
  // mocha configuration
  framework: 'mocha',
  mochaOpts: {
    reporter: './mocha-reporter',
    reporterOptions: {
      outputDir: `${testDirectory}/reports`,
      testDir: `${baseDirectory}/endToEnd`,
      takeScreenShotOnFailure: true,
    },
    timeout: 600000,
    slow: 3000,
  },
  seleniumAddress: 'http://localhost:4444/wd/hub',

  // turn off promise management in favor of async/await
  SELENIUM_PROMISE_MANAGER: false,

  // spec config
  specs: [`${baseDirectory}/endToEnd/**/*.spec.js`],

  // browser configuration
  timeout: 100000,
  multiCapabilities: [
    {
      browserName: 'chrome',
      shardTestFiles,
      maxInstances,
      chromeOptions: {
        args: [
          // 'show-fps-counter=true',
          '--headless',
          // '--disable-gpu',
          '--window-size=1300,1000',
        ],
      },
    },
    {
      browserName: 'firefox',
      shardTestFiles,
      maxInstances,
      'moz:firefoxOptions': {
        args: [
          '--headless',
        ],
      },
    },
  ],
  onPrepare,
  plugins: [
    {
      package: 'protractor-image-comparison',
      options: {
        baselineFolder: join(process.cwd(), `${baseDirectory}/screenshots/baseline/`),
        screenshotPath: join(process.cwd(), `${baseDirectory}/screenshots/tmp/`),
        formatImageName: '{tag}-{logName}-{width}x{height}',
        savePerInstance: true,
        autoSaveBaseline: true,
      },
    },
  ],
};

共有1个答案

羊时铭
2023-03-14

我找不到直接加载本地文件的方法,但是我给了它一个package.json,并用NPM直接将它安装到node_modules。具体地说,我跑了

npm安装./mocha-reporter--保存-dev

在project文件夹中创建package.json之后,在我的项目目录中。经过一些调试,我能够解决我的问题,因为包现在是节点的命名包的一部分。

 类似资料:
  • 我正在尝试为DropWizard Metrics创建一个自定义报告器,但找不到任何资源或示例来显示如何通过扩展来创建一个报告器。我可以通过遵循现有报告器(CSV、SLF4J等)来扩展这个类,但是我要在哪里注册它? 以下是我试图注册的自定义记者的摘录: 而且,这是创建自定义报告程序的正确方法还是其他方法? 先谢谢你。

  • # 5.1 自定义报告是什么? {#51-自定义报告是什么?} 在进行网站分析的时候,通常需要关联多个维度进行分析, 同时根据不同组合维度,选取合适的衡量指标。 例如,衡量 广告流量 达成的事件数 或 不同来源流量的所使用客户端及其地域分布。 在默认报告中,系统提供了单个维度的分析视角。 而自定义报告则可实现关联不同分析维度,衡量站点数据表现。 示例如下: 维度1 维度2 维度3 衡量指标 广告维

  • 背景: null > @test(groups={“init”})public void openURL() 包含用于启动webdriver并使用给定URL打开chrome>实例的webdriver代码。 @test(dependsonGroups={“init”})public void testLogin() 包含的webdriver代码指向: 1。找到用户名密码文本输入元素,从属性文件中输入

  • 使用指南 - 数据报告 - 访客分析 - 利用自定义变量报告自定义访客属性 通过自定义变量,可以提供您自定义的访客群体的网站访问状况,包括这个群体的网站访问次数、平均访问页数、跳出率等指标,帮助助您了解这个访客群体在您网站的访问情况。 在使用前要进行相关自变量的设置,具体请参考 在查看报告时,也可以对来源、访客过滤筛选查看。

  • 我正在从事一个需要以编程方式调用TestNG(使用数据提供程序)的项目。情况很好,只是在报告中,我们得到了@测试方法的名称,这是一种处理许多情况的通用方法。我们希望在报告中得到一个有意义的名字。 我一直在研究这个问题,发现了三种方法,但不幸的是,所有方法都失败了。 1) 实施ITest 我在这里和这里都发现了这个 我一输入@Test方法就设置了我想要的名称(对于我尝试的所有3种方式,这就是我设置名