Re: [HACKERS] Thoughts on unit testing? - Mailing list pgsql-hackers

From Torsten Zuehlsdorff
Subject Re: [HACKERS] Thoughts on unit testing?
Date
Msg-id be82716d-081c-8b6f-2a04-d22b4e8ae196@toco-domains.de
Whole thread Raw
In response to Re: [HACKERS] Thoughts on unit testing?  (Peter Geoghegan <pg@bowt.ie>)
Responses Re: [HACKERS] Thoughts on unit testing?  (Chris Travers <chris.travers@adjust.com>)
List pgsql-hackers

On 13.08.2017 21:19, Peter Geoghegan wrote:
> On Thu, Aug 10, 2017 at 2:53 PM, Thomas Munro
> <thomas.munro@enterprisedb.com> wrote:
>> The current regression tests, isolation tests and TAP tests are very
>> good (though I admit my experience with TAP is limited), but IMHO we
>> are lacking support for C-level unit testing.  Complicated, fiddly
>> things with many states, interactions, edge cases etc can be hard to
>> get full test coverage on from the outside.  Consider
>> src/backend/utils/mmgr/freepage.c as a case in point.
> 
> It is my understanding that much of the benefit of unit testing comes
> from maintainability. 

I never had this understanding. I write tests to test expected behavior 
and not the coded one. If possible i separate the persons writing 
unit-tests from the ones writing the function itself. Having someone 
really read the documentation or translate the expectation into a 
test-case, makes sure, the function itself works well.

Also it really safes time in the long run. Subtle changes / bugs can be 
caught which unit-tests. Also a simple bug-report can be translated into 
a unit-test make sure that the bugfix really works and that no 
regression will happen later. I literally saved ones a week of work with 
a single unit-test.

There are many other advantages, but to earn them the code need to be 
written to be testable. And this is often not the case. Most literature 
advises to Mocking, mixins or other techniques, which most times just 
translate into "this code is not written testable" or "the technique / 
language / concept / etc is not very good in being testable".

Greetings,
Torsten



pgsql-hackers by date:

Previous
From: Fabien COELHO
Date:
Subject: Re: [HACKERS] proposal: psql command \graw
Next
From: Masahiko Sawada
Date:
Subject: Re: [HACKERS] pgbench: Skipping the creating primary keys after initialization