Behat / Behat

BDD in PHP
http://behat.org
MIT License
3.9k stars 612 forks source link

Skip a scenario entirely based on @BeforeScenario #1437

Open jnicola opened 1 year ago

jnicola commented 1 year ago

I'd like to be able to skip a scenario entirely based on some conditional checks in a @BeforeScenario.

For some of our test environments, we don't fully import the default content since it would bog down testing.

However, I want to be able to setup nightly test runs that do have the default content brought in.

Tests marked with a tag such as requiresInitialImport would check against the database to see if an import has been performed. If it hasn't, skip the test it's okay. If it has however had imports performed, it's fair game!

jnicola commented 1 year ago

...also I know I can just have different environments / jenkins jobs exclude certain tags etc etc. I'm really not looking to solve this with tag exclusion, if possible.

barryswaisland-eagleeye commented 11 months ago

Just add this to a context...

    /**
     * @BeforeScenario
     */
    public function beforeScenario(BeforeScenarioScope $scope)
    {
        $tags = array_merge($scope->getScenario()->getTags(), $scope->getFeature()->getTags());
        if (in_array('requiresInitialImport', $tags, true) && !$this->databaseIsImported()) {
            throw new PendingException('required initial import'); 
        }
    }
usu commented 8 months ago

Just add this to a context...

    /**
     * @BeforeScenario
     */
    public function beforeScenario(BeforeScenarioScope $scope)
    {
        $tags = array_merge($scope->getScenario()->getTags(), $scope->getFeature()->getTags());
        if (in_array('requiresInitialImport', $tags, true) && !$this->databaseIsImported()) {
            throw new PendingException('required initial import'); 
        }
    }

Thanks. This worked for me when developing locally, however not for CI: Reason is that exit code is set to 1 (failed), whenever an exception is thrown during setup hooks.

Anyone has an idea on how to tweak exit code?

barryswaisland-eagleeye commented 8 months ago

how about https://github.com/tkotosz/behat-skip-tests/ ?