如何从karma runner套件中获取通过测试的列表?

Ami*_*ole 40 javascript unit-testing karma-runner

当我在我的webapp上运行业力时,我只获得通过测试传递的通用消息 - 有没有办法获得通过测试的列表?如何获得更详细的输出?

我在文档中的任何地方都找不到这个.

ben*_*ope 50

我知道怎么做!

Karma的终端输出来自名为Reporters的对象.Karma附带一些内置记者(可以在其中找到karma/lib/reporters).Karma也可以使用自定义记者.

您可以指定项目karma.config.js文件中使用的记者.

例如,'dots'报告者只是在每次测试通过时打印一个点:

reporters: ['dots'],
Run Code Online (Sandbox Code Playgroud)

'进步'记者打印的不仅仅是点:

reporters: ['progress'],
Run Code Online (Sandbox Code Playgroud)

当测试成功或失败时,自定义报告者karma-spec-reporter会打印每个测试的名称(但不是很多):

reporters: ['spec'],
Run Code Online (Sandbox Code Playgroud)

你可能想要推销自己的记者,因为karma-junit-reporter,karma-spec-reporter和包含的记者可能无法满足你的需求.

我猜测定制karma-spec-reporter是这种情况下的最佳选择,因为它在测试成功时已经打印了一行.

如果您正在寻找更简单易用的东西,这里是我建立的自定义记者.它报告没有终端颜色的传递和失败测试.

  • 通过`npm install karma-spec-reporter --save-dev`安装karma-spec-reporter.希望能帮助别人,因为我不清楚如何使用. (8认同)
  • 感谢@AnsonKao还需要在`karma.conf`的插件部分指定`karma-spec-reporter` (4认同)

Mat*_*att 41

我推荐Karma Spec Reporter.这将为您提供一个漂亮的单元测试报告.

Karma单元测试规范

如何使用它:

  1. 安装Karma Spec Reporter

在项目的命令行中,

npm install karma-spec-reporter --save-dev

  1. 将Karma Spec Reporter添加到配置中

karma.conf.js,

...
  config.set({
  ...
    reporters: ["spec"],
    specReporter: {
      maxLogLines: 5,         // limit number of lines logged per test
      suppressErrorSummary: true,  // do not print error summary
      suppressFailed: false,  // do not print information about failed tests
      suppressPassed: false,  // do not print information about passed tests
      suppressSkipped: true,  // do not print information about skipped tests
      showSpecTiming: false // print the time elapsed for each spec
    },
    plugins: ["karma-spec-reporter"],
  ...
Run Code Online (Sandbox Code Playgroud)

就这些.请享用.


小智 7

将此插件与业力0.9.0或更高版本一起使用

https://github.com/mlex/karma-spec-reporter