简体   繁体   English

单元测试的概念,是否应该将这些断言拆分为“单元”?

[英]Unit testing concept, should these assertions be splitted to be “unit”?

Despite the fact that I'm being playing for a little while with unit testing, I can't really get the concept of "unit", that is a single functionality. 尽管我在单元测试中玩了一段时间,但我并没有真正理解“单元”的概念,即单一功能。

For example, I'm testing a group of magic methods in the form of newXxx : 例如,我正在测试一组newXxx形式的魔术方法:

public function testMagicCreatorWithoutArgument()
{
    $retobj = $this->hobj->newFoo();

    // Test that magic method sets the attribute
    $this->assertObjectHasAttribute('foo', $this->hobj);
    $this->assertInstanceOf(get_class($this->hobj), $this->hobj->foo);

    // Test returned $retobj type
    $this->assertInstanceOf(get_class($this->hobj), $retobj);
    $this->assertNotSame($this->hobj, $retobj);

    // Test parent property in $retobj
    $this->assertSame($this->hobj, $retobj->getParent());
}

As you can see there are three "groups" of assertions in this test method. 如您所见,此测试方法中有三个断言的“组”。 Should I split them into three single test methods, in order to follow the "unit testing" principle? 为了遵循“单元测试”原则,是否应该将它们分为三种单一的测试方法?

Split would be something like: 拆分将类似于:

public function testMagicCreatorWithoutArgumentSetsTheProperty()
{
    $this->hobj->newFoo();

    $this->assertObjectHasAttribute('foo', $this->hobj);
    $this->assertInstanceOf(get_class($this->hobj), $this->hobj->foo);
}

/**
 * @depends testMagicCreatorWithoutArgumentReturnsNewInstance
 */
public function testMagicCreatorWithArgumentSetsParentProperty()
{
    $retobj = $this->hobj->newFoo();

    $this->assertSame($this->hobj, $retobj->getParent());
}

public function testMagicCreatorWithoutArgumentReturnsNewInstance()
{
    $retobj = $this->hobj->newFoo();

    $this->assertInstanceOf(get_class($this->hobj), $retobj);
    $this->assertNotSame($this->hobj, $retobj);
}

Your test method is testing this: 您的测试方法正在对此进行测试:

   $retobj = $this->hobj->newFoo();

in one test, and performing multiple assertions on this one object. 在一个测试中,并对这个对象执行多个断言。 That doesn't seem unreasonable. 这似乎并不合理。

I would be more concerned if you were testing multiple method invocations in the one test. 如果您要在一个测试中测试多个方法调用,我会更担心。 Why ? 为什么呢 An early assertion would abort the test and not perform a test on a further method. 早期声明会中止测试,并且不会对其他方法执行测试。 At best that means the second method wasn't tested, and at worst it hides evidence revealed by the 2nd test (that evidence could help identify the cause or scope of the first failure) 最好的情况是未测试第二种方法,而最糟糕的是它隐藏了第二次测试所揭示的证据(该证据可帮助确定第一次失败的原因或范围)

For this reason I do try to avoid too many assertions in the unit test method. 因此,我确实尝试避免在单元测试方法中使用过多的断言。 It's not unreasonable to check for a null object, and then check (in the same test) for a populated field. 检查一个空对象,然后检查(在同一测试中)一个填充字段并不是没有道理的。 I wouldn't chain those assertions together, though, and would rather have multiple tests testing different functionality of the same returned entity. 不过,我不会将这些断言链接在一起,而是希望进行多个测试来测试同一返回实体的不同功能。

As ever, there's a degree of practicality to be applied here. 与以往一样,这里有一定程度的实用性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM