Added the first basic test

This commit is contained in:
Pandy Knight 2023-07-19 23:20:18 -04:00
parent 8f2cb4c3a6
commit 0ec628ccbf
4 changed files with 78 additions and 0 deletions

45
cucumbers.py Normal file
View File

@ -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

View File

@ -0,0 +1,10 @@
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

View File

View File

@ -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