【问题标题】:Running setup code before a test suite in Node Unit在节点单元中的测试套件之前运行设置代码
【发布时间】:2014-01-22 21:08:47
【问题描述】:

在编写自动化系统/集成测试时,在所有测试之前运行的第一步通常是“启动服务器”。由于启动服务器的成本可能很高,因此最好只做一次,而不是在每次单独测试之前。 JUnit has easy functionality for doing this. nodeunit 中是否有等效的标准模式?还是需要手动滚动?

【问题讨论】:

    标签: node.js integration-testing nodeunit


    【解决方案1】:

    我不认为 N​​odeunit 有这个内置的,但是很多人用 Grunt 处理这样的任务。

    http://gruntjs.com/

    【讨论】:

      【解决方案2】:

      因为您使用 nodeunit 的测试套件只是节点模块,您可以利用该闭包进行全局设置/拆卸(仅适用于该测试套件):

      var myServer = require('./myservermodule');
      
      var testsRun = 0;
      var testsExpected = 3;
      
      function startTest(test) {
          test._reallyDone = test.done;
          test.done = function() {
              ++testsRun;
              test._reallyDone();
          };
      }
      
      module.exports = {
          'setUp' : function(cb) {
              if (!myServer.server) myServer.start(cb);
              else cb();
          },
          'tearDown' : function(cb) {
              console.log("Tests run: " + testsRun + "/" + testsExpected);
              if (testsRun===testsExpected) myServer.stop(cb);
              else cb();
          },
          'sometest1' : function(test) {
              startTest(test);
              test.expect(1);
              test.ok(true);
              test.done();
          },
          'sometest2' : function(test) {
              startTest(test);
              test.expect(1);
              test.ok(false);
              test.done();
          },
          'sometest3' : function(test) {
              startTest(test);
              test.expect(1);
              test.ok(true);
              test.done();
          }
      };
      

      【讨论】:

        【解决方案3】:

        是的,Nodeunit 有一个 setUp() 和一个 tearDown() 函数,它们总是在测试之前和之后运行。您可以使用setUp() 像这样启动您的服务器:

        var server = require("path/to/server.js");
        
        exports.setUp = function(callback) {
            server.start(8080, function() {
                callback();
            });
        };
        
        // run your tests here, setUp will be called before each of them
        

        这假设你在 server.js 中有:

        exports.start = function() {
            // start server here
        }
        

        tearDown() 函数在调用test.done() 之后运行。

        有关此示例,请在此处查看实际操作:https://github.com/jamesshore/Lessons-Learned-Integration-Testing-Node/blob/master/src/_server_test.js

        文档在这里:https://github.com/caolan/nodeunit#groups-setup-and-teardown

        【讨论】:

        • 应该更明确,但正如链接中所示,我要做的是在所有测试之前进行设置,而不是每次测试。
        • 啊,我明白了。很抱歉造成混乱。正如 Brad 所说,那么 Grunt 是一个不错的选择。
        【解决方案4】:

        有两种方法可以做到这一点:

        1. nodeunit 测试文件中的所有测试都是按顺序同步运行的。您可以将这组测试的设置代码放在第一个测试中,然后将拆解放在最后一个测试中。

        2. 如果您想更正式地执行此操作,并且不想为单元测试设置 Grunt,还有一个名为“nodeunit-async”的模块可以让您在之前运行全局设置和拆卸在你所有的测试之后。您可以在一组测试之前和之后运行一次全局设置和拆卸。

        这是 nodeunit-async 的简介:

        用于运行异步节点单元测试的轻量级包装器。当您希望为跨多个文件的每个测试运行通用的全局设置或拆卸函数,和/或在所有测试之前和之后运行一次夹具设置或拆卸函数时,特别有用。

        专为使用异步的自动和瀑布方法编写的单元测试而设计。

        https://github.com/Zugwalt/nodeunit-async

        【讨论】:

          猜你喜欢
          • 2017-05-02
          • 1970-01-01
          • 1970-01-01
          • 2016-10-18
          • 1970-01-01
          • 2023-04-11
          • 2013-12-04
          • 2014-11-24
          • 1970-01-01
          相关资源
          最近更新 更多