Behat: 3.4.0 breaks the ability to run individual tests
Update + quick fix
This is a long standing issue which is affecting many users. It has been solved but the PR is not yet accepted. If you are affected by this you can solve it by adding the following lines to your composer.json
:
"require-dev": {
"behat/gherkin": "^4.6.0",
"cweagans/composer-patches": "^1.6.7",
},
"patches": {
"behat/gherkin": {
"Correctly filter features, now that the base path is correctly set": "https://github.com/Behat/Gherkin/compare/v4.6.0...a7d84eb6ba245ea268a304312d14660788bf6c29.patch"
}
}
Note that this problem is not caused directly by Behat but it is due to a confusion between absolute and relative paths in the Behat/Gherkin
library. We are keeping this issue open in the Behat queue so that it is easier to find for people who encounter this problem when using Behat.
If possible please vote for + review + approve the fix at https://github.com/Behat/Gherkin/pull/129 - hopefully once it is approved it can be merged.
Original summary
This was originally reported at https://github.com/Behat/Gherkin/issues/128 but it appears to be a bug in Behat and not in Gherkin.
In https://github.com/Behat/Behat/pull/1056 a change was introduced to make it possible to run individual tests from inside a different folder than where the tests are located. Unfortunately this breaks the more common use case of running the tests from inside the actual folder where they are located.
To replicate this, create a folder structure in which the tests are located in a subfolder of the root of the project, e.g in the tests/
folder:
$ composer require behat/behat-3.4.0
$ mkdir tests && cd tests/
$ ../vendor/bin/behat --init
# Create a test
$ vim features/homepage.feature
# Make sure the gherkin cache is cleared, this might "hide" the problem
$ rm -rf /tmp/behat_*
# Try to run a single test
$ ../vendor/bin/behat features/homepage.feature -vvv
Result:
$ ../vendor/bin/behat features/homepage.feature -vvv
No scenarios
No steps
0m0.73s (9.22Mb)
[Behat\Testwork\Tester\Exception\WrongPathsException]
No specifications found at path(s) `features/homepage.feature`. This might be because of incorrect paths configuration in your `suites`.
Exception trace:
() at /home/pieter/v/joinup-dev/vendor/behat/behat/src/Behat/Testwork/Tester/Cli/ExerciseController.php:111
Behat\Testwork\Tester\Cli\ExerciseController->execute() at /home/pieter/v/joinup-dev/vendor/behat/behat/src/Behat/Testwork/Cli/Command.php:63
Behat\Testwork\Cli\Command->execute() at /home/pieter/v/joinup-dev/vendor/symfony/console/Command/Command.php:264
Symfony\Component\Console\Command\Command->run() at /home/pieter/v/joinup-dev/vendor/symfony/console/Application.php:841
Symfony\Component\Console\Application->doRunCommand() at /home/pieter/v/joinup-dev/vendor/symfony/console/Application.php:206
Symfony\Component\Console\Application->doRun() at /home/pieter/v/joinup-dev/vendor/behat/behat/src/Behat/Testwork/Cli/Application.php:124
Behat\Testwork\Cli\Application->doRun() at /home/pieter/v/joinup-dev/vendor/symfony/console/Application.php:125
Symfony\Component\Console\Application->run() at /home/pieter/v/joinup-dev/vendor/behat/behat/bin/behat:34
Using git bisect
I have pinpointed the introduction of this bug to https://github.com/Behat/Behat/commit/08802b7f2d736b88df339e5411a1c722e92adbe3
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 22
- Comments: 50 (16 by maintainers)
Commits related to this issue
- Temporarily lock Behat until a bug that affects us is fixed. Ref. https://github.com/Behat/Behat/issues/1076 — committed to ec-europa/joinup-dev by pfrenssen 7 years ago
- Temporarily lock Behat until a bug that affects us is fixed. Ref. https://github.com/Behat/Behat/issues/1076 — committed to ec-europa/joinup-dev by pfrenssen 7 years ago
- downgrade behat, due to single feature command line problems https://github.com/Behat/Behat/issues/1076 — committed to ministryofjustice/opg-digi-deps-client by elvisciotti 7 years ago
- lock down behat version pending a resolution to Behat/Behat/issues/1076 and Behat/Behat/issues/1108 — committed to acolono/opencounter_api by rosenstrauch 6 years ago
- Merge branch 'release/1.2.1' * release/1.2.1: update theme and exampleclient paths lock down behat version pending a resolution to Behat/Behat/issues/1076 and Behat/Behat/issues/1108 bump api v... — committed to acolono/opencounter_api by rosenstrauch 6 years ago
- Merge tag '1.2.1' into develop maintainance release * tag '1.2.1': update theme and exampleclient paths lock down behat version pending a resolution to Behat/Behat/issues/1076 and Behat/Behat/is... — committed to acolono/opencounter_api by rosenstrauch 6 years ago
On a side note, I was able to run tests again from PhpStorm after in the Run/Debug configuration I set the
Custom working directory
option (in the default configuration for Behat) to the directory in whichbehat.yml
lives (it needs to align with whatever in Behat is%paths.base%
).Revert to 3.3 for now, I’ll try to look into the bug ASAP.
We also can’t run a single feature anymore since the update to Behat 3.4. We start a single feature with
bin/behat feature/natarch-continuous/ArchiveLandingPage.feature
- I seem to have found the culprit within the Behat\Behat\Gherkin\Specification\Locator\FilesystemFeatureLocator::locateSpecifications() - That method instantiates the LazyFeatureIterator with the $locator = ‘features/natarch-continuous/ArchiveLandingPage.feature’ and $filters = array(new PathsFilter($suiteLocators)); If I set the $filters = []; Then the single feature is run. I can also runbin/behat features/natarch-continuous/
and it finds and runs the features inside the directory.This is our behat.yml configuration.
Can we remove $filters = array(new PathsFilter($suiteLocators)); from the FilesystemFeatureLocator?
@milkovsky the fix was actually in behat/gherkin 4.6.1 which is what @ciaranmcnulty is trying to say here – i got confused there for a second, i thought there was a “behat/behat” 4.6.1 version 😃. Since it was a patch when you updated behat it probably updated gherkin too.
Thanks @ciaranmcnulty !
Update to behat 3.6.1 fixed the issue for me.
Still happening, here is how I work around it (I want to run Behat in PhpStorm):
And I create the
tests/tests_bootstrap.php
file with:I have updated the PR with the proposed fix: https://github.com/Behat/Gherkin/pull/129
This should be resolved in 4.6.1
@pfrenssen ignore my previous comment about tests being run per suite. apparently if you do not specify a specific suite, then Behat will loop through all the suites. it will perform any BeforeSuite/AfterSuite hooks even if it has no scenario’s to run in that suite. so that’s what I was seeing being run per suite, not the scenario itself, just the BeforeSuite/AfterSuite cycling. the scenario’s themselves were only executed once.
so your branch works fine for me 😃 it’s been two years since this bug was introduced, it’d be nice to have it fixed and no longer need to set a custom working directory
and make sure that you clear Gherkin cache
This is still a problem for me with behat + gherkin. I was able to work around it by adding a unique tag to tests I want to run individually and just reference that with behat using
--tags @MEDSTAT-PUBLICATION-BUCKETS
. That seems to be an okay temporary solution until this is fixed.Hi,
Is there any progress made on this? We’re stuk on Behat 3.3 as well because of this.
Using @warmwaterkruik his information I tried to dive deeper into it and it’s a real maze.
What I encountered was in
PathsFilter
the methodisFeatureMatch
checks if the feature file is in the correct path.https://github.com/Behat/Gherkin/blob/e66888c99c97b85113822b9f37276c2f9c414c97/src/Behat/Gherkin/Filter/PathsFilter.php#L53
However
$feature->getFile()
resolves a relative path and not an absolute path, then if your working directory is not the base directoryrealpath()
won’t resolve the file, the function returnsfalse
and the feature isn’t executed. Basically what’s discussed in the already referenced #1085 and possibly related to #1056 .For linux, it is located in the /tmp dir I believe. Also, you can disable the cache in your behat.yml file, using this:
Full answer here.