NAME

Test::Excel - Interface to test and compare Excel files (.xls/.xlsx).

VERSION

Version 1.54

DESCRIPTION

This module is meant to be used for testing custom generated Excel files, it provides interfaces to compare_excel two Excel files if they are visually same. It now supports Excel files with the extensions .xls or .xlsx.

SYNOPSIS

Using as unit test as below:

use strict; use warnings;
use Test::More tests => 2;
use Test::Excel;

cmp_excel_ok("foo.xls", "foo.xls");

cmp_excel_not_ok("foo.xls", "bar.xls");

done_testing();

Using as standalone as below:

use strict; use warnings;
use Test::Excel;

if (compare_excel("foo.xls", "foo.xls")) {
    print "Excels are similar.\n";
}
else {
    print "Excels aren't similar.\n";
}

METHODS

cmp_excel($got, $exp, \%rule, $message)

This function will tell you whether the two Excel files are "visually" different, ignoring differences in embedded fonts / images and metadata. Both $got and $exp can be either instance of Spreadsheet::Read / file path (which is in turn passed to the Spreadsheet::Read constructor). This one is for use in TEST MODE.

use strict; use warnings;
use Test::More tests => 1;
use Test::Excel;

cmp_excel('foo.xls', 'bar.xls', {}, 'EXCELs are identical.');

done_testing();

cmp_excel_ok($got, $exp, \%rule, $message)

Test OK if excel files are identical. Same as cmp_excel().

cmp_excel_not_ok($got, $exp, \%rule, $message)

Test OK if excel files are NOT identical.

compare_excel($got, $exp, \%rule)

Same as cmp_excel_ok() but ideal for non-TEST MODE. This function will tell you whether the two Excel files are "visually" different, ignoring differences in embedded fonts / images and metadata. Both $got and $exp can be either instance of Spreadsheet::Read / file path (which is in turn passed to the Spreadsheet::Read constructor).

use strict; use warnings;
use Test::Excel;

print "EXCELs are identical.\n" if compare_excel("foo.xls", "bar.xls");

RULE

The parameter rule can be used optionally to apply exception when comparing the contents. This should be passed in as has ref and may contain keys from the table below.

+-----------------+---------------------------------------------------------+
| Key             | Description                                             |
+-----------------+---------------------------------------------------------+
| sheet           | "|" seperated sheet names.                              |
| tolerance       | Number. Apply to all NUMBERS except on 'sheet'/'spec'.  |
|                 | e.g. 10**-12                                            |
| sheet_tolerance | Number. Apply to sheets/ranges in the spec. e.g. 0.20   |
| spec            | Path to the specification file.                         |
| swap_check      | Number (optional) (1 or 0). Row swapping check.         |
|                 | Default is 0.                                           |
| error_limit     | Number (optional). Limit error per sheet. Default is 0. |
+-----------------+---------------------------------------------------------+

SPECIFICATION FILE

A spec file containing rules used should be in the format mentioned below. Keys and values are space-separated.

sheet       Sheet1
range       A3:B14
range       B5:C5
sheet       Sheet2
range       A1:B2
ignorerange B3:B8

As in v1.51 or above, we now support the use of regex in the specification file.

The following specification forces regex comparison in all sheets in range B2:B4.

sheet ALL
range B2:B4
regex 2022\-\d\d\-\d\d

The following specification forces regex comparison in all sheets.

sheet ALL
regex 2022\-\d\d\-\d\d

The following specification forces regex comparison in the sheet named Demo in range B2:B4.

sheet Demo
range B2:B4
regex 2022\-\d\d\-\d\d

What Is "Visually" Similar?

This module uses the Spreadsheet::Read module to parse the Excel files and then compares the parsed data structure for differences. It ignores certain components of the Excel file, such as embedded fonts, images, forms and annotations, and focuses entirely on the layout of each Excel page instead. Future versions may support font and image comparisons as well.

How to find out what failed the comparison?

Setting the environment variable DEBUG to a non-zero, non-empty value will output the PASS/FAIL comparison. For example:

$> $DEBUG=1 perl your-test-script.pl

NOTES

It should be clearly noted that this module does not claim to provide fool-proof comparison of generated Excel files. In fact there are still a number of ways in which I want to expand the existing comparison functionality. This module is no longer actively being developed as I moved to another company. This work was part of one of my projects. Having said that, I would be more than happy to add new features if requested. Any suggestions / ideas most welcome.

CAVEATS

Testing large Excel files can take a long time. This is because, well, it is doing a lot of computation. In fact, the test suite for this module includes tests against several large Excel files; however, I am not including those in this distibution for obvious reasons.

BUGS

None that I am aware of. Of course, if you find a bug, let me know, and I would do my best to fix it. This is still a very early version, so it is always possible that I have just "gotten it wrong" in some places.

SEE ALSO

Spreadsheet::Read - I could not have written without this module.

ACKNOWLEDGEMENTS

H.Merijn Brand (author of Spreadsheet::Read).
Kawai Takanori (author of Spreadsheet::ParseExcel::Utility).
Stevan Little (author of Test::PDF).

AUTHOR

Mohammad Sajid Anwar, <mohammad.anwar at yahoo.com>

CONTRIBUTORS

  • Julien Fiegehenn

  • Ed Sabol

REPOSITORY

https://github.com/manwar/Test-Excel

BUGS

Please report any bugs or feature requests through the web interface at https://github.com/manwar/Test-Excel/issues. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Test::Excel

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright (C) 2010 - 2024 Mohammad Sajid Anwar.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License.By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you,you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement,then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.