Git Product home page Git Product logo

test-driven-django-tutorial's Introduction

Update - this tutorial is out of date, and no longer being updated

This tutorial dates back to 2012, and was originally written for Django 1.3. It is totally out of date now, and I'm not maintaining it. If you're looking for (what I think is) a better introduction to TDD and Django, please check out

http://www.obeythetestinggoat.com

The site for my book, which contains everything that used to be in here, and much, much more, and is available entirely free (but also for money if you like)

For hysterical purposes, here is some of the old blurb.

The Concept

This idea was to provide an introduction to Test-Driven web development using Django (and Python). Essentially, we run through the same material as the official Django tutorial, but instead of 'just' writing code, we write tests first at each stage - both "functional tests", in which we actually pretend to be a user, and drive a real web browser, as well as "unit tests", which help us to design and piece together the individual working parts of the code.

The tutorial uses the new release of Django (1.4), and covers 95% of what's covered in the official Django tutorial. Suggestions, comments and feedback are gratefully received... What should I do next??

Who is this for?

Maybe you've done a bit of Python programming, and you're thinking of learning Django, and you want to do it "properly". Maybe you've done some test-driven web development in another language, and you want to find out about how it all works in the Python world. Most importantly, you've heard of, or had experience of, working on a project where complexity has started to get the better of you, where you're scared to make changes, and you wished there had been better testing from the get-go.

Who is this not for?

If you know Python, Django and Selenium inside out, I suspect there's better things that you can do with your time. If you're a total beginner programmer, I also think it might not be quite right for you - you might do better to get a couple of other tutorials under your belt first. If you're already a programmer, but have never tried Python, you'll be fine, but I thoroughly recommend the excellent "Dive into Python" for a bit more of an insight into the language itself.

Why should you listen to me?

I was lucky enough to get my first "proper" software development job about a year ago with a bunch of Extreme Programming fanatics, who've thoroughly inculcated me into their cult of Test-Driven development. Believe me when I say I'm contrary enough to have questioned every single practice, challenged every single decision, moaned about every extra minute spent doing "pointless" tests instead of writing "proper" code. But I've come round to the idea now, and whenever I've had to go back to some of my old projects which don't have tests, boy have I ever realised the wisdom of the approach.

So, I've learnt from some really good people, and the learning process is still fresh in my mind, so I hope I'll be good at communicating it. Most importantly, I still have the passion of a recent convert, so I hope I'll be good at conveying some enthusiasm.

Why Test-Driven Development?

The thing is, when you start out on a small project, you don't really need tests. Tests take time to write - as much as, if not more than, the actual code for your application. You've got to learn testing frameworks, and they inevitably come with a whole host of their own problems (and this applies especially to web-browser testing. oh boy.). Meanwhile, you know you could just knock out a few lines of code, and your application would be off the ground, and would start to be useful. There are deadlines! Clients who are paying for your time! Or maybe just the smell of that Internet money, and arriving late to the party means none of it will be for you!

Well, that's all true. At first. At first, it's obvious whether everything works. You can just log into the dev server, click around a bit, and see whether everything looks OK. And changing this bit of code over here, is only ever going to affect these things here and here... So it's easy to change stuff and see if you've broken anything...

But as soon as your project gets slightly larger, complexity rears its ugly head. Combinatorial explosion starts to make you its bitch. Changes start to have unpredictable effects. You start to worry about making changes to that thing over there, because you wrote it ages ago, and you're pretty sure other things depend on it... best to just use it as it is, even though it's hideously ugly... Well, anyway, changing this thing over here shouldn't affect too much stuff. I'll just run through the main bits of the site to check... Can't possibly check everything though... Oh well, I'll just deploy and see if anyone complains...

Automated tests can save you from this fate. If you have automated tests, you can know for sure whether or not your latest changes broke anything. With tests, you're free to keep refactoring your code, to keep trying out new ways to optimise things, to keep adding new functionality, safe in the knowledge that your tests will let you know if you get things wrong.

Look, that's got to be enough evangelising. If you don't believe me, just ask someone else with experience. They know. Now, onto the practicals.

Convinced? Get on with part 1 of the tutorial then!

http://harry.pythonanywhere.com/tutorial/1/

USEFUL LINKS

https://github.com/hjwp/Test-Driven-Django-Tutorial

https://docs.djangoproject.com/en/1.4/intro/tutorial02/

http://seleniumhq.org/docs/03_webdriver.html

http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/remote/webdriver.py

http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/remote/webelement.py

http://www.pythonanywhere.com/

LICENSE

Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported.

http://creativecommons.org/licenses/by-nc-sa/3.0/

test-driven-django-tutorial's People

Contributors

adorilson avatar alfredo avatar hjwp avatar johtso avatar roganov avatar tomviner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

test-driven-django-tutorial's Issues

include virtualenv

Maybe you should suggest using virtualenv instead of plain "easy_install module"?

Spelling mistake

The sentence:
"The question attribute will be translated into a column in the databse."

Doesn't work for python <=2.6 despite hinting so

try: import unittest2 as unittest #for Python <= 2.6
except: import unittest

but throws:

Creating tables ...
Installing custom SQL ...
Installing indexes ...
No fixtures found.
running tests
Traceback (most recent call last):
  File "functional_tests.py", line 70, in <module>
    run_functional_tests()
  File "functional_tests.py", line 55, in run_functional_tests
    tests = unittest.defaultTestLoader.discover('fts')
AttributeError: 'TestLoader' object has no attribute 'discover'

lower case a

look for "Django administration" not "Django Administration" in your test case.

test_voting_on_a_new_poll don't exists.

Hi,

in tutorial 2 part, we have the following FT outputs:

python manage.py test fts

======================================================================
ERROR: test_voting_on_a_new_poll (test_polls.PollsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/harry/workspace/TDDjango/mysite/fts/test_polls.py", line 40, in test_voting_on_a_new_poll
    self._setup_polls_via_admin()
  File "/home/harry/workspace/TDDjango/mysite/fts/test_polls.py", line 26, in _setup_polls_via_admin
    choice_1 = self.browser.find_element_by_name('choice_0')
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 244, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 525, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 144, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 118, in check_response
    raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element: {"method":"name","selector":"choice_set-0-choice"}' 

----------------------------------------------------------------------

But the test_voting_on_a_new_poll test never have been created.

Adorilson

Python code blocks not rendering

When viewing the .rst files in github the python codeblocks do not always render. I can see the code blocks in the raw format though.

Also, the images are broken.

Unable to start the tests, cant find the problem.

Hi,
I am a newbie, and seem unable to fix this problem. In tutorial step 1, where we expect to run the setup fixture:

def setUp(self):
self.browser = webdriver.Firefox()
self.browser.implicitly_wait(3)

my python environment somehow gives the error:

File "C:\Users\PC\PythonVirtualEnvironment\lib\site-packages\django\test\testcases.py", line 1135, in setUpClass
raise cls.server_thread.error
WSGIServerException: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions

Where might be the problem?

easy_install over pip?

easy_install django
easy_install selenium
easy_install mock

I'm new to the python world, but my impression is that pip is the preferred package management system. Thoughts?

I'll be working through your write up, and applying it to my own project to see how well it sticks (and might even extend it for covering APIs )

How to setup PyDev to work with this tutorial

Thank you for this tutorial, makes it very easy to start with TDD in Django.

I was just wondering if it would be possible to add a section with some info on how to setup a project in PyDev so that Django test can be run with the builtin unittest functionality?

Thank you for all the hard work!
Regards,
Uros

Broken Link

bob@ubuntu:~/.../$ wget -O functional_tests.py https://raw.github.com/hjwp/Test-Driven-Django-Tutorial/master/functional_tests.py
--2011-11-19 20:22:52--  https://raw.github.com/hjwp/Test-Driven-Django-Tutorial/master/functional_tests.py
Resolving raw.github.com... 207.97.227.243
Connecting to raw.github.com|207.97.227.243|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2011-11-19 20:22:53 ERROR 404: Not Found.

Test fails even after defining the pub_date attrib

I hope I'm not annoying you with all these issues. If so, tell me and I'll stop and I'll keep them to myself. ;)

In Part 1 under section Our first unit tests: testing a new "Poll" model even after defining the pub_date attrib in the model Poll, the test fails saying IntegrityError: polls_poll.pub_date may not be Null

Instead of

    # start by creating a new Poll object with its "question" set
    poll = Poll()
    poll.question = "What's up?"

    # check we can save it to the database
    poll.save()

    # check we can adjust its publication date
    poll.pub_date = datetime.datetime(2012, 12, 25)
    poll.save()

I believe it should be

    # start by creating a new Poll object with its "question" set
    poll = Poll()
    poll.question = "What's up?"

    # check we can adjust its publication date
    poll.pub_date = datetime.datetime(2012, 12, 25)

    # check we can save it to the database
    poll.save()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.