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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16897623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:24:26  来源:igfitidea点击:

How can I get a list of passing tests from karma runner suite?

javascriptunit-testingkarma-runner

提问by Amit Erandole

When I run karma on my webapp, I only get generic messages like tests passed - is there a way to get a list of passing tests? How do I get more verbose output?

当我在我的 web 应用程序上运行 karma 时,我只会收到类似测试通过的通用消息 - 有没有办法获取通过测试的列表?如何获得更详细的输出?

I cannot find this anywhere in the documentation.

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

回答by benshope

I know how this can be done!

我知道如何做到这一点!

Karma's terminal output comes from objects called Reporters. Karma ships with some built-in Reporters (they can be found in karma/lib/reporters). Karma is also able to use custom Reporters.

Karma 的终端输出来自称为 Reporters 的对象。Karma 附带了一些内置的 Reporters(它们可以在 中找到karma/lib/reporters)。Karma 还可以使用自定义 Reporters。

You can specify which reporters are used in your project's karma.config.jsfile.

您可以指定在您的项目karma.config.js文件中使用哪些报告器。

For example, the 'dots' reporter just prints a dot when each test passes:

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

reporters: ['dots'],

The 'progress' reporter prints more than dots:

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

reporters: ['progress'],

The custom reporter karma-spec-reporterprints the name of each test when the test succeeds or fails (but not much else):

自定义报告器karma-spec-reporter在测试成功或失败时打印每个测试的名称(但没有其他太多):

reporters: ['spec'],

You may want to roll your own reporter, since karma-junit-reporter, karma-spec-reporter, and the included reporters may not meet your needs.

您可能想要滚动自己的记者,因为 karma-junit-reporter、karma-spec-reporter 和包含的记者可能无法满足您的需求。

I am guessing that customizing karma-spec-reporter is the best option in this case, since it already prints a line when a test succeeds.

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

If you are looking for something even more simple to work from, hereis a custom reporter that I built. It reports passing and failing tests with no terminal colors.

如果您正在寻找更简单的工作方式,这里是我构建的自定义报告器。它报告通过和失败的测试,没有终端颜色。

回答by Matt

I recommend the Karma Spec Reporter. This will give you a pretty unit test report like this.

我推荐 Karma Spec Reporter。这会给你一个漂亮的单元测试报告。

Karma unit test spec

Karma 单元测试规范

How to use it:

如何使用它:

  1. Install the Karma Spec Reporter
  1. 安装 Karma Spec Reporter

On the command line in your project,

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

在您项目的命令行中,

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

  1. Add Karma Spec Reporter to the configuration
  1. 将 Karma Spec Reporter 添加到配置中

In 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"],
  ...

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"],
  ...

That is all. Enjoy.

就这些。享受。

回答by Satshabad

Use this plugin with karma 0.9.0 or later

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

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

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

回答by grant

You could add logs to your test spec. Check out log4js-node:

您可以将日志添加到您的测试规范中。查看 log4js-node:

https://github.com/nomiddlename/log4js-node

https://github.com/nomiddlename/log4js-node