WooDzu
WooDzu

Reputation: 4866

How to skip or ignore programmatically a suite in CodeceptJS

As the test suite grows I need to be able to run something in BeforeSuite() which will connect to external suite and skip the suite if an external resource is unavailable.

Feature('External Server');

BeforeSuite((I) => {
  // Check if server is available and skip all scenarios if it is not
});

Scenario('Login to the server', (I) => {
  // Do not run this if the server is not available
})

I understand I could probably set a variable, but I think it would be nice if there was a way to tell the runner that a suite has been skipped.

The goal is to have a suite marked as skipped in the output eg:

Registration --
  ✓ Registration - pre-checks in 4479ms
  ✓ Registration - email validation in 15070ms
  ✓ Registration - password validation in 8194ms

External Server -- [SKIPPED]
  - Login to the server [SKIPPED]

Upvotes: 1

Views: 3240

Answers (3)

Flint Weather
Flint Weather

Reputation: 11

My answer is compiled from a number of comments on the CodeceptJS github and stackoverflow. However, I can't recall the exact links or comments which helped me derive this solution, it's been at least a year, maybe two, since I started and have slowly modified this.


Edit: Found the github thread - https://github.com/codeceptjs/CodeceptJS/issues/661

Edit2: I wrote a post about "selective execution" (which avoids tagging unwanted tests with skip status) https://github.com/codeceptjs/CodeceptJS/issues/3544 I'll add a snippet at the bottom.


I'm on CodeceptJS 3.3.6

Define a hook file (eg: skip.js) and link it to your codeceptjs.conf.js file.

exports.config = { 
...
  plugins: {
    skipHook: {
      require: "../path/to/skip.js",
      enabled: true,
    }
  }
...
}

The basic code in skip.js is

module.exports = function () {
  event.dispatcher.on(event.test.before, function (test) {
    const skipThisTest = decideSkip(test.tags);
    if (skipThisTest) {
      test.run = function skip() {
        this.skip();
      };
      return;
    }
  });
};

I've defined a decision function decideSkip like so:

function decideSkip(testTags) {
  if (!Array.isArray(testTags)) {
    output.log(`Tags not an array, don't skip.`);
    return false;
  }

  if (testTags.includes("@skip")) {
    output.log(`Tags contain [@skip], should skip.`);
    return true;
  }

  if (
    process.env.APP_ENVIRONMENT !== "development" &&
    testTags.includes("@stageSkip")
  ) {
    output.log(`Tags contain [@stageSkip], should skip on staging.`);
    return true;
  }
}
(Mine is a bit more complicated, including evaluating whether a series of test case ids are in a provided list but this shows the essence. Obviously feel free to tweak as desired, the point is a boolean value returned to the defined event listener for event.test.before.)

Then, using BDD:

@skip @otherTags
Scenario: Some scenario
  Given I do something first
  When I do another thing
  Then I see a result

Or standard test code:

const { I } = inject();
Feature("Some Feature");

Scenario("A scenario description @tag1 @skip @tag2", async () => {
  console.log("Execute some code here");
});

I don't know if that will necessarily give you the exact terminal output you want External Server -- [SKIPPED]; however, it does notate the test as skipped in the terminal and report it accordingly in allure or xunit results; at least insofar as CodeceptJS skip() reports it.


For "selective execution" (which is related but not the same as "skip"), I've implemented a custom mocha.grep() utilization in my bootstrap. A key snippet is as follows. To be added either to a bootstrap anonymous function on codecept.conf.js or some similar related location.

  
  const selective = ["tag1", "tag2"];
  const re = selective.join("|");
  const regex = new RegExp(re);
  mocha.grep(regex);

Upvotes: 0

user3287106
user3287106

Reputation: 1

you can use

    Scenario.skip

in your step definition to skip a scenario. Note: if any steps have been executed before skipping then it will still show it in the report

https://codecept.io/basics/#todo-test

Upvotes: 0

bboydflo
bboydflo

Reputation: 947

maybe prepend x before every scenario in your feature? example xScenario. I don't think codecept supports something similar to only for features. it currently works with scenarios only as far as I know.

Upvotes: 0

Related Questions