在外部执行Laravel单元测试

时间:2017-01-30 06:30:33

标签: laravel phpunit

我在Laravel项目中编写了一些单元测试,并尝试通过第三方应用程序执行它们。我可以在phpunit的命令行中执行这些测试,但我的要求是在外部运行它们(自动化)。这有可能吗?在外部运行它时,我的自定义类/ Laravel助手类似乎无法访问。

use App\ZZZ\Scrape;
use App\ZZZ\Theme1;
use App\ZZZ\Theme2;

class ScrapeTest extends TestCase
{
protected static $scrape;
protected static $sxe;
protected static $theme;
protected static $url;
protected static $section;
protected static $extArray;
protected static $intArray;
protected static $safetyArray;
protected static $perfArray;
protected static $output;

public static function setUpBeforeClass()
{
    Config::set('aws.s3Path', 'zz/test/');
    self::$url = "http://www.testsite.com.au";
    self::$section = "testSection";
    self::$scrape = new Scrape(self::$url, self::$section);
    self::$scrape->html = self::$scrape->getHTML();
    self::$sxe = self::$scrape->getSimpleXmlElement();

    if(self::$scrape->isDarkTheme(self::$sxe))
    {
        self::$theme = new Theme1(self::$url,self::$section);
    }
    else
        self::$theme = new Theme2(self::$url,self::$section);

    self::$output = new stdClass();

    //exterior features
    self::$extArray = self::$theme->getFeatureDetails(self::$sxe, "exterior");
    self::$output->ExteriorFeatures = new stdClass();
    self::$output->ExteriorFeatures->Feature = self::$extArray;

    //interior features
    self::$intArray = self::$theme->getFeatureDetails(self::$sxe, "interior");
    self::$output->InteriorFeatures = new stdClass();
    self::$output->InteriorFeatures->Feature = self::$intArray;


}

public function testGetLeadingText()
{
    $result = self::$scrape->getLeadingText(self::$sxe);
    $this->assertNotNull($result, "No leading text");
}

public function testFetchColours()
{
    $result = self::$scrape->FetchColours();
    $this->assertNotNull($result, "Error with decoding colour API");
    $this->assertInternalType('array', $result->data, "FetchAPI  output is not an array");
    $this->assertNotSame(null, $result->data, "FetchAPI  output is an empty array");
 }  

}

我得到的一些错误:

Class' TestCase'在C中找不到:... \ ScrapeTest.php

Class' Config'在C中找不到:... \ ScrapeTest.php

Class' App \ ZZZ \ Scrape'在C中找不到:... \ ScrapeTest.php ...

非常感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的测试文件似乎不完整。

您需要声明存在测试类和测试用例的名称空间。

例如。如果在您的情况下,测试用例位于“测试”中,而测试文件位于“ Tests \ ZZZ”中,则在文件开头使用它。

namespace Tests\ZZZ;

use Tests\TestCase;