I have a question regarding writing unit tests in processes consisting of multiple steps.
Suppose for this example we have the following scenario: there is a website with users who have accounts of the website. In order to have less user data the website has a process in place for the removal of inactive accounts. If an account is inactive for a year (for example a user has not logged into its account) the account is removed. Four weeks before the account is removed the user is send an email to tell him the account will be removed. Two weeks before removal a second email is send to the user if the account is still inactive. This email will only be send if the first email is also send. Then if the user still hasn't used the account the account is removed and a final email will be send to the user. This email will only be send if the first two emails were send.
Suppose we need to write teststo check if the emails will be send if the conditions are met. One way of writing such a test can be (in pseudocode)
func test_happy_flow() {
user_account = setup_inactive_user_account()
date = date('four weeks before closure')
runClosureProcess(user_account, date)
assert(send_emails).contains(FIRST_EMAIL)
date = date('two weeks before closure')
runClosureProcess(user_account, date)
assert(send_emails).contains(SECOND_EMAIL)
date = date('closure date')
runClosureProcess(user_account, date)
assert(send_emails).contains(CLOSURE_EMAIL)
}
The pro of this method is that the flow of the order of emails is clear. The con is that multiple asserts are used and there are basically three different cases tested here.
One other way to do it is to use three different tests:
func test_if_first_email_will_be_send() {
user_account = setup_inactive_user_account()
date = date('four weeks before closure')
runClosureProcess(user_account, date)
assert(send_emails).contains(FIRST_EMAIL)
}
func test_if_second_email_will_be_send() {
user_account = setup_inactive_user_account()
date = date('four weeks before closure')
runClosureProcess(user_account, date)
date = date('two weeks before closure')
runClosureProcess(user_account, date)
assert(send_emails).contains(SECOND_EMAIL)
}
func test_if_closure_email_will_be_send() {
user_account = setup_inactive_user_account()
date = date('four weeks before closure')
runClosureProcess(user_account, date)
date = date('two weeks before closure')
runClosureProcess(user_account, date)
date = date('closure date')
runClosureProcess(user_account, date)
assert(send_emails).contains(CLOSURE_EMAIL)
}
This has more code and duplication but each scenario is tested individually.
Are there any patterns or 'standard' ways of testing these kinds of flows? Or will the preferred way depend on personal and team preferences?
Aucun commentaire:
Enregistrer un commentaire