James,

Unit testing would be a good lightning talk.  Here are some notes
from the session where I recently trained my team on it.  You may
want to update some of the links.  We're still on Django 1.4.

    - Online docs:
      - http://bristle.com/~fred/#django
        - https://docs.djangoproject.com/en/1.4/topics/testing
        - https://docs.djangoproject.com/en/1.4/topics/testing/#assertions
        - Other links in my "Django" row
      - http://bristle.com/~fred/#python
        - https://docs.python.org/2/library/unittest.html
        - https://docs.python.org/2/library/unittest.html#test-cases
        - Other links in my "Python" row
    % python manage.py help test
      - Run all test methods in all test classes of all apps
    % python manage.py test -v2
    % python manage.py test -v2 appname...
      - Run all test methods in all test classes of specified apps
    % python manage.py test -v3 appname...
      - Shows ALL steps.  Too much detail for typical use
    % python manage.py test -v2 appname.TestClass...
      - Run all test methods in specified test classes
    % python manage.py test -v2 appname.TestClass.test_name...
      - Run specified test methods
    - Ctrl-C to exit gracefully, Ctrl-C again to abort
    - Create tests.py in each app
    - Inherit from django.test.TestCase
      - Or django.test.SimpleTestCase if no DB access needed
    - Runs all tests (inherited from TestCase) in tests.py and models.py
- If you need to prevent South from running, use SOUTH_TESTS_MIGRATE = False
    - To avoid running tests of South itself, use SKIP_SOUTH_TESTS = True
    - Creates/deletes "test_" DBs.  Does not touch real DBs.
    - Can detect in settings.py and elsewhere whether you're running tests,
and can then flip to SQLite for speed (30 seconds instead of 75 minutes):
      RUNNING_UNIT_TESTS = 'test' in sys.argv
      if RUNNING_UNIT_TESTS:
          DATABASES['default'] = {
              'ENGINE': 'django.db.backends.sqlite3',
              'NAME': os.path.join(SITE_ROOT, 'default.sqlite'),
          }
    - Django suppresses all outgoing email during tests, and collects it
      in a dictionary, so you can assert things about it
    - Python Assertions and methods:
      - assertEqual(a, b)
      - assertNotEqual(a, b)
      - assertTrue(x)
      - assertFalse(x)
      - assertIs(a, b)
      - assertIsNot(a, b)
      - assertIsNone(x)
      - assertIsNotNone(x)
      - assertIn(a, b)
      - assertNotIn(a, b)
      - assertIsInstance(a, b)
      - assertNotIsInstance(a, b)
      -
      - assertRaises(exc, fun, *args, **kwds)
      - assertRaisesRegexp(exc, r, fun, *args, **kwds)
      -
      - assertAlmostEqual(a, b)
      - assertNotAlmostEqual(a, b)
      - assertGreater(a, b)
      - assertGreaterEqual(a, b)
      - assertLess(a, b)
      - assertLessEqual(a, b)
      - assertRegexpMatches(s, r)
      - assertNotRegexpMatches(s, r)
      - assertItemsEqual(a, b)
      - assertDictContainsSubset(a, b)
      -
      - skipIf()
      - skipUnless()
      -
      - fail(msg=None)
      - failureException
      - longMessage
      - maxDiff
      -
      - countTestCases()
      - defaultTestResult()
      - id()
      - shortDescription()
      - addCleanup(function, *args, **kwargs)
      - doCleanups()
    - Django Assertions and methods:
      - SimpleTestCase.assertRaisesMessage()
      - SimpleTestCase.assertFieldOutput()
      - TestCase.assertContains()
      - TestCase.assertNotContains()
      - TestCase.assertFormError()
      - TestCase.assertTemplateUsed()
      - TestCase.assertTemplateNotUsed()
      - TestCase.assertRedirects()
      - TestCase.assertQuerysetEqual()
      - TestCase.assertNumQueries()
      - SimpleTestCase.assertHTMLEqual()
      - SimpleTestCase.assertHTMLNotEqual()
      -
      - skipIfDbFeature('supports_transactions')
      - skipUnlessDbFeature('supports_transactions')
    - Advanced:
      - setUp()/tearDown()
      - setUpClass()/tearDownClass()
      - setUpModule()/tearDownModule()
      - suite()
        - Can order your tests manually in a suite
        - Can invoke a suite from another suite
      - @unittest.skipIf/skipUnless()
      - Mock HTTP requests
- http://dougalmatthews.com/2010/Jan/20/testing-your-first-django-app/
        - Tells:
          - How to mock an HTTP request (easy!):
              from django.test import Client
              from mysite.polls.models import Poll, Choice
              class PollTest(TestCase):
                  def test_voting(self):
                      c = Client()
# Perform a vote on the poll by mocking a POST request.
                      response = c.post('/polls/1/vote/', {'choice': '1',})
                      # In the vote view we redirect the user, so check the
                      # response status code is 302.
self.assertEqual(response.status_code, 302)
                      # Get the choice and check there is now one vote.
                      choice = Choice.objects.get(pk=1)
                      self.assertEqual(choice.votes, 1)
            - No.  Per:
              - https://docs.djangoproject.com/en/1.4/topics/testing
                /#default-test-client
              it is even easier.  Just:
                  response = self.client.get('/customer/details/')
          - How to mock an Ajax HTTP request:

            def test_ajax_vote(self):
                c = Client()

                # Extra parameters to make this a Ajax style request.
                kwargs = {'HTTP_X_REQUESTED_WITH':'XMLHttpRequest'}

                # A valid vote
response = c.post('/polls/1/vote/', {'choice': '1',}, **kwargs)
                self.assertEqual(response.status_code, 200)
                self.assertEqual(response.content, '1')

                # A invalid vote - choice doesn't exist
response = c.post('/polls/1/vote/', {'choice': '10',}, **kwargs)
                self.assertEqual(response.status_code, 200)
                self.assertEqual(response.content, '0')

                # An invalid vote - poll doesn't exist
response = c.post('/polls/2/vote/', {'choice': '1',}, **kwargs)
                self.assertEqual(response.status_code, 404)

      - Selenium test cases with "LiveServerTestCase":
https://docs.djangoproject.com/en/1.4/topics/testing/#live-test-server
      - Python Nose
      - Django Nose
      - Coverage

Hope this helps!
--Fred
------------------------------------------------------------------------
Fred Stluka -- mailto:f...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.
------------------------------------------------------------------------
On 9/24/14 2:04 PM, James Brewer wrote:
I want to attend a Django BarCamp in SF next weekend and BarCamp tradition is that everyone contribute in some way.

Of course I will help out with set up/break down if that is needed, but I would also like to give a lightening talk. I'm fairly unfamiliar with Django at this point and I've never spoken publicly, so I am not sure what I would talk about.

If anyone has any ideas that would be accessible for someone who is relatively new to Django, I would love to hear them. It doesn't have to be something I already know as I have a week to familiarize myself, but given the short timeline it should be something accessible.

Happy hacking!

James
--
You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com <mailto:django-users+unsubscr...@googlegroups.com>. To post to this group, send email to django-users@googlegroups.com <mailto:django-users@googlegroups.com>.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fa483337-d783-46cb-b140-d15e986a2498%40googlegroups.com <https://groups.google.com/d/msgid/django-users/fa483337-d783-46cb-b140-d15e986a2498%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/54230EAC.90009%40bristle.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to