[Tutor] Unit testing
Carroll, Barry
Barry.Carroll at psc.com
Tue Jun 27 01:35:10 CEST 2006
Regards,
Tino:
I agree with Kent on this. As much as possible, a unit test should test
what it is supposed to do.
> Date: Mon, 26 Jun 2006 15:50:36 -0400
> From: "Tino Dai" <tinoloc at gmail.com>
> Subject: Re: [Tutor] Unit testing
> To: "Kent Johnson" <kent37 at tds.net>, tutor at python.org
> Message-ID:
> <e033edfb0606261250n7288c8bdufce5045d5829c926 at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> > I often write unit tests that do this. In my opinion it is simple
and
> > straightforward and effective. Some purists will insist that a unit
test
> > shouldn't write the file system or touch a database or use any other
> > external resource, but I think that is silly - if the job of a bit
of
> code
> > is to write a file or interact with the database, then the simplest
way
> to
> > test it is to check the file or database. As long as the tests run
fast
> > enough it's OK. (For me, a unit test on a single module should
ideally
> run
> > in well under a second.)
> >
> > Alternately you can use StringIO or other substitutes for files in
your
> > tests. But somewhere in your test system you probably want to make
sure
> the
> > actual file is there on disk, whether it is in a unit test or
acceptance
> > test.
>
>
> Ok, that leads me to my next question. Currently, I have a class that
I
> want to unit test, but it contains a semaphore from another class.
Now, I
> could make the semaphore a global variable, or I bring in the other
class.
> One violates "good" programming principles and the other violates the
unit
> testing principles. Is there another way?
>
> -Tino
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
>
http://mail.python.org/pipermail/tutor/attachments/20060626/bf127497/att
ac
> hment.html
>
> ------------------------------
I think the same principle applies here. The code you are testing
involves a semaphore. By all means, include the other class and test
the semaphore.
I'm not familiar with the 'unit testing principles' that would forbid
your doing this. Even if I were, principles are to be followed when
they make sense. When it makes better sense to violate a principle,
then violate it.
Barry
barry.carroll at psc.com
541-302-1107
________________________
We who cut mere stones must always be envisioning cathedrals.
-Quarry worker's creed
More information about the Tutor
mailing list