Programatically skip tests?

411 views
Skip to first unread message

Doug Reeder

unread,
Jul 9, 2014, 12:02:50 PM7/9/14
to moc...@googlegroups.com
My web app runs in several environments, some of which don't support all the functionality. (For example, the DeviceStorage API is only available on Firefox OS.)  I can skip a test by modifying the test code (adding .skip to it() or describe()), but then it's skipped in all environments.  Is there a way to skip a test based on a run-time check?

Vlad GURDIGA

unread,
Jul 10, 2014, 3:41:37 AM7/10/14
to moc...@googlegroups.com
I had a similar question when I wanted to be able to only run a subset of tests. For example I want to run my unit tests very often, but not the acceptance and integration tests, because they take longer.

My hack around this is composed of two parts:
  1. a global flag of some kind that you set when you run the tests. In my case, because I run them in the browser, I use a keyword in location.hash, for example “skip-integration-tests”¹.
  2. implement wrappers for Mocha’s built-in describe:  describe.integration and describe.acceptance which look at that flag and decide wether to run that block or skip it.
So now, when I write an acceptance test I use describe.acceptance instead of the built-in describe, and it knows to run only when appropriate based on how I run the tests. For example:

make test — runs all the tests
SKIP_INTEGRATION_TESTS=1 make test — skips integration tests

I’m not bothered by the command length because I usually have it under a build task. For example, my default make task is to run all the unit tests:

default: @SKIP_INTEGRATION_TESTS=1 SKIP_ACCEPTANCE_TESTS=1 make test

so when I say “make” it runs this command. I guess something similar can easily be done in grunt, gulp, rake, ant, etc.

I hope this helps. As Mark Twain said, sorry for a that long explanation, I didn’t have time to write a shorter one. :)


¹ I used the negative “skip” because I wanted all the tests to run by default, and only exclude some of them when I wanted and I knew what I’m doing, just to be safe. ;-)

Doug Reeder

unread,
Jul 10, 2014, 11:34:20 AM7/10/14
to moc...@googlegroups.com
Ah, conditionally calling describe() or describe.skip()!  That will do most of what I want.

I also have some date-formatting functions, where the expected value depends on the language the browser is set to:
        switch (languageCode) {
            case 'en':
                expect(humanDate(sixDaysAhead)).to.equal('This ' + daysOfWeek[sixDaysAhead.getDay()]);
                break;
            case 'de':
                expect(humanDate(sixDaysAhead)).to.equal('Nächsten ' + daysOfWeek[sixDaysAhead.getDay()]);
                break;
        }

Is there a way to flag a test as skipped, after it's been started?  It would be nice to add a default statement, for other languages, to my switch statement,which skipped the test instead of calling expect().

Vlad GURDIGA

unread,
Jul 11, 2014, 3:15:58 AM7/11/14
to moc...@googlegroups.com
I think I usually use separate it() blocks instead of one that has a switch/case statement.

In this particular case though, if I’d read your test out loud, I’d say: “when the language is English, it should return [this value]”, which in Mocha’s DSL would usually look like:

describe('l10n', function() {
describe('English', function() {
it('returns the date in this here format', function() {
expect(humanDate(sixDaysAhead)).to.
equal('This ' + daysOfWeek[sixDaysAhead.getDay()]);
});
});

describe('German', function() {
it('returns this other specific date format', function() {
...
});
});
})

usually strive to have my tests as linear as possible, meaning no if/case statements. But this is the ideal case, you know… ;-)
Reply all
Reply to author
Forward
0 new messages