Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Unit Test systems include useless assertive methods? [closed]

I'm wondering why unittest systems like PHPUnit include what seems to be repetitive operators that just add overhead to the unit tests. I can understand a couple of those methods, but most seem like a total waste of time.

public function testPop(array stack)
{
    this->assertEquals('foo', array_pop(stack));
    this->assertEmpty(stack);
}

vs raw code (which is shorter and faster)

public function testPop(array stack)
{
    this->assert('foo' == array_pop(stack));
    this->assert(empty(stack));
}

Are these methods here for just so people that don't understand the language they are programming in can still write unit-tests? I'm sure the authors of this projects are smarter than myself so there must be a reason.

like image 810
Xeoncross Avatar asked Feb 21 '23 11:02

Xeoncross


2 Answers

These functions usually give more useful output. For example, an assertEquals test could tell you the expected and actual values, and that they were not equal.

For example, the following code:

this->assertEquals(1, 0);

Will produce this output:

Failed asserting that 0 matches expected 1.
like image 191
Brendan Long Avatar answered Apr 20 '23 01:04

Brendan Long


It's also about verbosity. This:

this->assert(foo == array_pop(stack));

Is much less verbose than:

this->assertEqual(foo, array_pop(stack));

Or even better:

$popped_value = array_pop(stack);
this->assertEqual($popped_value, foo);

In other frameworks (such as .NET's NUnit), it gets even better:

Assert.AreEqual(stack.Pop(), foo);

vs

var poppedValue = stack.Pop();
Assert.That(poppedValue, Is.EqualTo(foo));

Reading the last example is almost as if somebody was explaining the code to you. That's invaluable when you deal with old/somebody's else code.

like image 28
k.m Avatar answered Apr 19 '23 23:04

k.m