使用HTML报告与Mocha测试框架 [英] Using HTML reporting with Mocha test framework

查看:81
本文介绍了使用HTML报告与Mocha测试框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用NodeJS和Mocha生成一些测试,我想找到一种方法将结果放入浏览器中。我知道Mocha使用'html'记者和 mocha init< dir> 对此有所支持,但似乎两者似乎都不适用于我(记者实际上在没有运行的情况下抛出错误测试)。

I've been generating some tests using NodeJS and Mocha, and I'd like to find a way to place the results into a browser. I know that Mocha has support for this using 'html' reporter and mocha init <dir> however neither seem to be working for me (the reporter actually throws errors without even running a test).

有人能给我一个通过Mocha运行测试并生成HTML报告的好例子吗?我想模仿的一个例子是 visionmedia 网站。此外,为了示例,我们会说我正在使用名为 example.js 的测试文件。

Could someone give me a good example of running a test via Mocha and generating a HTML report?An example I want to mimic is on the visionmedia site. Also, for examples sake we'll say I'm using a test file called example.js.

谢谢提前获得任何帮助,令人惊讶的是,周围的例子很少。

Thanks in advance for any assistance, it's surprising there are so few example pieces around.

推荐答案

让Mocha在两个地方运行你的测试浏览器和终端遵循这个小教程:

To get Mocha to run your test in both browser and in the terminal follow this small tutorial:

我假设正常node.js mocha测试套件有以下插件。

I'm assuming the following plugins for a normal node.js mocha test suite.


  1. Node.js

  2. Mocha

以下树结构:

/root
  /test
    my_something_spec.js
  /javascript
  index.html



index.html



免责声明:我公然放弃了各种最佳做法,只是为了指出正确的方向。

index.html

Disclaimer: I've blatantly forgone all kinds of best practices but just to point you in the right direction.

<html>
<head>
    <meta charset="utf-8">
    <title>Mocha Tests</title>
    <link rel="stylesheet" href="node_modules/mocha/mocha.css" />
</head>
<body>
    <div id="mocha"></div>
    <script src="node_modules/mocha/mocha.js"></script>
    <script>mocha.setup('bdd')</script>
    <script src="test/my_something_spec.js"></script>
    <script>
        mocha.checkLeaks();
        mocha.run();
    </script>
</body>
</html> 



test / my_something_spec.js



test/my_something_spec.js

describe("my function", function() {
  it("is a function", function() {
    expect(true).to.be(true);
  });
});

使用简单的python服务器提供服务 python -m SimpleHTTPServer 8080 并访问 localhost:8080 将为您提供一个不错的测试失败。
从终端运行摩卡将为您提供相同的输出, expect 未定义。

Serving this up with a simple python server python -m SimpleHTTPServer 8080 from the root and visit localhost:8080 will give you a nice and failing test. And running mocha from the terminal will give you the same output, that expect isn't defined.

这篇关于使用HTML报告与Mocha测试框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆