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

Django/Python Unit Testing talk for Django District

on

  • 1,239 views

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.

http://www.django-district.org/events/58149912/?eventId=58149912&action=detail

Statistics

Views

Total Views
1,239
Views on SlideShare
1,170
Embed Views
69

Actions

Likes
0
Downloads
10
Comments
0

8 Embeds 69

http://ekive.blogspot.com 44
http://ekivemark.posterous.com 8
http://ratherbeskiing.blogspot.com 6
http://ekivemark.tumblr.com 6
http://ekive.blogspot.in 2
http://www.ekive.blogspot.com 1
http://ekive.blogspot.com.es 1
http://ekive.blogspot.sg 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Django/Python Unit Testing talk for Django District Django/Python Unit Testing talk for Django District Presentation Transcript

  • Fixtures and Fittings...mark@ekivemark.com @ekivemark A Newbie voyage in to Django UnitTesting @ekivemark
  • 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_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
  • 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
  • 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
  • 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
  • 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
  • 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
  • ?mark@ekivemark.com @ekivemark @ekivemark