Hi all,

This is a question of test code style.

In tests in my own projects, I use both the Python 'assert' statement
and the unittest TestCase.assert* methods, with the following
distinction:

* The 'assert' statement is used to make assertions about the
assumptions built in to *this* bit of code (i.e. the test itself).

* The TestCase.assert* methods are used to make assertions about the
outcome of the bit of code that is being tested.

This is consistent with the use of the assert statement in other code
and makes lots of sense to me. When reading tests it helps me see
straight away what are the assumptions and sanity checks in the test,
and what is actually being tested. It also has the advantage that if you
have some setup code in a test, and it has assert statements in it, it
can be moved out of a TestCase method without being altered.

Looking at Django's test suite, I can find a few instances where this
pattern is followed (e.g. [1], [2]). Sometimes the assert statement is
also used where it is impractical to call TestCase methods (e.g. [3]).
There are a few instances where the 'assert' statement is called when it
should be a TestCase method (e.g. [4]). And there are many instances
where TestCase asserts are called, when IMO it should be an assert
statement. (e.g. [5]).

So, the question is, going forward do we:

1) Use the pattern I outlined above?
2) Use a different pattern (e.g. always use TestCase assert methods
where it is possible)?
3) Simply not care about any of this?

My preference is to encourage 1). With existing tests, I would be
tempted to change the handful of instances which use the assert
statement when it should be TestCase assert method, but I wouldn't
bother with the many instances which are the other way around, unless I
was in the area.

Regards,

Luke

[1]
http://code.djangoproject.com/browser/django/trunk/tests/regressiontests/templates/tests.py#L347
[2]
http://code.djangoproject.com/browser/django/trunk/tests/regressiontests/utils/simplelazyobject.py#L70
[3]
http://code.djangoproject.com/browser/django/trunk/tests/regressiontests/model_forms_regress/models.py#L37
[4]
http://code.djangoproject.com/browser/django/trunk/tests/modeltests/model_forms/tests.py#L35
[5]
http://code.djangoproject.com/browser/django/trunk/tests/modeltests/serializers/tests.py#L69


-- 
"Smoking cures weight problems...eventually..." (Steven Wright)

Luke Plant || http://lukeplant.me.uk/


-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to