[Python-ideas] Unittest error message failure context lazy creation
Steven D'Aprano
steve at pearwood.info
Sat Aug 26 22:19:12 EDT 2017
On Sat, Aug 26, 2017 at 08:25:41PM +0200, francismb wrote:
> Hi all,
> while using `unittest` I see the pattern of creating an error message
> with the test context for the case that some `assert...` methods fails
> (to get a good error message). On the lines:
[...]
> The `msg` is just used in case the assert fails but its creation takes
> time and adds up.
>
> What is the best practice/pattern you use here?
I think the best practice here is:
The Rules of Optimization are simple. Rule 1: Don’t do it. Rule 2 (for
experts only): Don’t do it yet.
-- Michael A. Jackson, "Principles of Program Design"
Personally, I doubt that the time creating the error message will be
anything more than an insignificant fraction of the total time.
Perhaps as much as 0.1% of the total time? But I've just plucked that
number out of thin air, so it's probably wrong.
If you want to profile unittest and see just how much time is spent
creating error messages for tests which pass, go right ahead and I'll be
happy to be proven wrong. Until somebody actually profiles the tests,
and demonstrates that delaying creating of the error messages has the
potential to speed up unit testing by, oh, at least 5%, I'm sticking
with "don't do it yet".
--
Steve
More information about the Python-ideas
mailing list