[TIP] Docstrings in Test methods

Robert Collins robertc at robertcollins.net
Sun Nov 6 11:44:44 PST 2011


On Sun, Nov 6, 2011 at 3:21 PM, Alfredo Deza <alfredodeza at gmail.com> wrote:
> On Sat, Nov 5, 2011 at 2:25 PM, Gary Bernhardt <gary.bernhardt at gmail.com> wrote:
>> On Sat, Nov 5, 2011 at 8:50 AM, Alfredo Deza <alfredodeza at gmail.com> wrote:
>>> What do you guys prefer and why (or why not?)
>>
>> The problem isn't that you need more space for the description; that's
>> just the symptom. The problem is that your'e not separating the name
>> of the context from the name of the expectation. Try naming your
>> classes for the state of the world, and the tests for the exact thing
>> that you expect to happen. Like this:
>>
>> class when_bar_connects_successfully:
>>    def foo_is_true(self):
>>        ...
>>
>
> Not sure I entirely agree with this approach, it could get very
> repetitive very fast. I tend
> to have test classes that I can group with a setup that makes sense
> for a few tests, if I name
> the classes after the state I would have to probably use inheritance
> to avoid repeating setup
> over and over again.

Thats what I wrote fixtures (http://pypi.python.org/pypi/fixtures) for
- decouple your setup from your test classes, and you can get a
particular setup trivially:
class foo(TestCase):
   def test_example(self):
       state = self.useFixture(ComplexSetup())

-Rob



More information about the testing-in-python mailing list