Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite pytest-bdd to use hooks and marks #310

Open
youtux opened this issue Jul 10, 2019 · 8 comments
Open

Rewrite pytest-bdd to use hooks and marks #310

youtux opened this issue Jul 10, 2019 · 8 comments

Comments

@youtux
Copy link
Contributor

youtux commented Jul 10, 2019

Following the suggestion from issue #296, I will try to re-implement pytest-bdd to use marks and hooks.

I imagine an API that will look like this:

# test_some_scenario.py
pytestmark = pytest.mark.scenarios('a.feature')

# override "Scenario 3"
@pytest.mark.scenario('a.feature', 'Scenario 3')
def test_a_scenario(...):
    # do something more
    pass

instead of the current way:

import pytest_bdd

# override "Scenario 3"
@pytest_bdd.scenario('a.feature', 'Scenario 3')
def test_a_scenario(...):
    # do something more
    pass

scenarios('a.feature')

I yet don't know if this is entirely feasible without breaking other features, but we will see.

Short note about @scenario(...)

Potentially, I will also remove the single scenario mark/decorator, given that the only thing it allows to do is to require some more fixtures before the scenario is run, and to execute some more logic after the scenario finished.
This custom logic can already be implemented using given and then steps, but I understand that sometimes it can be just some compensation logic that should not appear in the feature file.
I still didn't figure out what to do with this, so probably I'll keep the @scenario mark/decorator for the time being.

@bubenkoff @olegpidsadnyi @sliwinski-milosz: Feedback on this idea would be appreciated.

@Vic152
Copy link

Vic152 commented Jul 22, 2019

I use @scenario to run things after the test. For example I reset my env to pristine condition when I run some destructive tests.
Edit: I did not know @scenario can override@scenarios

@youtux
Copy link
Contributor Author

youtux commented Jul 22, 2019

@Vic152 @Vic152 fair enough, but I would intend to still support running pre-test and post-test actions by using fixtures instead. So potentially your code would transform from this:

@scenario('a.feature', 'A scenario')
def test_a_scenario():
    do_cleanup()

to this:

pytestmark = [
    pytest.mark.scenarios('a.feature'),
    pytest.mark.usefixtures('cleanup_fixture'),
]

@pytest.fixture
def cleanup_fixture():
    yield
    do_cleanup()  # This would be run at the the end of each test

@Vic152
Copy link

Vic152 commented Jul 22, 2019

@youtux
What about if I wanted to use the cleanup fixture only in one scenario in the entire feature file? (I was trying to read pytest docs but the page seems to be down)

@elchupanebrej
Copy link

@youtux please review ideas from elchupanebrej#33 , they could be a good basement for this feature.

  • feature/scenario registry could be saved in a module-scoped fixture
  • parametrization could be done from pytest_generate_tests hook

@elchupanebrej
Copy link

Also, we could auto-collect Feature files using https://docs.pytest.org/en/7.1.x/example/nonpython.html ideas.
For example how this could be: all steps are defined at conftest.py plugins and feature files are placed in the same directory.

@youtux
Copy link
Contributor Author

youtux commented Jul 24, 2022

For example how this could be: all steps are defined at conftest.py plugins and feature files are placed in the same directory.

Yes, I wanted to do that for quite some time already, but never had the time to dig into that.

@elchupanebrej
Copy link

I'll try to implement PoC fo auto import in the nearest future

@elchupanebrej
Copy link

elchupanebrej commented Oct 6, 2022

@youtux please check the approach from elchupanebrej#74 and share your thoughts. That PR also uses ideas from #342 #542

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants