[Tutor] writing effective unittests

Japhy Bartlett japhy at pearachute.com
Thu Jan 3 23:59:03 CET 2013


The general idea is to write tests that use your code in realistic ways and
check the results.  So if you have a function that takes an input and
returns a result, you write a test that passes that function an input
checks the result.  If some inputs should make it error, you write a test
that checks that it errors.

In web programming, a common thing is to make a web request with different
variations of GET / PUT params, then check that it returns the right status
code, or that the result is valid JSON, etc.  Basically, try to simulate
all the things a real world user would be able to use, and test that your
code does what you intend for it to do.

TDD is a good principle but usually seems a little too pedantic for real
world programming.  Where tests (in my experience) get really useful is in
making sure that a new change hasn't unexpectedly broken something already
written.


On Thu, Jan 3, 2013 at 2:31 PM, Tino Dai <oberoc at gmail.com> wrote:

>
>
> I think what I need is a conceptual shift: how do python programmers use
>> unittests?
>>
>>
> Here at the Library, we use unit test to test the cases that have many
> known inputs. For example, some of the data could come in "foo bar baz",
> and some others could come in as "foo, bar, baz", and others could come in
> "foo; bar; baz". As we code for each one of these cases, we write tests to
> make sure that changes that we make don't break any of our former code. I
> think that TDD is a good thing to strive for, but in most cases is an ideal
> that can't be reached in most cases.
>
> Hope that helps,
> Tino
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130103/50898b3e/attachment.html>


More information about the Tutor mailing list