Giter VIP home page Giter VIP logo

pytest-bdd-example's Introduction

pytest-bdd Cheat Sheet

DOI

Recommendations

Before using this cheat sheet:

  • Learn the following about Gherkin:
    • Feature, Scenario
    • Given, When, Then
    • Scenario Outline, Examples
    • Tags
    • Comments
  • For people without any Python experience, learn basic syntax of Python such as:
    • indentation
    • commenting
    • declarition of variables and functions
  • Read through the official document: pytest-bdd.pdf .

Setup

1. Install python environment
2. Install pip
  1. Download get-pip.py
  2. Run following command:
python get-pip.py
  1. Upgrade pip
pip install -U pip
3. Instal pytest
pip install -U pytest
4. Install pytest-bdd
pip install pytest-bdd
5. Install selenium
pip install selenium
6. Download a webdriver

Write feature files

• Template
# This is a comment
@aTag @anotherTag @etc...
Feature: some description ...
  Scenario: some description ...
    Given ...
    When ...
    Then ...
    And ...
• Scenario Outline
#pageTitle.feature
Feature: Check the page title
  Scenario Outline: Check page title for XX page
    Given I am on the zoo website
    When I navigate to <xx_link>
    Then I check page title is "<xx>"
    And I close the browser
    Examples:
    |xx_link        |xx       |
    |adoption_link  |Adoption |
    |about_link     |About    |
    |contact_link   |Contact  |
  • Vertical example table
Scenario Outline: Outlined given, when, thens 
  Given there are <start> cucumbers
  When I eat <eat> cucumbers
  Then I should have <left> cucumbers
  Examples: Vertical
  | start | 12 | 2  | 
  | eat   | 5  | 1  |  
  | left  | 7  | 1  |

Generate test files

NOTICE:

  • A test file and its corresponding feature file MUST stay in the same dirctory
  • The name of a test file MUST be in the form of test_*.py or *_test.py
  • According to the previous sentence, And statement will be autometically modified to given, when, or then statement in the test file. • Create fully functional but of course empty tests and step definitions for given a feature file.
pytest-bdd generate someFeature.feature

• Redirect the generated code to a test file.

pytest-bdd generate pageTitle.feature > test_some_feature.py

• Advanced code generation

py.test --generate-missing --feature features tests/functional

Modify test files

• import
from pytest_bdd import (given, scenario, then, when)
import pytest
from selenium import webdriver
• Fixture for driver setup

Allow the browser automaticlly run before executing tests and quit after all the tests finished.

@pytest.fixture(autouse=True, scope='function')
def setup(request):
    global driver
    driver = webdriver.Chrome("/Users/yuxuan.zhao/chromedriver")
    def fin():
        driver.quit()
        request.addfinalizer(fin)
• Step definition
  • Step aliases
@given('I am on the zoo website')
@given('I am on the homepage of zoo website')
def i_am_on_the_zoo_website():
    """I am on the zoo website."""
    driver.get("http://www.thetestroom.com/webapp/")
  • Step arguments
@when('I navigate to <xx_link>')
def i_navigate_to_xx_link(xx_link):
    """I navigate to <xx_link>."""
    driver.find_element_by_id(xx_link).click()
  • Driver usage
    • Navigate to a page given by the URL
driver.get("http://www.python.org")  
  • An assertion to confirm that title has “Python” word in it
assert "Python" in driver.title
  • Find an element
elem = driver.find_element_by_name("q")
# find_element_by_id
# find_element_by_name
# find_element_by_link_text
# find_element_by_partial_link_text
# etc..
  • Clear filds
elem.clear()
  • Send keys
from selenium.webdriver.common.keys import Keys
elem.send_keys("pycon")

See more information in the official document: selenium-python.pdf

Run the tests

  • Run all the tests
pytest
  • Run a specified file
pytest test_something.py
  • Run tests with sepcific tags
pytest -k "Tag1 and Tag2 and Tag3"
  • Run tests without a certain tag
pytest -k "not Tag1"

Report

To have an output in json format:

py.test --cucumberjson=<path to json report>

Example

https://github.com/VirtaKuivapuu/pytest-bdd-example

pytest-bdd-example's People

Contributors

credoquiaabsurdum avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pytest-bdd-example's Issues

Error - Launch configuration ContactConfirm.feature references non-existing project pytest-bdd-example-master.

Hi,
I downloaded the python project and imported into Eclipse. i have installed Pydev and Cucumber(to see the feature file contents in gherkin format). when i right click in any of the feature files and run as "cucumber file", i get the below error mesage -
"Launch configuration ContactConfirm.feature references non-existing project pytest-bdd-example-master."

Also it shows the step doesn't have matching glue code. I have tried all steps but nothing is working.

Any help here will be really appreciated.

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.