Your SlideShare is downloading. ×
Django/Python Unit Testing talk for Django District
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Django/Python Unit Testing talk for Django District


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. …

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.

Published in: Technology, Business

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Fixtures and @ekivemark A Newbie voyage in to Django UnitTesting @ekivemark
  • 2. New to Django
  • 3. import django.expertsAUDIENCE = “Technical”if not(developer)==Trueand techie(dangerous): bad_idea = give_talk(AUDIENCE)
  • 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. Make it readablepython 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. Localize variables • 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}/ 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=""SMSCODE_FOR_TEST=9999VALID_LASTNAME="First"VALID_FIRSTNAME="Arthur" @ekivemarkUSERNAME_NOT_TEST=nottesterPASSWORD_NOT_TEST=passwordSMSCODE_NOT_TEST=9999
  • 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. 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("") print " Test Runtime: "+str( if answer == None: print " Test Harness ready" else: print " This Test Harness has a problem" Test_End("") return @ekivemark
  • 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 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. ? @ekivemark @ekivemark