cucumber / language-service

Cucumber Language Service
MIT License
12 stars 22 forks source link

Add pytest-bdd support #84

Closed sevapopov2 closed 1 year ago

sevapopov2 commented 1 year ago

Hello, I am trying to contribute to this project by adding pytest-bdd plugin to support pytest framework in this extension. This feature suggestion was described in issue #80. I tryed to perform first steps in adding this plugin's support by including given steps definition into python steps definition file. Unfortunately, I am not familiar with Typescript and I am not sure how to add other step definition keywords, but I also need to add scenario step that provides a path to scenario, the point where pytest-bdd test step definitions usually start I will appreciate all help in including the rest steps for a pytest-bdd future support. Thank you!

aslakhellesoy commented 1 year ago

Hi @sevapopov2 it looks to me like pytest-bdd uses a similar API/syntax for step definitions as behave (which the Python support was designed for).

Is the VSCode extension not recognising pytest-bdd step definitions? Can you share the console output?

sevapopov2 commented 1 year ago

Hello, Thank you for your help! Yes, I checked behave's step definitions and they are really similar to pytest-bdd's. Yes, the problem is that it isn't possible to create step definition within cucumber extension even when one step is already defined with pytest-bdd in tests/step_defs. The console_output will be attached below. From what I read in it, the server cannot even find feature file. It's located in tests/features. Thank you! console_output.log

aslakhellesoy commented 1 year ago

Thanks for the log. Here is the relevant part:

[Info  - 12:20:53 PM] Unable to generate step definition. Please create one first manually.
[Info  - 12:20:56 PM] Reindexing...
[Info  - 12:20:56 PM] * Found 0 feature file(s) in ["features/**/*.feature","src/test/**/*.feature"]
[Info  - 12:20:56 PM] * Found 0 steps in those feature files
[Info  - 12:20:56 PM] * Found 0 glue file(s) in ["features/**/*.php","features/**/*.rb","features/**/*.ts","features/**/*.py","src/test/**/*.java","*specs*/**/*.cs"]
[Info  - 12:20:56 PM] * Found 0 parameter types in those glue files
[Info  - 12:20:56 PM] * Found 0 step definitions in those glue files
[Info  - 12:20:56 PM] * Built 0 suggestions for auto complete
[Info  - 12:20:56 PM] Unable to generate step definition. Please create one first manually.

The last line says:

Unable to generate step definition. Please create one first manually.

It appears that your cucumber.glue globs don't point to any existing .py files. You need at least 1 existing step definition in order for the Generate step definition action to work. From the IMPORTANT section:

MPORTANT: Generate step definition will only be enabled if there is at least one existing step definition. This is so that the extension can determine the programming language for the step definition, and provide choices for existing files to add it to.

sevapopov2 commented 1 year ago

Hello, I added paths recommended for pytest-bdd to configurations you mentioned and most of the step definitions work but I have some questions: Would it be possible to add the new recording to the cucumber feature default paths for convincing pytest-bdd's standards? Adding "tests/features//*.feature" as path, because it's recommended to put all tests for pytest in tests folder. From my experience "features/*/.feature" didn't work for pytest-bdd feature files definition in the project. The same question is about step definitions: Would it be possible to add "tests/step_defs//*.py" as the path to cucumber glue defaults for Python? And another question: As I told, all steps such as given, when, then work with Pytest_bdd after explicitely writing paths mentioned above. How can I add scenario decorator? It is the decorator to point the scenario we are working on. It looks like that: @scenario('path_to_feature_file.feature', 'scenario_name') def test(): pass It can be really useful to start writing the scenario and can really help in saving the time on copying/writing the path to the feature file. Thank you in advance!

aslakhellesoy commented 1 year ago

Adding "tests/features//*.feature" as path, because it's recommended to put all tests for pytest in tests folder. From my experience "features//.feature" didn't work for pytest-bdd feature files definition in the project.

Did you mean tests/features/**/*.feature? Sure, we can add that. I suggest tests/**/*.feature

Would it be possible to add "tests/step_defs/**/.py" as the path to cucumber glue defaults for Python?

Sure, I suggest adding tests/**/*.py instead

How can I add scenario decorator?

I assume you mean this:

@scenario('publish_article.feature', 'Publishing the article')
def test_publish():
    pass

This seems to be a pytest-bdd feature that is at odds with the standard Cucumber implementations: The ability to scope step definitions to specific feature files.

This is not possible in standard Cucumber, and this is a deliberate design choice we won't change. We're therefore not going to change this in this plugin either. Sorry.

sevapopov2 commented 1 year ago

Hello, Thank you for your answer! Yes, I meant exactly this path to feature files, for some reason double asterisk isn't shown in the web version of github while reading the comment. Yes, I agree with step_definitions path. As far as the scenario decorator, that's fine, there is no problem for me that it cannot be added. Should be separate given import added as with behave to step definitions python file? I tried to do that but I am not sure if I did it correctly. I will mark this pull request as a review then for you to check. Thank you for your help!

aslakhellesoy commented 1 year ago

I'm not sure what you're trying to accomplish with this PR. You have only modified test data, and that's not going to have any impact on the library.

Please try the VSCode Cucumber extension 1.2.11 which contains these changes:

I am closing this PR

sevapopov2 commented 1 year ago

Hello, At the moment of making changes to test-data, I thought it's necessary to add the separate given import for pytest-bdd to work. As it turned later it was only needed to update globs. Thank you again for your help, clarifying how the extension works and adding the support for the plugin!