| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
from hypothesis import given, strategies as st |
| 12 |
|
| 13 |
|
| 14 |
# Our tested class |
| 15 |
class Product: |
| 16 |
def __init__(self, price: float) -> None: |
| 17 |
self.price: float = price |
| 18 |
|
| 19 |
def get_discount_price(self, discount_percentage: float): |
| 20 |
return self.price * (discount_percentage / 100) |
| 21 |
|
| 22 |
|
| 23 |
# The @given decorator generates examples for us! |
| 24 |
@given( |
| 25 |
price=st.floats(min_value=0, allow_nan=False, allow_infinity=False), |
| 26 |
discount_percentage=st.floats( |
| 27 |
min_value=0, max_value=100, allow_nan=False, allow_infinity=False |
| 28 |
), |
| 29 |
) |
| 30 |
def test_a_discounted_price_is_not_higher_than_the_original_price( |
| 31 |
price, discount_percentage |
| 32 |
): |
| 33 |
product = Product(price) |
| 34 |
assert product.get_discount_price(discount_percentage) <= product.price |