[TIP] how can I find in TestCase whether runs verbosely?

Chris Jerdonek chris.jerdonek at gmail.com
Mon Feb 18 01:30:27 PST 2013


On Mon, Feb 18, 2013 at 1:01 AM, Matěj Cepl <mcepl at redhat.com> wrote:
> On 18/02/13 00:29, Robert Collins wrote:
>> Can I ask why?
>>
>> Background:
>> The idea is that there are two completely separate(*) things happening :
>> Tests are running
>> Something is showing the result of those tests to users.
>>
>> [...]
>>
>> So I'm guessing you have something you want to do, but I'm not sure
>> why the test case needs to know.
>
> Maybe what I need is --debug mode.
>
> I am trying (as my lame attempt to commemorate Aaron Swartz) to make
> html2text.py (http://luther.ceplovi.cz/git/html2text.git) fully tested
> and working on Python 2.4-3.3 (both inclusive). Generally the tests are
> in a simple manner (I know, it is not the best possible manner of
> testing, but complete rewrite of the test suite is something I would
> rather avoid) ... take a HTML file, run it through the process, and
> compare the result with stored text file (actually, it is a Markdown) by
> plain self.assertEqual.
>
> You are right that for normal running of the test suite (to make sure,
> that nothing has been broken) both in verbose and non-verbose mode, it
> is probably best to leave the things as they are, but now when I am
> debugging some really intricate case, it might be useful (and maybe not,
> and I should use pdb more) to let the test suite to spit out generated
> text files for deeper analysis.
>
> What do you think?

A couple approaches I have taken for this (with standard unittest) are--

1) Choose a dedicated logger.Logger for test logging, and use
log.debug() with that logger for debug messages during your testing.
When testing, configure your logging to log only the test logger and
suppress other loggers.  When testing in verbose mode, test debug
messages will be logged as desired.

2) For cases where files are generated during testing, choose a
temporary directory for all testing. Also define a context manager
that creates a subdirectory of this temp directory where the
subdirectory name is a function of the test case id.  Define the
context manager in a way that deletes the temporary directory only if
the test case succeeds.  This way, the contents of the directories of
all failed test cases will be available after test runs for
inspection.

Neither of these approaches requires the TestCase to know whether
tests are being run in debug/verbose mode or not.

--Chris



More information about the testing-in-python mailing list