pytest-bdd icon indicating copy to clipboard operation
pytest-bdd copied to clipboard

Generate py.test code and fixtures from .feature files

Open prologic opened this issue 12 years ago • 12 comments

It would be nice if py.test_bdd provided a cli tool to automatically generate the necessary structures from .feature file(s). I think I would find it painful to have to reproduce the contents of .feature files in my tests.

prologic avatar Dec 16 '13 05:12 prologic

sorry what would be painful, create .feature files or call scenario() function in a test file?

On 16 December 2013 06:09, James Mills [email protected] wrote:

It would be nice if py.test_bdd provided a cli tool to automatically generate the necessary structures from .feature file(s). I think I would find it painful to have to reproduce the contents of .feature files in my tests.

— Reply to this email directly or view it on GitHubhttps://github.com/olegpidsadnyi/pytest-bdd/issues/32 .

Anatoly Bubenkov

bubenkoff avatar Dec 16 '13 07:12 bubenkoff

Using the fixtures and re-specifying parts of the scenario.

Sent from my iPad

On 16 Dec 2013, at 17:58, Anatoly Bubenkov [email protected] wrote:

sorry what would be painful, create .feature files or call scenario() function in a test file?

On 16 December 2013 06:09, James Mills [email protected] wrote:

It would be nice if py.test_bdd provided a cli tool to automatically generate the necessary structures from .feature file(s). I think I would find it painful to have to reproduce the contents of .feature files in my tests.

— Reply to this email directly or view it on GitHub< https://github.com/olegpidsadnyi/pytest-bdd/issues/32> .

Anatoly Bubenkov

— Reply to this email directly or view it on GitHubhttps://github.com/olegpidsadnyi/pytest-bdd/issues/32#issuecomment-30642084 .

prologic avatar Dec 16 '13 08:12 prologic

this could be implemented as current-folder-aware py.test command-line argument. So that when you're in certain folder, it will look for existing fixtures (and so keywords) already implemented and available, and will print the generated code for tests and for keywords to stdout and then exist immediately. Would you mind to start on this maybe?

bubenkoff avatar Dec 16 '13 08:12 bubenkoff

I'll do some investigation.

I only bring this up because all the docs I've seen for pytest_bdd lead me to believe that I (as a developer) have to re-produce parts of each scenario in my test modules.

It sort of makes having the .feature file(s) redundant?

James Mills / prologic

E: [email protected] W: prologic.shortcircuit.net.au

On Mon, Dec 16, 2013 at 6:37 PM, Anatoly Bubenkov [email protected]:

this could be implemented as current-folder-aware py.test command-line argument. So that when you're in certain folder, it will look for existing fixtures (and so keywords) already implemented and available, and will print the generated code for tests and for keywords to stdout. Would you mind to start on this maybe?

— Reply to this email directly or view it on GitHubhttps://github.com/olegpidsadnyi/pytest-bdd/issues/32#issuecomment-30643759 .

prologic avatar Dec 16 '13 08:12 prologic

you're actually right, but that's by-design the idea is that .feature file is the communication channel between 'product owner' and 'developer' ideally 'product owner' writes them, not 'developer'. And 'developer' just 'implements' the test. It's not redundancy in fact, it's 'implementation'.

On 16 December 2013 09:42, James Mills [email protected] wrote:

I'll do some investigation.

I only bring this up because all the docs I've seen for pytest_bdd lead me to believe that I (as a developer) have to re-produce parts of each scenario in my test modules.

It sort of makes having the .feature file(s) redundant?

James Mills / prologic

E: [email protected] W: prologic.shortcircuit.net.au

On Mon, Dec 16, 2013 at 6:37 PM, Anatoly Bubenkov [email protected]:

this could be implemented as current-folder-aware py.test command-line argument. So that when you're in certain folder, it will look for existing fixtures (and so keywords) already implemented and available, and will print the generated code for tests and for keywords to stdout. Would you mind to start on this maybe?

— Reply to this email directly or view it on GitHub< https://github.com/olegpidsadnyi/pytest-bdd/issues/32#issuecomment-30643759>

.

— Reply to this email directly or view it on GitHubhttps://github.com/olegpidsadnyi/pytest-bdd/issues/32#issuecomment-30643974 .

Anatoly Bubenkov

bubenkoff avatar Dec 16 '13 09:12 bubenkoff

also note that usually you have a library of keywords (fixtures) implemented, and you just re-use it most of the time.

On 16 December 2013 09:42, James Mills [email protected] wrote:

I'll do some investigation.

I only bring this up because all the docs I've seen for pytest_bdd lead me to believe that I (as a developer) have to re-produce parts of each scenario in my test modules.

It sort of makes having the .feature file(s) redundant?

James Mills / prologic

E: [email protected] W: prologic.shortcircuit.net.au

On Mon, Dec 16, 2013 at 6:37 PM, Anatoly Bubenkov [email protected]:

this could be implemented as current-folder-aware py.test command-line argument. So that when you're in certain folder, it will look for existing fixtures (and so keywords) already implemented and available, and will print the generated code for tests and for keywords to stdout. Would you mind to start on this maybe?

— Reply to this email directly or view it on GitHub< https://github.com/olegpidsadnyi/pytest-bdd/issues/32#issuecomment-30643759>

.

— Reply to this email directly or view it on GitHubhttps://github.com/olegpidsadnyi/pytest-bdd/issues/32#issuecomment-30643974 .

Anatoly Bubenkov

bubenkoff avatar Dec 16 '13 09:12 bubenkoff

I see. This issue might in fact be redundant in that case :)

I'm just wondering if there are ways in which we can improve going from .feature(s) to test_xyz.py pre-populated with the scenario in the .feature file(s) with appropriate code in place ready to start implementing.

James Mills / prologic

E: [email protected] W: prologic.shortcircuit.net.au

On Mon, Dec 16, 2013 at 7:14 PM, Anatoly Bubenkov [email protected]:

you're actually right, but that's by-design the idea is that .feature file is the communication channel between 'product owner' and 'developer' ideally 'product owner' writes them, not 'developer'. And 'developer' just 'implements' the test. It's not redundancy in fact, it's 'implementation'.

On 16 December 2013 09:42, James Mills [email protected] wrote:

I'll do some investigation.

I only bring this up because all the docs I've seen for pytest_bdd lead me to believe that I (as a developer) have to re-produce parts of each scenario in my test modules.

It sort of makes having the .feature file(s) redundant?

James Mills / prologic

E: [email protected] W: prologic.shortcircuit.net.au

On Mon, Dec 16, 2013 at 6:37 PM, Anatoly Bubenkov [email protected]:

this could be implemented as current-folder-aware py.test command-line argument. So that when you're in certain folder, it will look for existing fixtures (and so keywords) already implemented and available, and will print the generated code for tests and for keywords to stdout. Would you mind to start on this maybe?

— Reply to this email directly or view it on GitHub<

https://github.com/olegpidsadnyi/pytest-bdd/issues/32#issuecomment-30643759>

.

— Reply to this email directly or view it on GitHub< https://github.com/olegpidsadnyi/pytest-bdd/issues/32#issuecomment-30643974>

.

Anatoly Bubenkov

— Reply to this email directly or view it on GitHubhttps://github.com/olegpidsadnyi/pytest-bdd/issues/32#issuecomment-30645628 .

prologic avatar Dec 16 '13 09:12 prologic

'Collecting features' at runtime seems like an excellent feature to add. Big +1 from me! Currently, people are going to have to write their own collection algorithms or put the scenario() calls everywhere, which seems like it should be a possibility rather than the default, but I can appreciate the design mentioned as well.

PhoenixWright avatar Mar 12 '14 17:03 PhoenixWright

:+1: I think a test file generator is a great way to eliminate some repetition here. Probably as a separate binary, like "make_bdd_test" or something.

e.g. if I have

# features/eating_food.feature

Feature:
  As a hungry person
  I want to eat food
  So that I am full

  Scenario: Eating fruit
    Given I have a plate of fruit
    When I eat the fruit
    Then I should be full

  Scenario: Eating bread
    Given I have a plate of bread
    When I eat the bread
    Then I should be full

Then do something like:

$ make_bdd_test features/eating_food.feature

To get a generated file similar to:

# tests/functional/test_eating_food.py

import pytest_bdd import scenario, given, when, then


@scenario('eating_food.feature', 'Eating fruit')
def test_eating_fruit():
    pass


@scenario('eating_food.feature', 'Eating bread')
def test_eating_bread():
    pass


@given('I have a plate of fruit')
def i_have_a_plate_of_fruit():
    pass


@given('I have a plate of bread')
def i_have_a_plate_of_bread():
    pass


@when('I eat the fruit')
def i_eat_the_fruit():
    pass


@when('I eat the bread')
def i_eat_the_bread():
    pass


@then('I should be full')
def i_should_be_full():
    pass

dustinfarris avatar Aug 08 '14 00:08 dustinfarris

I like the idea but not the name :) How about: bdd generate </path/to/feature>

James Mills / prologic

E: [email protected] W: prologic.shortcircuit.net.au

On Fri, Aug 8, 2014 at 10:52 AM, Dustin Farris [email protected] wrote:

[image: :+1:] I think a test file generator is a great way to eliminate some repetition here. Probably as a separate binary, like "make_bdd_test" or something.

e.g. if I have

features/eating_food.feature

Feature: As a hungry person I want to eat food So that I am full Scenario: Eating fruit Given I have a plate of fruit When I eat the fruit Then I should be full Scenario: Eating bread Given I have a plate of bread When I eat the bread Then I should be full

Then do something like:

$ make_bdd_test features/eating_food.feature

To get a generated file similar to:

tests/functional/test_eating_food.py

import pytest_bdd import scenario, given, when, then

@scenario('eating_food.feature', 'Eating fruit')def test_eating_fruit(): pass

@scenario('eating_food.feature', 'Eating bread')def test_eating_bread(): pass

@given('I have a plate of fruit')def i_have_a_plate_of_fruit(): pass

@given('I have a plate of bread')def i_have_a_plate_of_bread(): pass

@when('I eat the fruit')def i_eat_the_fruit(): pass

@when('I eat the bread')def i_eat_the_bread(): pass

@then('I should be full')def i_should_be_full(): pass

— Reply to this email directly or view it on GitHub https://github.com/olegpidsadnyi/pytest-bdd/issues/32#issuecomment-51551772 .

prologic avatar Aug 08 '14 01:08 prologic

@therealprologic, can you please check the proposed solution, if it helps https://github.com/bubenkoff/pytest-bdd/tree/code-generation this branch https://github.com/bubenkoff/pytest-bdd/tree/code-generation#test-code-generation-helper

bubenkoff avatar Sep 18 '14 18:09 bubenkoff

Hey @therealprologic, so that PR was merged and now there's also more advanced way of generating the code: https://github.com/olegpidsadnyi/pytest-bdd#advanced-code-generation would be nice to hear some feedback from you

bubenkoff avatar Sep 23 '14 10:09 bubenkoff