nodeunit icon indicating copy to clipboard operation
nodeunit copied to clipboard

single setUp/tearDown per suite/group?

Open funston opened this issue 12 years ago • 5 comments

The current examples I can find only show how to do a setup/teardown per test. Is it possible to:

  1. run a single test harness/suite setup/tear down
  2. barring that: a) get some hook into nodeunit to know how many tests are to be run (ie, if a -t option was used) b) get some notification when all the test.done() are called to write a custom "done" hook

funston avatar Nov 10 '12 01:11 funston

I could use something like this too. I am trying to test a REST service built using express. I would like to have an "outer" testcase that does setup of the express app (and of course teardown of that app), and do that once. I would have nested test cases to run the actual URL pattern tests against the REST service. As it is now, I start the web app with each test, and tear it down after each test. Not good.

One approach to addressing this might be to allow a flag on a nested setUp and teardown to NOT invoke the parent test case container's setUp and teardown.

sggraham32 avatar Feb 14 '13 15:02 sggraham32

Ultimately the parent setUp and tearDown should only be run on the group not on each test case. i.e:

exports.testCaseGroups2 = function (test) {
    var call_order = [];
    var s = {
        setUp: function (callback) {
            call_order.push('setUp');
            callback();
        },
        tearDown: function (callback) {
            call_order.push('tearDown');
            callback();
        },
        test1: function (test) {
            call_order.push('test1');
            test.done();
        },
        group1: {
            test2: function (test) {
                call_order.push('group1.test2');
                test.done();
            },
            test3: function (test) {
                call_order.push('group1.test3');
                test.done();
            }
        }
    };
    nodeunit.runSuite(null, s, {}, function (err, assertions) {
        test.same(call_order, [
            'setUp',
            'test1',
            'tearDown',
            'setUp',
            'group1.test2',
            'group1.test3',
            'tearDown'
        ]);
        test.done();
    });
};

chriskinsman avatar Apr 24 '15 15:04 chriskinsman

I know that this is super old, but it confused me for a bit, so for the sake of anyone else that ends up here. I found this to be false:

Ultimately the parent setUp and tearDown should only be run on the group not on each test case.

I stuck a console.log() in the setUp() method where several groups of tests were required (each requiring their own subgroups too) and my log message displayed just before each individual test in the console instead of before each group.

chasingmaxwell avatar May 22 '17 21:05 chasingmaxwell

I agree, just tested and the code posted by @chriskinsman follows this call order:

[
  'setUp',
  'test1',
  'tearDown',
  'setUp',
  'group1.test2',
  'tearDown',
  'setUp',
  'group1.test3',
  'tearDown'
]

@chasingmaxwell Have you found a nice workaround to fix this?

boggyhole avatar Jul 21 '17 05:07 boggyhole

@jonas-wilhelm unfortunately, I don't have a workaround handy. The project I was working on recently migrated to a different test framework.

chasingmaxwell avatar Jul 21 '17 21:07 chasingmaxwell