Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertEquals() hides contents of long strings #5846

Open
mikkorantalainen opened this issue May 30, 2024 · 2 comments
Open

assertEquals() hides contents of long strings #5846

mikkorantalainen opened this issue May 30, 2024 · 2 comments
Labels
feature/assertion Issues related to assertions and expectations type/bug Something is broken

Comments

@mikkorantalainen
Copy link

Q A
PHPUnit version 10.5.20
PHP version 8.1.2-1ubuntu2.17 (cli)
Installation Method PHAR

Summary

When I have long strings in an array that's passed to assertEquals() and the values are not equal, the emitted failure message clips away important part of the string.

How to reproduce

mkdir test && cd test
wget -O phpunit https://phar.phpunit.de/phpunit-10.phar
cat << 'EOF' > UnitTest.php
<?php
class UnitTest extends PHPUnit\Framework\TestCase
{
  public function testAssertingArrays()
  {
    $mock_testresult = array(
      0 => "Some short string",
      1 => "Some really long string that just keeps going and going and going but contains important clue HERE about why this whole test failed.",
    );
    $this->assertEquals($mock_testresult, array(), "Expected empty array but got non-empty array.");
  }
}
EOF
php phpunit UnitTest.php

Current behavior

PHPUnit 10.5.20 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.2-1ubuntu2.17

F                                                                   1 / 1 (100%)

Time: 00:00.001, Memory: 24.79 MB

There was 1 failure:

1) UnitTest::testAssertingArrays
Expected empty array but got non-empty array.
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
-    0 => 'Some short string'
-    1 => 'Some really long string that ...ailed.'
 )

/home/user/test/UnitTest.php:10

FAILURES!
Tests: 1, Assertions: 1, Failures: 1

Expected behavior

Otherwise same but the array offset 1 should show enough of the string to also see the word "HERE".

Additional information

I can see the whole string if I run php phpunit --debug UnitTest.php instead but the output is so verbose otherwise that it's hard to read. I'd rather see normal output but the whole string, but I couldn't find any documented feature to turn off this kind of string clipping/shortening.

@mikkorantalainen mikkorantalainen added the type/bug Something is broken label May 30, 2024
@sebastianbergmann sebastianbergmann added feature/assertion Issues related to assertions and expectations type/enhancement A new idea that should be implemented type/bug Something is broken and removed type/bug Something is broken type/enhancement A new idea that should be implemented labels May 31, 2024
@hans-thomas
Copy link

hans-thomas commented Sep 27, 2024

@sebastianbergmann It's because of this block of code in the sebastian/exporter package.

Should we remove that condition to solve this?

@mfn
Copy link

mfn commented Sep 27, 2024

Wow, it would be super useful to not have this clipping at all.

I've so many cases where I see the clipping where I realize my "expected" actually is wrong and it would just speed up things if I can copypaste it from that output and carry on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/assertion Issues related to assertions and expectations type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

4 participants