Codeceptjs: How to skip a test by Helper / hooks?

Created on 10 Aug 2017  路  10Comments  路  Source: codeceptjs/CodeceptJS

What are you trying to achieve?

I try to create a helper to help me to skip some test cases for specific browser, but I can not find document about how to skip a test case. Any suggestion?

  • So far I can use beforeStep (step) to know what's the next step and the arguments, but I don't know how to skip the step (or all the steps) . <= this maybe anoterh way to help me skip "all steps in a test case" , not so good but may work.

  • Or, refer to https://github.com/mochajs/mocha/issues/1901 , is there any similiar api in codeceptjs?

enhancement question

Most helpful comment

Hi,
I know that I arrive more than a year late, but I found a workaround for it.
If in your hook/helper you want to skip a test, I override the function run as

test.run = function skip() {
          this.skip()
        }

And it skipped the test.
Is not the most elegant way, but it works

All 10 comments

@zordius
Hi!
Unfortunately there is no ability to skip tests from scenario code now. We use promise chain for scenarios, so async method this.skip is not working as expected for it.
Maybe somebody will help us with it (or after some research I will implement it later)

But now you can use grep feature to mark tests, that you want to run

You should place your test for different browsers in different files and add tags in Feature (e.g. @chrome, @firefox, @all, etc.)
And then use this tags as filters

`codeceptJS run --grep='@chrome|@all'

Ya, we already adopt this practice, but the practice can just skip a test file and I am looking for a way to control test case more programmatically.

Here are some possible designs:

  • A new recorder API to insert a CANCEL action BEFORE the queue as first action.
  • A way to let _before() function to do CANCEL, use the previous API.
  • something like options.filter = function (testCase) {} in config to filtering test cases by any logic <= this may be more easy

Maybe using decorators to tag features and scenarios is a interesting solution. Other tags, such as the mentioned @firefox, @chrome, etc., could be added too.

Hi,
I know that I arrive more than a year late, but I found a workaround for it.
If in your hook/helper you want to skip a test, I override the function run as

test.run = function skip() {
          this.skip()
        }

And it skipped the test.
Is not the most elegant way, but it works

I'm using this for skipping of scenario (e.g. when the scenario is unreliable in Firefox):

if (require("codeceptjs").config.get().helpers.WebDriver.browse == "firefox") {
    return;
}

It's placed at the begin of Scenario(). Of course you can have a "shortcut" method for that in a step object's/helper method if you want to call in more scenarios.

I think we should create method like SkipIf(() => 2 > 2).Scenario

Hi,
I know that I arrive more than a year late, but I found a workaround for it.
If in your hook/helper you want to skip a test, I override the function run as

test.run = function skip() {
          this.skip()
        }

And it skipped the test.
Is not the most elegant way, but it works

Where/How do you add this? I'm familiar with Page Objects and Helper (extend codeceptjs.helper) but I'm not yet familiar with building hooks.
I'm trying to skip a BDD/Gherkin setup but haven't found a solution that doesn't involve reverting from using Given/When/Then style and back to Scenario style.

@ryaxc , you just need to register a script to define your hooks and capture events. Just add the following block to your codecept.conf.js:

  hooks: [
    'hooks/my_hooks.js',
  ],

and, inside the hooks/my_hooks.js' file, add some code like this:

const { event } = require('codeceptjs');

event.dispatcher.on(event.test.before, function (test) {
    if (test.tags.includes('@skip') {
      test.run = function skip() {
        this.skip()
      }
    }
});

Hope that helps.

But it would be nice to introduce a proper SkipIf() function like @Vorobeyko was suggesting.

Consider this as a workaround as well:

(skipConditionIsMet ? xScenario : Scenario) ('title', async () => {
     // test code
})

It's a bit ugly to read, but it works as expected.

@ngraf This is good and I prefer to this way :)

Was this page helpful?
0 / 5 - 0 ratings