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

I've noticed that on my local CI test platform, one of my tests is failing and I'm at a loss as to why. It's intermittent, but it fails far more often than not.

Here's the TAP output up until the failure is produced:

pi@pi-test:~/repos/rpi-wiringpi $ prove -v t/75-serial.t t/75-serial.t .. ok 1 - An object of class 'RPi::Serial' isa 'RPi::Serial' ok 2 - putc() and getc() ok not ok 3 - puts() and gets() ok # Failed test 'puts() and gets() ok' # at t/75-serial.t line 32. # got: 'hello, world!' # expected: 'hello, world!'

Notice how the got and expected are identical. What am I missing here?

The test is writing a string to, and then reading the string back from a serial interface. Here's the test code if it helps:

use strict; use warnings; use lib 't/'; use RPiTest qw(check_pin_status); use RPi::WiringPi; use RPi::Const qw(:all); use Test::More; my $mod = 'RPi::WiringPi'; if (! $ENV{RPI_SERIAL}){ plan skip_all => "RPI_SERIAL environment variable not set\n"; } if (! $ENV{PI_BOARD}){ $ENV{NO_BOARD} = 1; plan skip_all => "Not on a Pi board\n"; } my $pi = $mod->new; my $s = $pi->serial("/dev/ttyS0", 115200); isa_ok $s, 'RPi::Serial'; $s->putc(254); is $s->getc, 254, "putc() and getc() ok"; $s->puts("hello, world!"); # FAILING TEST IS BELOW is $s->gets(13), "hello, world!", "puts() and gets() ok"; $pi->cleanup; check_pin_status(); done_testing();

Replies are listed 'Best First'.
Re: Unit test failing but "got" and "expected" are equal
by Corion (Patriarch) on Jan 04, 2019 at 20:09 UTC

    Most likely you're getting tripped up by (weirdo?) whitespace or other stuff that renders identical in the terminal.

    To better diagnose that, I would dump the output on failure in a way that makes the differences spottable:

    my $res = $s->gets(13); if( !is $res, "hello, world!", "puts() and gets() ok") { (my $s = $res) =~ s!([^\w])!sprintf '\\x%02x', ord($1)!ge; diag $s; ($s = "hello, world!") =~ s!([^\w])!sprintf '\\x%02x', ord($1)!ge; diag $s; };

      Good call, thanks Corion!

      # hello\x2c\x20world\x21\x0f # hello\x2c\x20world\x21

      At least now I have something to focus on.

      Update: In the meantime until I can figure out exactly what's going on, I've made the following change to the test so that my automation doesn't keep yelling at me:

      like $s->gets(13), qr/^hello, world!/, "puts() and gets() ok";
        qr/^hello, world!/

        You might also consider  qr/^hello, world!\s*\z/ since  \x0f is a member of the  \s set and the quoted regex allows anything at the end of the string. No! I was thinking of  \f (form feed) and not of  \x0f ... never mind ...


        Give a man a fish:  <%-{-{-{-<