unit tests

Tom Metro <[email protected]>
Newsgroups gmane.comp.log.logwatch.devel
Message-ID <[email protected]>
MrC wrote:
> Tom Metro wrote:
>> (BTW, any thoughts on writing unit tests for these scripts?)
> 
> I too asked about this some time ago.  The general feedback was 
> negative; so I wrote my own basic test data in the amavis and postfix 
> scripts.
>
> I would personally like to see  (in no particular order):
> 
> 1) test logs and golden files

Sample data can always be improved upon later. It seems the first step 
is agreeing how to structure the tests and where to place them in the 
project directory.

Maybe have a tests/ top-level directory (parallel to the scripts/, lib/, 
project/, etc. directories). Each script could have it's own directory 
to contain its tests and test data, so for example, the Dovecot service 
filter:
tests/scripts/services/dovecot/{data.log,test.t}

Then we'd need to agree on a test runner (like prove[1]) and perhaps 
create a script or Makefile at the top level to conveniently run the 
unit tests.

1. http://search.cpan.org/~andya/TAP-Parser-0.51/bin/prove

Next I'd consider whether it makes sense to create some common 
infrastructure for the tests. For example, for almost every service 
filter you'll want to run the tests at a minimum of the three standard 
detail levels. A common infrastructure might iterate over all the *.log 
files in the script's test directory, pipe the data into the script, and 
pass the output to a method named <logfile>_<detailLevel> for validation.

I'd drive this process from the .t file, so you'd be free to skip the 
standard stuff if not applicable, or supplement with additional tests. 
The end result may be just a helper library, like Test::More, that 
provides functions like:
  pipe($script,$data_file,&validate,"$data_file: valid for detail level 
%d");

  -Tom

-- 
Tom Metro
Venture Logic, Newton, MA, USA
"Enterprise solutions through open source."
Professional Profile: http://tmetro.venturelogic.com/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.