Kevin Burke
Kevin Burke

Reputation: 64776

Difference between assertEquals and assertSame in PHPUnit?

PHPUnit contains an assertEquals() method, but it also has an assertSame() one. At first glance it looks like they do the same thing.

What is the difference between the two? Why are they both specified?

Upvotes: 168

Views: 98062

Answers (8)

ggorlen
ggorlen

Reputation: 56875

Never use assertEquals (and family)*

* unless arguments were first manually type-checked, or you're fully aware of the behavior and are prepared for tests to pass when it treats, say, true and 42, as equal--I can't imagine why one would tolerate the risk of extremely subtle false positives in critical testing code.

For primitive values (other than floats) and arrays

Use assertSame. The behavior is predictable. "42" and true are not equal.

For floats

Avoid if possible by using integers. If you must compare floats, assertEqualsWithDelta is as broken as assertEquals:

$this->assertEqualsWithDelta(0.99, "1", 0.1); // passes :\
$this->assertEqualsWithDelta(99999, true, 0.1); // passes D:

The implementation of assertEqualsWithDelta uses an inaccurate approach, as Comparing Floating Point Numbers describes: abs($this->value - $other) < PHP_FLOAT_EPSILON;, so you might want to write a custom function.

Or type-check before calling assertEqualsWithDelta to avoid the more immediate issues:

function is_number($v) {
    return is_float($v) || is_int($v);
}

class FloatTest extends TestCase {
    private function assertClose(
        $expected,
        $actual,
        $delta = 1e-7
    ): void {
        if (!is_number($expected)) {
            $type = gettype($expected);
            throw new Error("\$expected type $type was not a number");
        }
        else if (!is_number($actual)) {
            $type = gettype($actual);
            throw new Error("\$actual value $type was not a number");
        }
        else if (!is_number($delta)) {
            $type = gettype($delta);
            throw new Error("\$delta value $type was not a number");
        }
    
        $this->assertEqualsWithDelta($actual, $expected, $delta);
    }

    public function testFloats() {
        $this->assertClose(2, "2"); // fails as it should
    }
}

For objects

Use a custom equals(self $other): bool function and assertObjectEquals.

If all else fails

You can't go wrong with assertTrue, the downsides being a lack of semantic appropriateness and poor default error message.

Upvotes: 1

As it's been said before, assertSame reports an error if the two elements do not share type and value but it's also important to note this from the documentation:

Reports an error identified by $message if the two variables $expected and $actual do not reference the same object.

So this test would fail too even though they share type and value:

class SameTest extends TestCase
{
    public function testFailure()
    {
        $this->assertSame(new stdClass, new stdClass);
    }
}

Upvotes: 3

Grigoreas P.
Grigoreas P.

Reputation: 2472

When it comes to objects comparison:

assertSame

Can only assert if two objects are referencing the same object instance. So even if two separate objects have for all of their attributes exactly the same values, assertSame() will fail if they don't reference the same instance.

$expected = new \stdClass();
$expected->foo = 'foo';
$expected->bar = 'bar';

$actual = new \stdClass();
$actual->foo = 'foo';
$actual->bar = 'bar';

$this->assertSame($expected, $actual); // FAILS

assertEquals

Can assert if two separate objects match their attribute values in any case. So it's the method suitable for asserting object match.

$this->assertEquals($expected, $actual); // PASSES

Reference

Upvotes: 49

Mike Purcell
Mike Purcell

Reputation: 19979

I use both sporadically, but according to the docs:

assertSame

Reports an error identified by $message if the two variables $expected and $actual do not have the same type and value."

And as you can see in the example below the above excerpt, they are passing '2204' and 2204, which will fail using assertSame because one is a string and one is an int, basically:

'2204' !== 2204
assertSame('2204', 2204) // this test fails

assertEquals

"Reports an error identified by $message if the two variables $expected and $actual are not equal."

assertEquals does not appear to take datatype into consideration so using the above example of 2204:

'2204' == 2204
assertEquals('2204', 2204) // this test passes

I just ran some unit tests against the above examples, and indeed they resulted in documented behavior.

Upvotes: 254

Richard A Quadling
Richard A Quadling

Reputation: 3988

As previously mentioned, assertEquals() is primarily about an interpreted value, be it by type juggling or an object with an __magic presentation method (__toString() for example).

A good use case for assertSame() is testing a singleton factory.

class CacheFactoryTest extends TestCase
{
    public function testThatCacheFactoryReturnsSingletons()
    {
        $this->assertSame(CacheFactory::create(), CacheFactory::create());
    }
}

Upvotes: 0

GogromaT
GogromaT

Reputation: 511

Moreover,

// Passes
$this->assertSame("123.", "123.");
$this->assertEquals("123.", "123");
// Fails
$this->assertSame("123.", "123");

Upvotes: 1

bronze man
bronze man

Reputation: 1607

$this->assertEquals(3, true);
$this->assertSame(3, true);

The first one will pass!

The second one will fail.

That is the difference.

I think you should always use assertSame.

Upvotes: 21

Arpan Buch
Arpan Buch

Reputation: 1400

assertSame() == Tests that if the actual output and the expected parameter are same.

that is :

$this->assertSame('$expected','$expected');

or

$this->assertSame('100','100');

assertEquals == If we see with respect to a website page, i have a page which has 2 'table' so when i run assertEquals i will check its count that the 'table' are 2 by using a count function. Eg:

$this->assertEquals(2, $var->filter('table')->count()); 

Here we can see that assertEquals checks that there are 2 tables found on the web page. we can also use divisions found on the page using '#division name' inside the bracket.

Eg 2:

public function testAdd()
{
    $calc = new Calculator();

    $result = $calc->add(30, 12);

    // assert that our calculator added the numbers correctly!
    $this->assertEquals(42, $result);
}

Upvotes: 0

Related Questions