标签: phpunit

使用PHPUnit作为Symfony 2依赖

我想使用PHPUnit来测试我的Symfony 2应用程序.我composer.json按照文档中的描述将PHPUnit添加到我的Symfony 2项目文件中:

"require": {
        ...
        "phpunit/phpunit": "3.7.*"
    },
Run Code Online (Sandbox Code Playgroud)

我现在如何调用PHPUnit来运行测试?我试图通过它运行它

$ php /path/to/symfony2app/vendor/phpunit/phpunit.php
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误消息:

Warning: require_once(File/Iterator/Autoload.php): failed to open stream: No such file or directory in C:\...\vendor\phpunit\phpunit\PHPUnit\Autoload.php on line 45

Fatal error: require_once(): Failed opening required 'File/Iterator/Autoload.php' (include_path='.;\...\xampp\php\PEAR') in C:\...\vendor\phpunit\phpunit\PHPUnit\Autoload.php on line 45
Run Code Online (Sandbox Code Playgroud)

我想这是PEAR缺失的一个问题,但不应该PEAR是PHPUnit中的依赖项并通过composer update

phpunit cygwin symfony composer-php

1
推荐指数
1
解决办法
2674
查看次数

1
推荐指数
1
解决办法
542
查看次数

模拟对象并保留其他方法的功能

比方说我有这个类:

class Foo {

    public function add($x, $y)
    {
        return $x + $y;
    }

    public function subtract($x, $y)
    {
        return $x - $y;
    }
}
Run Code Online (Sandbox Code Playgroud)

我只想改变add方法的行为:

$mock = $this->getMock('Foo');
$mock->expects($this->once())->method('add')->will($this->returnCallback(function ($x, $y) {
    return ($x + 0) + ($y + 0);
}));

$this->assertEquals(4, $mock->add(2,2));
$this->assertEquals(2, $mock->subtract(4,2));
Run Code Online (Sandbox Code Playgroud)

为什么我的减法方法现在返回null?我期待它表现平常.

Failed asserting that null matches expected 2.
Run Code Online (Sandbox Code Playgroud)

php phpunit mocking

1
推荐指数
1
解决办法
88
查看次数

如果不调用所需的方法,Mockery/Etsy PHPExtensions不会失败测试

我有下面的代码,我希望在运行时失败,因为类DoesNothing不使用mock类或调用任何必需的方法.

<?php
class DoesNothing
{

}

class DoesNothingTest extends YourMockeryTestCase
{
    /**
     * @test
     */
    public function somethingIsCalled()
    {
        $this->mock = Mockery::mock();

        $keys = array(
            '1234',
            'abcxyz',
            '*&(%&^$-*/~@:{}',
            ')*&GA^FAUIB(*',
            '',
            ' ',
        );

        foreach ($keys as $key) {
            $this->mock
                ->shouldReceive('remove')
                ->atLeast()->times(1)
                ->with($key);
        }
        $var = new DoesNothing($this->mock);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,它会通过.我希望它能说"没有调用方法删除"等.

可能有什么不对?与Mockery如何与PHPUnit谈话有关?

谢谢,马丁

编辑:我也提到我们正在使用Etsy的PHPExtensions将它集成到PHPUnit中

php tdd phpunit unit-testing mockery

1
推荐指数
1
解决办法
221
查看次数

功能测试clickAndWait不起作用

我在yii中编写web应用程序,phpunit selenum测试是我登录表单的测试用例

public function testHasLoginForm()
    {
        $this->open('site/login');
        $this->assertTextPresent('Login');

        $this->assertElementPresent('name=LoginForm[username]');
        $this->assertElementPresent('name=LoginForm[password]');

        $this->assertTextPresent('Remember me next time');  
        $this->assertTextPresent('Reset Password?');

        $this->assertElementPresent('name=Login');

        $this->type('name=LoginForm[username]','pradeep@techanveshan.com');
        $this->type('name=LoginForm[password]','password');

        $this->clickAndWait("//input[@value='Login']"); //this line cause the error

    }
Run Code Online (Sandbox Code Playgroud)

一切正常,除非我把这个命令 $ this-> clickAndWait("// input [@ value ='Login']");

这行给了我这样的错误:

Invalid response while accessing the Selenium Server at "http://localhost:4444/s
elenium-server/driver/:" ERROR: Command execution failure. Please search the use
r group at https://groups.google.com/forum/#!forum/selenium-users for error deta
ils from the log window.  The error message is: Value does not implement interfa
ce Event.
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会这样?

php selenium phpunit yii selenium-webdriver

1
推荐指数
1
解决办法
1311
查看次数

phpUnit-测试类中不是测试的函数

我正在phpunit中的报告之间进行一些速度比较,因为我试图找出一个优化问题。

我有一些功能不一定要测试,但是也不属于项目的功能。我使用它们是为了使我的测试小巧易读。我正在使用的函数对传递给我的参数执行cUrl操作。

因此,我正在运行两个Urls(一个项目的两个版本,一个版本以其原始形式,一个版本进行了优化),并查看它们是否返回彼此相等的文本。我不会在应用程序本身中执行此操作。我这样做是因为它比尝试找出正确的函数调用要快,因为该项目有点混乱。

所以我有一个像这样的测试:

public function testOne(){

    $results = $this->testRange(13,1,2013,16,1,2013);
    $this->assertEquals($results['opt'], $results['non_opt']);

}//tests
Run Code Online (Sandbox Code Playgroud)

还有我的两个非测试功能:

protected function testRange($fromDay,
                          $fromMonth,
                          $fromYear,
                          $toDay,
                          $toMonth,
                          $toYear){

    $this->params['periodFromDay'] = $fromDay;
    $this->params['periodFromMonth'] = $fromMonth;
    $this->params['periodFromYear'] = $fromYear;
    $this->params['periodToDay'] = $toDay;
    $this->params['periodToMonth'] = $toMonth;
    $this->params['periodToYear'] = $toYear;

    $this->data['from']=$fromDay."-".$fromMonth."-".$fromYear;
    $this->data['to']=$toDay."-".$toMonth."-".$toYear;;

    return $this->testRunner();

}//testOneDay


protected function testRunner(){

    //include"test_bootstrap.php";
    $response = array();

    foreach($this->types as $key=>$type){

        $params = http_build_query($this->params);
        $url=$this->paths[$type];
        $curl_url = $url."?".$params;
        $ch = curl_init($curl_url);
        $cookieFile = "tmp/cookie.txt";

        if(!file_exists($cookieFile))
        {

            $fh = fopen($cookieFile, "w");
            fwrite($fh, "");
            fclose($fh);

        }//if

        curl_setopt($ch,CURLOPT_COOKIEFILE,$cookieFile);
        curl_setopt($ch,CURLOPT_COOKIEJAR,$cookieFile); …
Run Code Online (Sandbox Code Playgroud)

php phpunit curl

1
推荐指数
1
解决办法
677
查看次数

PHPUnit如何测试两个条件

我是使用PHPUnit的新手,我发现使用assertEquals函数轻松测试是否需要给定值,但是我不确定如何使用多个条件测试值,例如:

function myFunction($foo, $bar, $baz)      
{
    if (($foo != 3) AND ($foo != 5)) {
       // something
    }

    if (($bar < 1) OR ($bar > 10)) {
      // something
    }

    if ( (strlen($baz) === 0) OR (strlen($baz) > 10) ) {
      // something
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以在这些条件下帮助编写单元测试吗?谢谢您的帮助

php phpunit unit-testing

1
推荐指数
2
解决办法
3106
查看次数

Symfony 2功能测试外部URL

无论我做什么,我总是得到一个

Symfony\Component\HttpKernel\Exception\NotFoundHttpException:"找不到[...]的路由"

$crawler->text(),当我尝试请求外部URL时$crawler = $client->request('GET', 'http://anotherdomain.com');.

我想这样做是因为我正在使用另一个virtualHost来渲染一些使用Symfony 1.2的页面,而另一些使用Symfony 2.3.

我也试过

$client = static::createClient(array(), array('HTTP_HOST' => 'anotherdomain.com'));
$client->followRedirects(true);
Run Code Online (Sandbox Code Playgroud)

但它总是试图在Symfony 2中呈现它.

url phpunit web-crawler cross-domain symfony

1
推荐指数
1
解决办法
979
查看次数

assertNotEmpty("0")失败.为什么?

这样做assertNotEmpty("0")PHPUnit中失败.它不应该失败,因为它"0"是一个长度为1的字符串.

以下测试成功.

$this->assertEquals(1, strlen("0"));
$this->assertInternalType('string', "0");
Run Code Online (Sandbox Code Playgroud)

然后,为什么它说"0"是空的?assert语句是否在内部将其转换为整数以检查空虚?

php phpunit assertions

1
推荐指数
1
解决办法
597
查看次数

PHPUnit中的测试用例类型

我是PHPUnit的新手,事实上我今天开始了.而且,据我所读,我开始只了解这个脚本的作用.

class UserTest extends PHPUnit_Framework_TestCase
{
     protected $user;

    // test the talk method
    protected function setUp() {
        $this->user = new User();
        $this->user->setName("Tom");
    }

    protected function tearDown() {
        unset($this->user);
    }

 public function testTalk() {
        $expected = "Hello world!";
        $actual = $this->user->talk();
        $this->assertEquals($expected, $actual);
    }

}
Run Code Online (Sandbox Code Playgroud)

对于这堂课:

<?php
class User {
    protected $name;

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function talk() {
        return "Hello world!";
    }
} 
Run Code Online (Sandbox Code Playgroud)

好的,所以我已经确定测试会根据测试的相等性返回Ok/Fail语句,但我要找的更多.我需要一种实际的方法来测试一个更复杂的类,它的结果,在这个例子中不喜欢,不能轻易猜到.

说,我写了一个执行轮询的脚本.如果方法/类可以工作,我将如何测试或以何种方式测试?上面的代码只显示方法结果是否只会是,'Hello …

php phpunit unit-testing

1
推荐指数
1
解决办法
838
查看次数