Test using Pytest BDD

Test using Pytest BDD

 

Introduction

pytest-bdd implements a subset of the Gherkin language to enable automating project requirements testing and to facilitate behavioral driven development.

Unlike many other BDD tools, it does not require a separate runner and benefits from the power and flexibility of pytest. It enables unifying unit and functional tests, reduces the burden of continuous integration server configuration and allows the reuse of test setups.

 

Supported Pytest Version: 3.3.2 and above

Supported file types : JSON

Sample Test Result File

Click to download a sample file.

article.feature:

Feature: Blog A site where you can publish your articles. 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

article.py:

from pytest_bdd import scenario, given, when, then @scenario('publish_article.feature', 'Publishing the article') def test_publish(): pass @given("I'm an author user") def author_user(auth, author): auth['user'] = author.user @given('I have an article') def article(author): return create_test_article(author=author) @when('I go to the article page') def go_to_article(article, browser): browser.visit(urljoin(browser.url, '/manage/articles/{0}/'.format(article.id))) @when('I press the publish button') def publish_article(browser): browser.find_by_css('button[name=publish]').first.click() @then('I should not see the error message') def no_error_message(browser): with pytest.raises(ElementDoesNotExist): browser.find_by_css('.message.error').first @then('the article should be published') def article_is_published(article): article.refresh() # Refresh the object in the SQLAlchemy session assert article.is_published

The following command is necessary to generate the cucumber file in JSON format:

py.test --cucumberjson=<path_to_json_eport>

 

Example: py.test --cucumberjson=/user/path/result.json

 

Result File Output

 

 

Sample Project: https://pypi.org/project/pytest-bdd/