概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:26:20 | 236 次阅读 | 评论: 0 | 来源: 网络整理

Roughly speaking, there are two types of test. Unit testing allows you to test the input and output of specific functions. Functional testing allows you to command a “browser” where you browse to pages on your site, click links, fill out forms and assert that you see certain things on the page.

单元测试

Unit tests are used to test your “business logic”, which should live in classes that are independent of Symfony. For that reason, Symfony doesn’t really have an opinion on what tools you use for unit testing. However, the most popular tools are PhpUnit and PhpSpec.

功能测试

Creating really good functional tests can be tough so some developers skip these completely. Don’t skip the functional tests! By defining some simple functional tests, you can quickly spot any big errors before you deploy them:

最佳实践

Define a functional test that at least checks if your application pages are successfully loading.

A functional test can be as easy as this:

// src/AppBundle/Tests/ApplicationAvailabilityFunctionalTest.php
namespace AppBundleTests;

use SymfonyBundleFrameworkBundleTestWebTestCase;

class ApplicationAvailabilityFunctionalTest extends WebTestCase
{
    /**
     * @dataProvider urlProvider
     */
    public function testPageIsSuccessful($url)
    {
        $client = self::createClient();
        $client->request('GET', $url);

        $this->assertTrue($client->getResponse()->isSuccessful());
    }

    public function urlProvider()
    {
        return array(
            array('/'),
            array('/posts'),
            array('/post/fixture-post-1'),
            array('/blog/category/fixture-category'),
            array('/archives'),
            // ...
        );
    }
}

This code checks that all the given URLs load successfully, which means that their HTTP response status code is between 200 and 299. This may not look that useful, but given how little effort this took, it’s worth having it in your application.

In computer software, this kind of test is called smoke testing and consists of “preliminary testing to reveal simple failures severe enough to reject a prospective software release”.

在功能测试里指定具体URL

Some of you may be asking why the previous functional test doesn’t use the URL generator service:

最佳实践

Hardcode the URLs used in the functional tests instead of using the URL generator.

Consider the following functional test that uses the router service to generate the URL of the tested page:

public function testBlogArchives()
{
    $client = self::createClient();
    $url = $client->getContainer()->get('router')->generate('blog_archives');
    $client->request('GET', $url);

    // ...
}

This will work, but it has one huge drawback. If a developer mistakenly changes the path of the blog_archives route, the test will still pass, but the original (old) URL won’t work! This means that any bookmarks for that URL will be broken and you’ll lose any search engine page ranking.

测试Javascript代码

The built-in functional testing client is great, but it can’t be used to test any JavaScript behavior on your pages. If you need to test this, consider using the Mink library from within PHPUnit.

Of course, if you have a heavy JavaScript frontend, you should consider using pure JavaScript-based testing tools.

更多信息

Consider using Faker and Alice libraries to generate real-looking data for your test fixtures.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务