简体   繁体   English

是否可以在不扩展PHPUnit_Framework_TestCase的情况下使用PHPUnit断言?

[英]Is it possible to use PHPUnit assertions without extending PHPUnit_Framework_TestCase?

Maybe the question comes across as weird, but here is the problem that I'm trying to solve... first of all, please keep in mind I am more of a Java developer getting used to working with PHP, so maybe my thought process is the problem! 也许这个问题很奇怪,但这是我要解决的问题......首先,请记住,我更像是一个习惯使用PHP的Java开发人员,所以也许我的想法过程是问题!

I am testing a web site that I have built with Symfony. 我正在测试我用Symfony构建的网站。 For my component testing, I create my test class extending WebTestCase and my test I have a set of assertions like the following to verify that the page title is where I want it and containing what I expect: 对于我的组件测试,我创建我的测试类扩展WebTestCase和我的测试我有一组断言如下,以验证页面标题是我想要它并包含我期望的:

$text = "Page Title";
$selector = "h2#pageHeading";
$this->assertEquals(1, $crawler->filter($selector)->count(), "Found wrong number of elements using selector ".$selector);
$this->assertEquals(trim($text),
        trim($crawler->filter($selector)->text()),
        "Element $selector didn't have expected text");

Then I write more tests for other pages within the site, and in all of them I want to test the title is there as it should be, so to be able to reuse code I refactor the above into a function in parent class that the other test classes reuse: 然后我为站点内的其他页面编写了更多的测试,并且在所有这些中我想测试标题是否应该如此,所以为了能够重用代码我将上面的内容重构为父类中的一个函数测试类重用:

function assertPageTitle($text) {
    $selector = "h2#pageHeading";
    $this->assertEquals(1, $crawler->filter($selector)->count(), "Found wrong number of elements using selector ".$selector);
    $this->assertEquals(trim($text),
            trim($crawler->filter($selector)->text()),
            "Element $selector didn't have expected text");
}

And I call that method in my tests. 我在测试中称这种方法。 As tests develop there are more similar "complex assertions" that I can refactor, and all of them go to the parent class, thus bloating my parent class into a massive assertion container: 随着测试的发展,我可以重构更多类似的“复杂断言”,并且所有这些都会转到父类,从而使我的父类膨胀成一个庞大的断言容器:

protected function assertSelectedOptionContainsTextValue($selector, $text, $value, $crawler) {
    ...
}

protected function assertMenusContainItems($menus, $crawler) {
    ...
}

protected function assertErrorMessageShown($message, $crawler) {
    ...
}

... (more and more) ...

You get the idea. 你明白了。 My next thought at this point is to refactor all this "complex assertions" into other classes, probably following the Page Object pattern, but then the other classes won't have access to the assertEquals method unless those other classes also extend WebTestCase or at least PHPUnit_Framework_TestCase , which doesn't seem a very good idea... 我在这一点上的下一个想法是将所有这些“复杂断言”重构为其他类,可能遵循Page Object模式,但是其他类将无法访问assertEquals方法,除非那些其他类也扩展了WebTestCase或者至少PHPUnit_Framework_TestCase ,这似乎不是一个好主意......

So is there an easy way to access the assertEquals method (and related) without having to extend the base PHPUnit classes? 那么有一种简单的方法来访问assertEquals方法(和相关的)而无需扩展基础PHPUnit类吗? Can I use composition somehow? 我可以用某种方式使用作文吗?

PHPUnit's built-in assertions are implemented as public static methods in the PHPUnit_Framework_Assert class. PHPUnit的内置断言在PHPUnit_Framework_Assert类中实现为公共静态方法。 Just invoke them as PHPUnit_Framework_Assert::assertTrue() , for instance. 例如,只需将它们调用为PHPUnit_Framework_Assert::assertTrue()

First of all, you are overwriting $text variable at the beginning of your "generic" function - but I understand that you want to only show idea. 首先,你在“通用”函数的开头覆盖了$ text变量 - 但我知道你只想表现出想法。

Secondly, using unit testing to this kind of tests isn't the best choose. 其次,使用单元测试来进行这种测试并不是最好的选择。 I think you should extract logic of titles to separated classes and then test it by PHPUnit or use more appropriate testing solutions like Behat or Selenium. 我认为您应该将标题的逻辑提取到分离的类中,然后通过PHPUnit进行测试或使用更合适的测试解决方案,如Behat或Selenium。

If you still haven't changed your opinion... You must extend at least PHPUnit_Framework_TestCase whenever you want to use PHPUnit to testing. 如果您还没有改变您的意见......每当您想使用PHPUnit进行测试时,您必须至少扩展PHPUnit_Framework_TestCase

Little hint: Traits would be good to extract your assertions. 小提示:提取你的断言会很好。

In order to create reusable phpunit assertion in trait. 为了在特质中创建可重用的phpunit断言。 That can be used in many tests cases. 这可以在许多测试案例中使用。 You can use trait that statically calls PHPUnit_Framework_Assert::assertSame , quick example how to achieve it: 您可以使用静态调用PHPUnit_Framework_Assert::assertSame ,快速示例如何实现它:

<?php

namespace Tests\Foo;

use PHPUnit_Framework_Assert;

trait AssertTrait
{
    public function assertInTraitAlwaysFailing()
    {
        PHPUnit_Framework_Assert::assertSame(1, 2, 'always failing');
    }
}

It would act as helper that can be used in many places where you need same assertion to prevent duplication. 它可以作为帮助程序,可以在需要相同断言以防止重复的许多地方使用。

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

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