Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Fixtures and Fittings...mark@ekivemark.com                          @ekivemark        A Newbie voyage in to Django UnitTes...
New to Django
import django.expertsAUDIENCE = “Technical”if not(developer)==Trueand techie(dangerous):    bad_idea = give_talk(AUDIENCE)
Why Fixtures?fixtures =   [apps/services/fixtures/services_testdata.json,             #apps/accounts/fixtures/accounts_tes...
Make it readablepython manage.py dumpdata accounts --indent=4 >./apps/accounts/fixtures/testdata.json[    {        "pk": 1...
Localize variables  • settings_test.py            a bunch of frequently used test variables__author__ = mark @ekivemark"""...
Build a DRY toolset        • Printing results to the screendef Test_Start(message=""):    print "_________________________...
Time and Date stamp                      your tests# @unittest.skipclass Accounts_Simple_TestCase(TestCase):    """Backgro...
User permissionsdef check_permission(usr_name="",showprint=False):    """    View permissions for a user profile    """   ...
?mark@ekivemark.com       @ekivemark                         @ekivemark
Upcoming SlideShare
Loading in …5
×

Django/Python Unit Testing talk for Django District

1,647 views

Published on

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

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Django/Python Unit Testing talk for Django District

  1. 1. Fixtures and Fittings...mark@ekivemark.com @ekivemark A Newbie voyage in to Django UnitTesting @ekivemark
  2. 2. New to Django
  3. 3. import django.expertsAUDIENCE = “Technical”if not(developer)==Trueand techie(dangerous): bad_idea = give_talk(AUDIENCE)
  4. 4. Why Fixtures?fixtures = [apps/services/fixtures/services_testdata.json, #apps/accounts/fixtures/accounts_test_data.json, apps/coupons/fixtures/accounts_test_data.json, apps/intake/fixtures/intake_inprocess.json] • Compare screen data entry to UnitTest • Testing pagination • Testing Search @ekivemark
  5. 5. Make it readablepython manage.py dumpdata accounts --indent=4 >./apps/accounts/fixtures/testdata.json[ { "pk": 1, "model": "accounts.userprofile", "fields": { "mobile_phone_number": "7036232789", "vid": "820912028666724", "pin": 4829, "gender": "", "fax_number": "", "anonymous_patient_id": "76766c7d-25eb-4118-9a4c-b939c80929ce", "user": 2, "secondary_insurance_or_payer": "", "organization": "", "primary_insurance_or_payer": "" } }] @ekivemark
  6. 6. Localize variables • settings_test.py a bunch of frequently used test variables__author__ = mark @ekivemark"""Unit Test Framework Standard Variables used across modulesAdd{from ..test.settings_test import * }to top of {app name}/tests.py in order to be able to use variables without the{settings_test.} prefixThis 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 IveUSERNAME_FOR_TEST=markPASSWORD_FOR_TEST=123456789EMAIL_FOR_TEST="spam1@nospam.org"SMSCODE_FOR_TEST=9999VALID_LASTNAME="First"VALID_FIRSTNAME="Arthur" @ekivemarkUSERNAME_NOT_TEST=nottesterPASSWORD_NOT_TEST=passwordSMSCODE_NOT_TEST=9999
  7. 7. Build a DRY toolset • Printing results to the screendef 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. 8. Time and Date stamp your tests# @unittest.skipclass 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. 9. User permissionsdef 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
  10. 10. ?mark@ekivemark.com @ekivemark @ekivemark

×