Skip to content

Commit

Permalink
Merge pull request #3545 from adamatan/master
Browse files Browse the repository at this point in the history
Add a basic Python example (fixes #3544)
  • Loading branch information
Zac-HD committed Jan 21, 2023
2 parents ee56faf + 149d590 commit a515963
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ their individual contributions.
* `Aaron Meurer <https://github.com/asmeurer>`_
* `Adam Johnson <https://github.com/adamchainz>`_
* `Adam Matan <https://github.com/adamatan/adamatan>_`
* `Adam Sven Johnson <https://www.github.com/pkqk>`_
* `Afrida Tabassum <https://github.com/oxfordhalfblood>`_ (afrida@gmail.com)
* `Afonso Silva <https://github.com/ajcerejeira>`_ (ajcerejeira@gmail.com)
Expand Down
34 changes: 34 additions & 0 deletions hypothesis-python/examples/test_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

from hypothesis import given, strategies as st


# Our tested class
class Product:
def __init__(self, price: float) -> None:
self.price: float = price

def get_discount_price(self, discount_percentage: float):
return self.price * (discount_percentage / 100)


# The @given decorater generates examples for us!
@given(
price=st.floats(min_value=0, allow_nan=False, allow_infinity=False),
discount_percentage=st.floats(
min_value=0, max_value=100, allow_nan=False, allow_infinity=False
),
)
def test_a_discounted_price_is_not_higher_than_the_original_price(
price, discount_percentage
):
product = Product(price)
assert product.get_discount_price(discount_percentage) <= product.price

0 comments on commit a515963

Please sign in to comment.