Skip to content

Instantly share code, notes, and snippets.

@prcutler
Created September 17, 2018 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prcutler/cdc47e5d25389fa278e939af04f323f6 to your computer and use it in GitHub Desktop.
Save prcutler/cdc47e5d25389fa278e939af04f323f6 to your computer and use it in GitHub Desktop.
Account Unit Tests
import unittest
import unittest.mock
import pyramid.testing
class AccountControllerTests(unittest.TestCase):
def test_register_validation_valid(self):
# 3 A's of test: Arrange, Act, then Assert
# Arrange
from nflpool.viewmodels.register_viewmodel import RegisterViewModel
data = {
'first_name': 'Paul',
'last_name': 'Cutler',
'email': 'paul.r.cutler@gmail.com',
'password': 'Aa123456@',
'confirm_password': 'Aa123456@'
}
# noinspection PyTypeChecker
vm = RegisterViewModel()
vm.from_dict(data)
# Act
target = 'nflpool.services.account_service.find_account_by_email'
with unittest.mock.patch(target, return_value=None):
vm.validate()
# Assert:
self.assertIsNone(vm.error)
def test_register_validation_existing_user(self):
# Arrange
from nflpool.viewmodels.register_viewmodel import RegisterViewModel
from nflpool.data.account import Account
data = {
'email': 'paul.r.cutler@gmail.com',
}
# noinspection PyTypeChecker
vm = RegisterViewModel()
vm.from_dict(data)
# Act
target = 'nflpool.services.account_service.find_account_by_email'
with unittest.mock.patch(target, return_value=Account()):
vm.validate()
# Assert:
self.assertIsNotNone(vm.error)
self.assertTrue('exist' in vm.error)
@prcutler
Copy link
Author

prcutler commented Sep 17, 2018

Account Service: https://github.com/prcutler/nflpool/blob/master/nflpool/services/account_service.py

Error:

Testing started at 1:27 PM ...
/Users/pcutler/workspace/nflpool/.env/bin/python /Applications/PyCharm.app/Contents/helpers/pycharm/_jb_unittest_runner.py --path /Users/pcutler/workspace/nflpool/nflpool/tests/account_tests3.py
Launching unittests with arguments python -m unittest /Users/pcutler/workspace/nflpool/nflpool/tests/account_tests3.py in /Users/pcutler/workspace/nflpool/nflpool/tests

Error
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 605, in run
    testMethod()
  File "/Users/pcutler/workspace/nflpool/nflpool/tests/account_tests3.py", line 45, in test_register_validation_existing_user
    with unittest.mock.patch(target, return_value=Account()):
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py", line 1243, in __enter__
    original, local = self.get_original()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py", line 1217, in get_original
    "%s does not have the attribute %r" % (target, name)
AttributeError: <module 'nflpool.services.account_service' from '/Users/pcutler/workspace/nflpool/nflpool/services/account_service.py'> does not have the attribute 'find_account_by_email'


Error
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 605, in run
    testMethod()
  File "/Users/pcutler/workspace/nflpool/nflpool/tests/account_tests3.py", line 26, in test_register_validation_valid
    with unittest.mock.patch(target, return_value=None):
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py", line 1243, in __enter__
    original, local = self.get_original()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py", line 1217, in get_original
    "%s does not have the attribute %r" % (target, name)
AttributeError: <module 'nflpool.services.account_service' from '/Users/pcutler/workspace/nflpool/nflpool/services/account_service.py'> does not have the attribute 'find_account_by_email'



Ran 2 tests in 0.331s

FAILED (errors=2)

Process finished with exit code 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment