Skip to content

Instantly share code, notes, and snippets.

@massifaqiri
Created July 25, 2020 22:46
Show Gist options
  • Save massifaqiri/d4131a3953cdb07823cac44ee7dcc659 to your computer and use it in GitHub Desktop.
Save massifaqiri/d4131a3953cdb07823cac44ee7dcc659 to your computer and use it in GitHub Desktop.
import pytest
from funcs import capital_case
from funcs import Wallet
#Simple Assertion
def test_capital_case():
assert capital_case("holy") == "Holy"
#Raise an error if our function is not handling an exception of type. It's more like a test of error handling
def test_raises_exception_on_non_string_arguments():
with pytest.raises(TypeError):
capital_case(9)
#Testing a class and its instance/method
def test_initial_balance():
mywallet = Wallet()
assert mywallet.init_balance == 0
#Testing a class and its instance/method
def test_deposit():
mywallet = Wallet()
mywallet.deposit(10)
assert mywallet.init_balance == 10
#Pytest Fixture: it saves repetitions by creating a fixture that could be called over and over, as following (as param & instance):
@pytest.fixture
def wallet():
return Wallet()
wallet.deposit(10)
wallet.spend(5)
assert wallet.init_balance == 5
#Parameterized Test Functions/Decorators: passing several data sets for testing, as a decorator:
@pytest.mark.parametrize("earned, spent, expected", [(30, 10, 20), (20, 2, 18), (15, 5, 10),])
def test_transactions(earned, spent, expected):
my_wallet = Wallet()
my_wallet.deposit(earned)
my_wallet.spend(spent)
assert my_wallet.init_balance == expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment