A short talk I am giving at the Python/Django Meetup on April 24th. I am no Django expert but I was asked to talk about my lessons learned in getting in to the UnitTesting framework for Python/Django.
http://www.django-district.org/events/58149912/?eventId=58149912&action=detail
6. Localize variables
• settings_test.py
a bunch of frequently used test variables
__author__ = 'mark @ekivemark'
"""
Unit Test Framework Standard Variables used across modules
Add
{from ..test.settings_test import * }
to top of {app name}/tests.py in order to be able to use variables without the
{settings_test.} prefix
This avoids setting variable values in multiple locations.
"""
# Edit this number to your cellphone number for testing purposes.
TESTER_CELL_NUMBER = "9999999999"
# User full name = Harvey Ive
USERNAME_FOR_TEST='mark'
PASSWORD_FOR_TEST='123456789'
EMAIL_FOR_TEST="spam1@nospam.org"
SMSCODE_FOR_TEST='9999'
VALID_LASTNAME="First"
VALID_FIRSTNAME="Arthur"
@ekivemark
USERNAME_NOT_TEST='nottester'
PASSWORD_NOT_TEST='password'
SMSCODE_NOT_TEST='9999'
7. Build a DRY toolset
• Printing results to the screen
def Test_Start(message=""):
print "_______________________________________________"
caller = inspect.getframeinfo(inspect.currentframe().f_back)[2]
print "Test Harness:" +
inspect.getframeinfo(inspect.currentframe().f_back)[0]
print term_color.OKGREEN + "Entering:" + caller +
term_color.ENDC
if message != "":
print "Notes:"+message
print "------------------------------------------------"
return
Highlighting helps
when quickly
scanning output @ekivemark
8. Time and Date stamp
your tests
# @unittest.skip
class Accounts_Simple_TestCase(TestCase):
"""Background to this test harness
and prove the test harness works
"""
# Add your fixtures here
# fixtures = ['testdata.json']
def test_basic_addition_Accounts(self):
"""
Tests that 1 + 1 always equals 2.
"""
Test_Start("1+1=2")
answer = self.assertEqual(1 + 1, 2)
Test_Msg("RESTCat.apps.accounts.tests.py")
print " Test Runtime: "+str(datetime.now())
if answer == None:
print " Test Harness ready"
else:
print " This Test Harness has a problem"
Test_End("RESTCat.apps.accounts.tests.py")
return
@ekivemark
9. User permissions
def check_permission(usr_name="",showprint=False):
"""
View permissions for a user profile
"""
if usr_name=="":def give_permission(usr_name="",permit_this="",showprint=False):
usr_key = 1 """
else: Add a permission to a user
try: """
usr_key = User.objects.get(username=usr_name)
except: if usr_name=="": def remove_permission(usr_name="",not_permitted="",showprint=False):
usr_key = 1 if showprint!=False: """
remove a permission from a user
print "No permission added"
usr_permission = Permission.objects.filter(user=usr_key)
result = "" """
else:
if showprint!=False: if usr_name=="":
usr_key = User.objects.get(username=usr_name)
print "User:" + usr_name
if showprint!=False: if showprint!=False:
print "key:" + str(usr_key) "got usr_key"
print print not_permitted+" permission not revoked"
print "Permissions:" print usr_key result = ""
print usr_permission else:
print "Now add permission:["+permit_this+"]"
usr_key = User.objects.get(username=usr_name)
if showprint!=False:
new_permission = Permission(user=usr_key, permission_name=permit_this)
return usr_permission new_permission.save() print "revoking ["+not_permitted+"] for "+usr_key
result = new_permission
kill_permission = Permission.objects.filter(user=usr_key,
return result permission_name=not_permitted)
if showprint!=False:
print kill_permission
kill_permission.delete()
result = "revoked ["+not_permitted+"] for "+str(usr_key)
return result
@ekivemark