← Back to team overview

canonical-django team mailing list archive

Re: Django testing best practices

 

Hi, Jamu.

On Mon, Jun 14, 2010 at 6:54 AM, Jamu Kakar <jamshed.kakar@xxxxxxxxxxxxx> wrote:
> On Mon, Jun 14, 2010 at 1:44 PM, Deryck Hodge
> <deryck.hodge@xxxxxxxxxxxxx> wrote:
>> On Sun, Jun 13, 2010 at 2:01 PM, James Westby
>> <james.westby@xxxxxxxxxxxxx> wrote:
>>>
>>> We weren't sure about appropriate tests for the basics of models, such
>>> as creating an object and checking that it has an attribute that was
>>> defined via a field. This is hampered as in my testing some things about
>>> the field, such as limits on the length of a string don't seem to be
>>> enforced in a test environment (with the sqlite3 backend).
>>
>> I don't usually test things that are covered by the Django test suite.
>>  So I wouldn't test model validation again, since Django itself has
>> tests for this.  Others don't like to trust the framework this way, so
>> it's a personal choice.  Were it me, I would just test the behavior of
>> the model that is unique to my application.
>
> I try to avoid testing framework code, too.  That said, I do think
> it's worth testing that your model uses the framework as you expect.
> For example, if I have a class like:
>
>    class Person(object):
>        name = String()
>        password = Password(min_length=5)
>
> then a test like:
>
>    def test_minimum_password_length(self):
>        """A person's password must contain at least 5 characters."""
>        person = create_person("username", "password")
>        person.set_password("12345")
>        self.assertRaises(LengthError, person.set_password, "1234")
>
> is testing application logic, even though the logic to ensure the
> constraint is not violated may bubble up from the framework.

This is a good example.  For me, it comes down to what is critical
and/or specific to the application.  User accounts, to continue with
your case here, would need good coverage and have some overlap with
the framework.  It would not be a priority for me to ensure that every
time I set a string length it is enforced via the framework.

Cheers,
deryck


-- 
Deryck Hodge
https://launchpad.net/~deryck
http://www.devurandom.org/



References