Codeception 多个测试,1 个脚本


Codeception multiple tests, 1 script

我想我可能弄错了概念或没有正确思考某事。我正在寻找一种连接到数据库的方法,然后对表的每一行运行硒测试(在 phantomjs 中)。该测试是检查定制CMS上的损坏图像,并且可以应用于任何CMS。

我基本上想通过从数据库加载它们的 ID,然后为每个 ID 运行单独的测试来为每个页面(特定类型)运行验收测试。

这是我到目前为止所拥有的:

$I = new WebGuy($scenario);
$results = $I->getArrayFromDB('talkthrough', '`key`', array());
foreach ($results as $r) {
    $I->wantTo('Check helpfile '.$r['key'].'for broken images');
    $I->amOnPage('/talkThrough.php?id='.$r['key']);
    $I->seeAllImages();
}

这在某种程度上是有效的,因为它一直执行到第一次失败(因为它作为具有许多断言的 1 个测试运行)。

如何使其作为单个测试运行?

我最终循环访问并将失败的密钥存储在逗号分隔的字符串中,并设置一个布尔值来表示发现失败。

$I = new WebGuy($scenario);
$results = $I->getArrayFromDB('talkthrough', '`key`', array());
$failures = "Broken help files are: ";
$failures_found = false;
foreach ($results as $key => $r) {
   $I->wantTo('Check helpfile '.$r['key'].'for broken images');
   $I->amOnPage('/talkThrough.php?id='.$r['key']);
   $allImagesFine = $I->checkAllImages();
   if($allImagesFine != '1')
   {
       $fail = $r['key'].",";
       $failures.= $fail;
       $failures_found = true;
   }
}
$I->seeBrokenImages($failures_found,$failures);

以下作为我的网络助手

<?php
namespace Codeception'Module;
// here you can define custom functions for WebGuy 
class WebHelper extends 'Codeception'Module
{
    function checkAllImages()
    {
        $result = $this->getModule('Selenium2')->session->evaluateScript("return     (function(){ return Array.prototype.slice.call(document.images).every(function (img) {return img.complete && img.naturalWidth > 0;}); })()");
        return $result;
    }
    function getArrayFromDB($table, $column, $criteria = array())
    {
        $dbh = $this->getModule('Db');
        $query = $dbh->driver->select($column, $table, $criteria);
        $dbh->debugSection('Query', $query, json_encode($criteria));
        $sth = $dbh->driver->getDbh()->prepare($query);
        if (!$sth) 'PHPUnit_Framework_Assert::fail("Query '$query' can't be executed.");
        $sth->execute(array_values($criteria));
        return $sth->fetchAll();
    }
    function seeBrokenImages($bool,$failArray)
    {
        $this->assertFalse($bool,$failArray);
    }
}

感谢您提交的答案

这是行不通的。请避免在测试中使用循环和条件。您应该手动放置key。而不是从数据库中获取它们。因为它引入了额外的复杂性。

这可能不是最好的设计选择,但如果你真的想遵循这种方法,你可以使用codeception中的指定工具,以便允许你的测试继续运行,即使一个断言失败:https://github.com/Codeception/Specify