I have started a hobby project and would like to integrate better and more thorough testing practices. Since I am not too used to it, I'd love to have some feedback on the sample code below. In particular, I'd be interested in the best practices surrounding setting up data necessary to feed a particular testing function or set of functions. As it probably matters, I intend to use pytest
.
Here is the sample code from the first testing module in my repo so far:
from accounts import Account
from accounts import AccountRollup
from accounts import AccountType
from accounts import AmountType
from accounts import JournalEntry
from accounts import JournalEntryLeg
# Setup chart of accounts
cash_account = Account(1, "Cash", AccountType.Asset)
mortgage_account = Account(2, "Mortgage", AccountType.Liability)
revenue_account = Account(3, "Revenues", AccountType.Income)
balance_sheet = AccountRollup([cash_account, mortgage_account])
income_statement = AccountRollup(revenue_account)
chart_of_accounts = AccountRollup([balance_sheet, income_statement])
def test_journal_entry_balance():
# Journal entry is balanced if Debit == Credit
jnl = JournalEntry(
[
JournalEntryLeg(cash_account, 50, AmountType.Debit),
JournalEntryLeg(cash_account, 50, AmountType.Debit),
JournalEntryLeg(mortgage_account, 100, AmountType.Credit),
]
)
assert jnl.is_balanced()
# Journal entry is not balanced if Debit != Credit
jnl = JournalEntry(
[
JournalEntryLeg(cash_account, 100, AmountType.Debit),
JournalEntryLeg(mortgage_account, 50, AmountType.Credit),
]
)
assert not jnl.is_balanced()
# Journal entry is not balanced even if posting negative amounts
jnl = JournalEntry(
[
JournalEntryLeg(cash_account, 100, AmountType.Debit),
JournalEntryLeg(mortgage_account, -100, AmountType.Debit),
]
)
assert not jnl.is_balanced()
# Test floating-point precision (This test should fail, just testing pytest)
jnl = JournalEntry(
[
JournalEntryLeg(cash_account, 100, AmountType.Debit),
JournalEntryLeg(mortgage_account, 99.9, AmountType.Credit),
]
)
assert jnl.is_balanced()
If I had to guess areas of improvement:
- I find the code very repetitive and verbose. It could probably be worthwhile grouping variable assignments and assertions together instead of separated by test case.
- Set variable assignments in global variables doesn't look very too clean.