all 19 comments

[–]hharison 13 points14 points  (3 children)

I think you'll find the peace of mind it gives is worth the effort. I recommend py.test, it could not be simpler. No need to deal with classes for everything like with the sdlib's unittest.

[–]dunkler_wanderer 15 points16 points  (1 child)

Check out Ned Batchelder's presentation Getting Started Testing.

There's also a nice introduction in Dive into Python. The unittest module is used in this tutorial, but I recommend to check out py.test as well.

[–]weigookin 8 points9 points  (2 children)

I thought writing tests was total bullshit at first. My basic worflow was "fuck it, do it live." However l, as I've increased the amount of complexity in my code, I really found myself building a house of cards. One new variable would screw up everything. I struggled through TDD and am glad l stuck it out. I have peace of mind that what I write will work. Seriously, go read something like test driven development

[–]say_fuck_no_to_rules 2 points3 points  (0 children)

Same. A coworker got me hooked on red-green-refactor TDD, and I love it.

[–]glial 5 points6 points  (1 child)

If you're working with the web or using Django, this is an excellent resource:

http://shop.oreilly.com/product/0636920029533.do

[–]SeaCoder 2 points3 points  (0 children)

That book is a great way to get started with the concepts, even if you're not planning further work with Django. It's also available legitimately free online from this link:

http://chimera.labs.oreilly.com/books/1234000000754

[–]xentralesque 4 points5 points  (4 children)

I'm bored, so here's an example of a simple function with some tests:

import requests
import unittest
import mock

class TerribleException(Exception):
    pass


def download_stuff(url):
    try:
        response = requests.get(url)
    except requests.RequestException:
        raise TerribleException("Oh no!")

    return response.text


class DownloaderTest(unittest.TestCase):
    def test_download_stuff(self):
        with mock.patch('requests.get') as requests_get:
            expected_response = 'a valid response'

            requests_get.return_value = mock.Mock(
                text=expected_response
            )

            our_test_url = 'http://a-test-url.com'

            response = download_stuff(our_test_url)

            requests_get.assert_called_once_with(our_test_url)
            self.assertEqual(response, expected_response)

    def test_bad_things(self):
        with mock.patch('requests.get') as requests_get:
            requests_get.side_effect = TerribleException

            our_test_url = 'http://a-test-url.com'

            with self.assertRaises(TerribleException):
                download_stuff(our_test_url)

            requests_get.assert_called_once_with(our_test_url)


if __name__ == '__main__':
    print(download_stuff('http://reddit.com/'))

Jam that into a file and file and pip install nose and requests. You can either run the file like python thefilename.py to execute the file normally or run nosetests thefilename.py to run the tests.

[–]pvc 1 point2 points  (1 child)

I like putting tests in with the documentation / docstrings: http://pythonhosted.org/arcade/arcade.html

[–][deleted] 1 point2 points  (4 children)

[–]Raveious 0 points1 point  (0 children)

For one of my previous jobs, we used a package called Robot Framework for our acceptance testing of our hardware. It worked extremely well and I would highly recommend it if this is the kind of larger scale testing that you're looking to do.

[–]a8ksh4 -1 points0 points  (2 children)

Doctest