在 PHPUnit 中测试工作链接


Testing for working links in PHPUnit

我是PHPUnit的新手,对于我的第一个自制测试用例,我想通过提供这样的PHPUnit链接来检查网站是否有图像。

让我们调用链接我想测试http://somedomain.com/images/images.jpg

现在,我

写了这个函数,我知道我可能做错了,但这就是我在这里问它的原因。

class WebsiteHasImage{
    public function images(){
        $website = 'http://somedomain.com/images/img.jpg';
        return $website;
    }
}

class WebsiteHasImageTest extends PHPUnit_Framework_TestCase
{
    public function testSuccess(){
      $this->assertTrue($this->images(), TRUE);
    }
}

 Call to undefined method WebsiteHasImageTest::images() in ... on line 15

第 15 行是这样的。 $this->assertTrue($this->images(), TRUE);

你的类将为images()返回一个字符串,这不是你真正要测试的。

class WebsiteHasImage{
    public function images(){
        $website = 'http://somedomain.com/images/img.jpg';
        return $website;
    }
}
class WebsiteHasImageTest extends PHPUnit_Framework_TestCase
{
    public function testSuccess()
    {
        WebSiteImage = new WebSiteHasImage();
        $this->assertEquals('http://somedomain.com/images/img.jpg', $WebSiteImage->images(), 'Check returned File Name is correct');
    }
}

所做的只是测试图像字符串是否正确。

您可以尝试通过另一个调用来读取图像,然后进行比较,但这需要您的网站已启动。

或者,调用可以查看当前应用程序目录中的 images/img.jpg并执行一些测试(字节大小正确等)来验证图像。

您可以尝试阅读网站并返回内容,但同样,这需要网站。 因此,为了测试您的代码,您有一个返回网站数据的类,然后模拟该类以返回一个固定字符串,然后您将针对该类编写测试以查看类中的代码是否正确提取图像。 然后,您的类可以测试图像是否存在,并再次模拟它以始终返回它以测试您的代码的作用。

public function GetWebPageContents($HTTPAddress)
{
    $HTML = ...    // Code that reads the contents
    return $HTML;
}
public function GetImageList($HTML)
{
    $ImageList = array();
    ...      // Code to find all images, adding to ImageList
    return $ImageList;
}

测试类添加

public function testGetWebPageContents()
{
$HTMLText = '<html><body><img scr=""></img></body><</html>';        // Fill this with a sample webpage text
    $MockClass = $this->getMock('WebsiteHasImage', array('GetWebPageContents'));
    // Set up the expectation for the GetWebPageContents() method 
    $MockClass->expects($this->any())
                ->method('GetWebPageContents')
                ->will($this->returnValue($HMTLText));
    // Create Test Object
    $TestClass = new MockClass();
    $this->assertEquals($HTMLText, $TestClass->GetWebPageContents('http://testaddress.com'), 'Validate Mock HTML return'); // Address is garbage as it does not matter since we mocked it
    $WebsiteTest = new WebsiteHasImage();
    $ImageList = $WebsiteTest->GetImageList($HTMLText);
    $this->assertEquals(1, count($ImageList), 'Count Total Number of Images');
    $this->assertEquals('images/img.jpg', $ImageList[0], 'Check actual image file name from dummy HTML parsing');
    ...       // More tests for contents
}

现在,最好的变化是使用类中的读取数据,为此,在从网站读取HTML代码后,您将HTML代码正常传递给类。 在测试中,您将使用构造函数中的 Mock 对象或通过 Set 传递执行读取的对象。 在谷歌上搜索依赖注入、模拟对象等......以查找有关此主题的更多信息。

我自己没有使用过PHPunit,但是查看您的代码$this->images()不起作用,因为您在WebsiteHasImageTest类中没有图像方法。

<?php
class WebsiteHasImageTest extends PHPUnit_Framework_TestCase
{
    public function images(){
        $website = 'http://somedomain.com/images/img.jpg';
        return $website;
    }
    public function testSuccess(){
      $this->assertTrue($this->images(), TRUE);
    }
}

这将消除对WebsiteHasImage的需求,但如果需要,您可以这样做。

<?php
class WebsiteHasImage{
    public function images(){
        $website = 'http://somedomain.com/images/img.jpg';
        return $website;
    }
}
class WebsiteHasImageTest extends PHPUnit_Framework_TestCase
{
    public function testSuccess(){
      $images = new WebsiteHasImage();
      $this->assertTrue($images->images(), TRUE);
    }
}

所以它只是归结为:a) 将方法移动到WebsiteHasImageTest类或b) 在 WebsiteHasImageTest testSuccess 方法中启动一个新的WebsitehasImage()对象。