Skip to content

Instantly share code, notes, and snippets.

@dmytrostriletskyi
Last active June 12, 2023 16:00
Show Gist options
  • Save dmytrostriletskyi/861f5ac04263f133f26c3cafcefa57ee to your computer and use it in GitHub Desktop.
Save dmytrostriletskyi/861f5ac04263f133f26c3cafcefa57ee to your computer and use it in GitHub Desktop.
# That is how the tests might look in pytest/unittest.
# Basically each case is written by own test function.
# Each function has a documentation describing the test case.
# Basically, a lot of stuff is repeated:
# - name variable
# - surname variable
# - documentation (at least, cases)
# - asserts sometimes or most of the time
# More cases we have (usually we have a lot), more duplication thing the test class has
# as well as the size, etc.
class TestUserService:
def test_create():
"""
Case: create a user.
Expect: the user is created.
"""
name, surname = "Dmytro", "Striletskyi"
created_user = UserService.create(name=name, surname=surname)
assert name = created_user.name
assert surname = created_user.surname
def test_create_admin():
"""
Case: create a user.
When: it is an admin.
Expect: the admin user is created with read-write privileges.
"""
name, surname = "Dmytro", "Striletskyi"
created_user = UserService.create(name=name, surname=surname)
make_user_admin(created_user)
assert is_admin == created_user.is_admin
assert UserPreveligies.READ_WRITE_ACCESS == created_user.preveligies
# Folling Ruby RSpec style it might be rewritten in such way:
test UserService
case 'Create a user':
name, surname = "Dmytro", "Striletskyi"
created_user = UserService.create(name=name, surname=surname)
expect 'the user is created':
assert UserModel.last_object()
expect 'the user name equals one that was set':
assert name = created_user.name
expect 'the user surname equals one that was set':
assert surname = created_user.surname
case 'when it is an admin':
make_user_admin(created_user)
expect 'the user is admin':
assert is_admin == created_user.is_admin
expect 'the user has read write preveligies':
assert UserPreveligies.READ_WRITE_ACCESS == created_user.preveligies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment