有没有办法在一个内部进行测试 TestCase 按一定的顺序运行?例如,我想将对象的生命周期从创建到使用再到销毁分开,但我需要确保在运行其他测试之前先设置该对象。

有帮助吗?

解决方案

也许您的测试中存在设计问题。

通常每个测试不得依赖于任何其他测试,因此它们可以按任何顺序运行。

每个测试都需要实例化并销毁它运行所需的所有内容,这将是完美的方法,您永远不应该在测试之间共享对象和状态。

您能否更具体地说明为什么 N 次测试需要相同的对象?

其他提示

PHPUnit 通过以下方式支持测试依赖项 @依靠 注解。

以下是文档中的一个示例,其中测试将按照满足依赖关系的顺序运行,每个依赖测试将一个参数传递给下一个:

class StackTest extends PHPUnit_Framework_TestCase
{
    public function testEmpty()
    {
        $stack = array();
        $this->assertEmpty($stack);

        return $stack;
    }

    /**
     * @depends testEmpty
     */
    public function testPush(array $stack)
    {
        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertNotEmpty($stack);

        return $stack;
    }

    /**
     * @depends testPush
     */
    public function testPop(array $stack)
    {
        $this->assertEquals('foo', array_pop($stack));
        $this->assertEmpty($stack);
    }
}

然而,重要的是要注意,具有未解决的依赖关系的测试将 不是 被执行(理想的,因为这可以快速引起对失败测试的关注)。因此,在使用依赖项时必须密切注意。

正确的答案是用于测试的正确配置文件。我遇到了同样的问题,并通过使用必要的测试文件顺序创建测试套件来修复它:

phpunit.xml:

<phpunit
        colors="true"
        bootstrap="./tests/bootstrap.php"
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="true"
        convertWarningsToExceptions="true"
        strict="true"
        stopOnError="false"
        stopOnFailure="false"
        stopOnIncomplete="false"
        stopOnSkipped="false"
        stopOnRisky="false"
>
    <testsuites>
        <testsuite name="Your tests">
            <file>file1</file> //this will be run before file2
            <file>file2</file> //this depends on file1
        </testsuite>
    </testsuites>
</phpunit>

如果您希望测试共享各种辅助对象和设置,您可以使用 setUp(), tearDown() 添加到 sharedFixture 财产。

PHPUnit 允许使用“@depends”注释,该注释指定依赖测试用例并允许在依赖测试用例之间传递参数。

在我看来,采用以下场景,我需要测试特定资源的创建和销毁。

最初我有两种方法,a。测试创建资源和b。测试销毁资源

A。测试创建资源

<?php
$app->createResource('resource');
$this->assertTrue($app->hasResource('resource'));
?>

b.测试销毁资源

<?php
$app->destroyResource('resource');
$this->assertFalse($app->hasResource('resource'));
?>

我认为这是一个坏主意,因为 testDestroyResource 依赖于 testCreateResource。更好的做法是

A。测试创建资源

<?php
$app->createResource('resource');
$this->assertTrue($app->hasResource('resource'));
$app->deleteResource('resource');
?>

b.测试销毁资源

<?php
$app->createResource('resource');
$app->destroyResource('resource');
$this->assertFalse($app->hasResource('resource'));
?>

替代解决方案:在测试中使用 static(!) 函数来创建可重用元素。例如(我使用 selenium IDE 来记录测试,并使用 phpunit-selenium (github) 在浏览器内运行测试)

class LoginTest extends SeleniumClearTestCase
{
    public function testAdminLogin()
    {
        self::adminLogin($this);
    }

    public function testLogout()
    {
        self::adminLogin($this);
        self::logout($this);
    }

    public static function adminLogin($t)
    {
        self::login($t, 'john.smith@gmail.com', 'pAs$w0rd');
        $t->assertEquals('John Smith', $t->getText('css=span.hidden-xs'));
    }

    // @source LoginTest.se
    public static function login($t, $login, $pass)
    {
        $t->open('/');
        $t->click("xpath=(//a[contains(text(),'Log In')])[2]");
        $t->waitForPageToLoad('30000');
        $t->type('name=email', $login);
        $t->type('name=password', $pass);
        $t->click("//button[@type='submit']");
        $t->waitForPageToLoad('30000');
    }

    // @source LogoutTest.se
    public static function logout($t)
    {
        $t->click('css=span.hidden-xs');
        $t->click('link=Logout');
        $t->waitForPageToLoad('30000');
        $t->assertEquals('PANEL', $t->getText("xpath=(//a[contains(text(),'Panel')])[2]"));
    }
}

好的,现在,我可以在其他测试中使用这个可重用元素:) 例如:

class ChangeBlogTitleTest extends SeleniumClearTestCase
{
    public function testAddBlogTitle()
    {
      self::addBlogTitle($this,'I like my boobies');
      self::cleanAddBlogTitle();
    }

    public static function addBlogTitle($t,$title) {
      LoginTest::adminLogin($t);

      $t->click('link=ChangeTitle');
      ...
      $t->type('name=blog-title', $title);
      LoginTest::logout($t);
      LoginTest::login($t, 'paris@gmail.com','hilton');
      $t->screenshot(); // take some photos :)
      $t->assertEquals($title, $t->getText('...'));
    }

    public static function cleanAddBlogTitle() {
        $lastTitle = BlogTitlesHistory::orderBy('id')->first();
        $lastTitle->delete();
    }
  • 通过这种方式,您可以构建测试的层次结构。
  • 您可以保持每个测试用例完全独立的属性(如果您在每次测试后清理数据库)。
  • 最重要的是,如果将来登录方式发生变化,您只需修改 LoginTest 类,并且其他测试中不需要正确的登录部分(它们应该在更新 LoginTest 后工作):)

当我运行测试时,我的脚本会清理数据库并开始。上面我用我的 SeleniumClearTestCase 类(我在那里制作了 snapshot() 和其他不错的函数)它是 MigrationToSelenium2 (来自github,使用seleniumIDE + ff插件“Selenium IDE:在firefox中移植记录的测试:PHP Formatters" )是我的类 LaravelTestCase 的扩展(它是 Illuminate\Foundation esting estCase 的副本,但不扩展 PHPUnit_Framework_TestCase),当我们想要在测试结束时清理数据库时,它设置 laravel 可以访问 eloquent),这是PHPUnit_Extensions_Selenium2TestCase 的扩展。为了设置 laravel eloquent,我还在 SeleniumClearTestCase 函数 createApplication 中(在 setUp, ,我从 laral test/TestCase 中获取这个函数)

如果您的测试需要按特定顺序运行,那么您的测试确实存在问题。每个测试应该完全独立于其他测试:它可以帮助您定位缺陷,并允许您获得可重复(因此可调试)的结果。

查看 这个网站 获取大量想法/信息,了解如何以避免此类问题的方式考虑测试。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top