天天看點

symfony3.4中使用PHPunit

symfony3.4 phpunit test.

參考位址:http://symfony.com/doc/3.4/testing.html

參考位址: http://symfony.com/doc/current/testing.html#functional-tests

測試分類

  1. unit test
  2. functional test

前提條件:安裝PHPunit。

一、Unit Test

1. 安裝PHPunit-bridge

[[email protected] symfony_test]# composer require --dev symfony/phpunit-bridge
...
           

2. 建立測試檔案

參照官方文檔

3. 測試

# run all tests of the application
 ./vendor/bin/simple-phpunit

# run all tests in Util directory.
 ./vendor/bin/simple-phpunit tests/AppBundle/Util

# run tests for the Calculator.php class
 ./vendor/bin/simple-phpunit tests/AppBundle/Util/CalculatorTest.php

# run all tests for the entire Bundle
 ./vendor/bin/simple-phpunit tests/AppBundle/

[[email protected] symfony_test]# ./vendor/bin/simple-phpunit tests/AppBundle/Controller/UserControllerTest.php
           
[[email protected] symfony_test]# vim ./vendor/bin/simple-phpunit 
[[email protected] symfony_test]# vim ./vendor/bin/simple-phpunit --help
...
[[email protected] symfony_test]# 
           

二、functional test

functional test 測試方式和unit測試一緻。

<?php

namespace Tests\AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DefaultControllerTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();

        $crawler = $client->request('GET', '/');

        $this->assertEquals(, $client->getResponse()->getStatusCode());
        $this->assertContains('Welcome to Symfony', $crawler->filter('#container h1')->text());
    }
}
           
在導入類的時候,很容易導入Symfony\Bundle\FrameworkBundle\Tests\Functional\WebTestCase導緻錯誤