Skip to content

Pytest BDD [work in progress] #13

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions PytestBDD/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
__pycache__/
13 changes: 13 additions & 0 deletions PytestBDD/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pytest = "*"
pytest-bdd = "*"

[dev-packages]

[requires]
python_version = "3.9"
203 changes: 203 additions & 0 deletions PytestBDD/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions PytestBDD/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run

45 changes: 45 additions & 0 deletions PytestBDD/cucumbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
This module contains a simple class modeling a cucumber basket.
Cucumbers may be added or removed from the basket.
The basket has a maximum size, however.
"""


class CucumberBasket:

def __init__(self, initial_count=0, max_count=10):
if initial_count < 0:
raise ValueError("Initial cucumber basket count must not be negative")
if max_count < 0:
raise ValueError("Max cucumber basket count must not be negative")

self._count = initial_count
self._max_count = max_count

@property
def count(self):
return self._count

@property
def full(self):
return self.count == self.max_count

@property
def empty(self):
return self.count == 0

@property
def max_count(self):
return self._max_count

def add(self, count=1):
new_count = self.count + count
if new_count > self.max_count:
raise ValueError("Attempted to add too many cucumbers")
self._count = new_count

def remove(self, count=1):
new_count = self.count - count
if new_count < 0:
raise ValueError("Attempted to remove too many cucumbers")
self._count = new_count
16 changes: 16 additions & 0 deletions PytestBDD/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
9 changes: 9 additions & 0 deletions PytestBDD/tests/features/cucumbers.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: Cucumber Basket
As a gardener,
I want to carry cucumbers in a basket,
So that I don't drop them all.

Scenario: Add cucumbers to a basket
Given the basket has 2 cucumbers
When 4 cucumbers are added to the basket
Then the basket contains 6 cucumbers
23 changes: 23 additions & 0 deletions PytestBDD/tests/step_defs/test_cucumbers_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pytest_bdd import scenario, given, when, then

from cucumbers import CucumberBasket


@scenario('../features/cucumbers.feature', 'Add cucumbers to a basket')
def test_add():
pass


@given("the basket has 2 cucumbers", target_fixture='basket')
def basket():
return CucumberBasket(initial_count=2)


@when("4 cucumbers are added to the basket")
def add_cucumbers(basket):
basket.add(4)


@then("the basket contains 6 cucumbers")
def basket_has_total(basket):
assert basket.count == 6