pytest-bdd/README.rst

244 lines
6.2 KiB
ReStructuredText
Raw Normal View History

2013-03-31 09:42:42 +08:00
BDD library for the py.test runner
2013-06-16 22:15:28 +08:00
==================================
2013-03-29 15:27:44 +08:00
.. |Build Status| image:: https://api.travis-ci.org/olegpidsadnyi/pytest-bdd.png
:target: https://travis-ci.org/olegpidsadnyi/pytest-bdd
.. |Pypi| image:: https://pypip.in/v/pytest-bdd/badge.png
:target: https://crate.io/packages/pytest-bdd/
.. |Coverrals| image:: https://coveralls.io/repos/olegpidsadnyi/pytest-bdd/badge.png?branch=master
:target: https://coveralls.io/r/olegpidsadnyi/pytest-bdd
2013-03-31 09:03:40 +08:00
2013-05-28 22:41:45 +08:00
2013-03-31 16:40:12 +08:00
Install pytest-bdd
==================
2013-03-31 09:42:42 +08:00
::
2013-03-31 09:42:42 +08:00
pip install pytest-bdd
2013-03-31 09:42:42 +08:00
Example
=======
publish\_article.feature:
::
2013-03-31 09:03:40 +08:00
Scenario: Publishing the article
Given I'm an author user
And I have an article
When I go to the article page
And I press the publish button
Then I should not see the error message
And the article should be published # Note: will query the database
2013-03-31 09:42:42 +08:00
test\_publish\_article.py:
2013-03-31 09:42:42 +08:00
::
2013-03-31 09:42:42 +08:00
from pytest_bdd import scenario, given, when, then
2013-03-31 09:42:42 +08:00
test_publish = scenario('publish_article.feature', 'Publishing the article')
2013-03-31 09:42:42 +08:00
@given('I have an article')
def article(author):
return create_test_article(author=author)
2013-03-31 09:42:42 +08:00
@when('I go to the article page')
def go_to_article(article, browser):
browser.visit(urljoin(browser.url, '/manage/articles/{0}/'.format(article.id)))
2013-03-31 09:42:42 +08:00
2013-04-07 23:30:09 +08:00
@when('I press the publish button')
def publish_article(browser):
browser.find_by_css('button[name=publish]').first.click()
2013-03-31 09:42:42 +08:00
2013-04-07 23:30:09 +08:00
@then('I should not see the error message')
def no_error_message(browser):
with pytest.raises(ElementDoesNotExist):
browser.find_by_css('.message.error').first
2013-03-31 09:42:42 +08:00
@then('And the article should be published')
def article_is_published(article):
article.refresh() # Refresh the object in the SQLAlchemy session
assert article.is_published
2013-04-03 05:47:44 +08:00
2013-04-09 05:30:23 +08:00
Step aliases
============
Sometimes it is needed to declare the same fixtures or steps with the
different names for better readability. In order to use the same step
function with multiple step names simply decorate it multiple times:
2013-04-09 05:30:23 +08:00
::
2013-04-09 05:30:23 +08:00
@given('I have an article')
@given('there\'s an article')
def article(author):
return create_test_article(author=author)
2013-04-09 05:30:23 +08:00
Note that the given step aliases are independent and will be executed
when mentioned.
For example if you assoicate your resource to some owner or not. Admin
user cant be an author of the article, but article should have some
default author.
2013-04-09 05:30:23 +08:00
::
2013-04-09 05:30:23 +08:00
Scenario: I'm the author
Given I'm an author
And I have an article
2013-04-09 05:30:23 +08:00
Scenario: I'm the admin
Given I'm the admin
And there is an article
2013-04-09 05:30:23 +08:00
Step parameters
===============
Sometimes it is hard to write good scenarios without duplicating most of
contents of existing scenario. For example if you create some object
with static param value, you might want to create another test with
different param value. By Gherkin specification its possible to have
parameters in steps: http://docs.behat.org/guides/1.gherkin.html
Example:
::
Scenario: Parametrized given, when, thens
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
As you can see we dont use Scenario Outline, but use just Scenario,
just because its simple to implement for pytest.
The code will look like:
::
# here we use pytest power to parametrize test
@pytest.mark.parametrize(
['start', 'eat', 'left'],
[(12, 5, 7)])
@scenario(
'parametrized.feature',
'Parametrized given, when, thens',
)
# note that we should receive same arguments in function that we use for test parametrization either directly
# or indirectly (throught fixtures)
def test_parametrized(start, eat, left):
"""We don't need to do anything here, everything will be managed by scenario decorator."""
@given('there are <start> cucumbers')
def start_cucumbers(start):
return dict(start=start)
@when('I eat <eat> cucumbers')
def eat_cucumbers(start_cucumbers, start, eat):
2013-08-12 07:01:37 +08:00
start_cucumbers['eat'] = eat
@then('I should have <left> cucumbers')
2013-08-12 07:01:37 +08:00
def should_have_left_cucumbers(start_cucumbers, start, eat, left):
assert start - eat == left
2013-08-12 07:01:37 +08:00
assert start_cucumbers['start'] == start
assert start_cucumbers['eat'] == eat
2013-04-12 06:37:10 +08:00
Reuse fixtures
==============
2013-04-10 07:00:27 +08:00
Sometimes scenarios define new names for the fixture that can be
inherited. Fixtures can be reused with other names using given():
2013-04-10 07:00:27 +08:00
::
2013-04-10 07:00:27 +08:00
given('I have beautiful article', fixture='article')
2013-04-09 05:30:23 +08:00
2013-04-12 06:37:10 +08:00
Reuse steps
===========
It is possible to define some common steps in the parent conftest.py and
simply expect them in the child test file.
common\_steps.feature:
2013-04-12 06:37:10 +08:00
::
2013-04-12 06:38:49 +08:00
Scenario: All steps are declared in the conftest
Given I have a bar
Then bar should have value "bar"
2013-04-12 06:37:10 +08:00
conftest.py:
2013-04-12 06:38:49 +08:00
::
from pytest_bdd import given, then
2013-04-12 06:37:10 +08:00
@given('I have a bar')
def bar():
return 'bar'
2013-04-12 06:37:10 +08:00
@then('bar should have value "bar"')
def bar_is_bar(bar):
assert bar == 'bar'
2013-04-12 06:37:10 +08:00
test\_common.py:
2013-04-12 06:37:10 +08:00
::
2013-04-12 06:38:49 +08:00
test_conftest = scenario('common_steps.feature', 'All steps are declared in the conftest')
2013-04-12 06:37:10 +08:00
There are no definitions of the steps in the test file. They were
collected from the parent conftests.
2013-05-28 22:41:45 +08:00
Feature file paths
==================
But default, pytest-bdd will use current modules path as base path for
finding feature files, but this behaviour can be changed by having
fixture named pytestbdd\_feature\_base\_dir which should return the
new base path.
test\_publish\_article.py:
2013-05-28 22:41:45 +08:00
::
2013-05-28 22:41:45 +08:00
import pytest
from pytest_bdd import scenario
@pytest.fixture
def pytestbdd_feature_base_dir():
2013-05-28 22:41:45 +08:00
return '/home/user/projects/foo.bar/features'
test_publish = scenario('publish_article.feature', 'Publishing the article')
2013-04-03 05:47:44 +08:00
Subplugins
==========
The pytest BDD has plugin support, and the main purpose of plugins
(subplugins) is to provide useful and specialized fixtures.
2013-04-03 05:47:44 +08:00
List of known subplugins:
::
2013-04-03 05:47:44 +08:00
* pytest-bdd-splinter -- collection of fixtures for real browser BDD testing
2013-04-03 05:47:44 +08:00
2013-06-15 04:24:42 +08:00
License
=======
This software is licensed under the `MIT license <http://en.wikipedia.org/wiki/MIT_License>`_.
2013-06-15 04:24:42 +08:00
© 2013 Oleg Pidsadnyi