http://www.perlmonks.org?node_id=1046475

fsavigny has asked for the wisdom of the Perl Monks concerning the following question:

I am using Test::More to test a function that produces error messages (which are quite long - don't get distracted by the content of the one tested here!) Here, I am simply testing whether the output looks as it should:

is( $returned_message, $expected, 'expected_error_message');

As far as I am concerned, the output looks right, but is says it isn't.

I am sure this must be some very stupid oversight on my part, but I can't seem to figure out where. If you have a look at this snippet of output:

not ok 8 - expected_error_message # Failed test 'expected_error_message' # got: ' # esis2sth: Fatal runtime error triggered in #testelement->#testevent # Bad thing # What triggered this in your code should be in this line in foo.es +s: # 11 Line 11 of the code # [Input from: test.sgml, line 127] # ' # expected: ' # esis2sth: Fatal runtime error triggered in #testelement->#testevent # Bad thing # What triggered this in your code should be in this line in foo.es +s: # 11 Line 11 of the code # [Input from: test.sgml, line 127] # '

can you indentify any difference between got and expected?

It's not only that I cannot see the difference between got and expected, but I have also tried copying the strings between the single quotes into files and then diff'ing these files: no difference.

I am new to testing (but not to Perl), so I hope this problem is somehow familiar to somebody...

Many thanks for any help!!

t get distracted by the content of the one tested here!) Here, I am simply testing whether the output looks as it should:

Replies are listed 'Best First'.
Re: is (from Test::More) fails although strings should (no: must!) be equal
by choroba (Cardinal) on Jul 26, 2013 at 10:57 UTC
    As posted, your strings seem to be identical. This might be a result of copy'n'paste, though, so try to save the strings directly from the code rather than copying them. Pay special attention to whitespace and invisible characters.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      I am really baffled. Some 15 minutes, and already three answers! Thank you so much!

      I chose the write-to-file route because it seemed the most accessible and transparent:

      { my $FH; open $FH, ">", "got.dump"; print $FH $returned_message; close $FH; open $FH, ">", "expected.dump"; print $FH $expected; close $FH; }

      Then I compared got.dump and expected.dump on the command line, where it also became obvious that they even had different sizes. If you open the files in an editor, you see that.

      The reason was indeed the copy&paste: I had somehow forgotten I had colorised some of the output, and copy&paste dropped the control codes: Bad thing.

      So, that was indeed very stupid of me. I had noticed there was something irregular with the colorising, but dismissed it because I reasoned that was not part of what I needed to test because it would be plainly obvious anyway ... what a fuzzy and human way of reasoning.

      is fails no longer! Thanks so much again!

      Florian

Re: is (from Test::More) fails although strings should (no: must!) be equal
by rjt (Curate) on Jul 26, 2013 at 10:58 UTC
    It's not only that I cannot see the difference between got and expected, but I have also tried copying the strings between the single quotes into files and then diff'ing these files: no difference.

    Even so, it's likely that there are differences in whitespace or line endings that are invisible to you, and may either be ignored by diff(1) or be munged by a copy/paste. cmp the strings yourself and use diag to report the difference. If they compare as different, you have a likely suspect.

Re: is (from Test::More) fails although strings should (no: must!) be equal
by daxim (Curate) on Jul 26, 2013 at 10:58 UTC