From acf0bd91353d3a25b0a9cd81f36fca3cbde8043f Mon Sep 17 00:00:00 2001 From: satoru kimura Date: Thu, 6 Nov 2014 08:22:39 +0900 Subject: [PATCH 1/2] =?UTF-8?q?tests.rst=20=E3=81=AE=E5=8E=9F=E6=96=87?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- best_practices/tests.rst | 114 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 best_practices/tests.rst diff --git a/best_practices/tests.rst b/best_practices/tests.rst new file mode 100644 index 0000000..0bbcbd6 --- /dev/null +++ b/best_practices/tests.rst @@ -0,0 +1,114 @@ +Tests +===== + +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 +---------- + +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`_. + +Functional Tests +---------------- + +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: + +.. best-practice:: + + Define a functional test that at least checks if your application pages + are successfully loading. + +A functional test can be as easy as this: + +.. code-block:: php + + /** @dataProvider provideUrls */ + public function testPageIsSuccessful($url) + { + $client = self::createClient(); + $client->request('GET', $url); + + $this->assertTrue($client->getResponse()->isSuccessful()); + } + + public function provideUrls() + { + 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"*. + +Hardcode URLs in a Functional Test +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Some of you may be asking why the previous functional test doesn't use the URL +generator service: + +.. best-practice:: + + 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: + +.. code-block:: php + + 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. + +Testing JavaScript Functionality +-------------------------------- + +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. + +Learn More about Functional Tests +--------------------------------- + +Consider using `Faker`_ and `Alice`_ libraries to generate real-looking data +for your test fixtures. + +.. _`Faker`: https://github.com/fzaninotto/Faker +.. _`Alice`: https://github.com/nelmio/alice +.. _`PhpUnit`: https://phpunit.de/ +.. _`PhpSpec`: http://www.phpspec.net/ +.. _`Mink`: http://mink.behat.org +.. _`smoke testing`: http://en.wikipedia.org/wiki/Smoke_testing_(software) From e4d42dc1f06726528a74e03379b2c4a69b4321a2 Mon Sep 17 00:00:00 2001 From: satoru kimura Date: Thu, 6 Nov 2014 08:24:41 +0900 Subject: [PATCH 2/2] =?UTF-8?q?tests.rst=20=E3=81=AE=E7=BF=BB=E8=A8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- best_practices/tests.rst | 75 ++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 49 deletions(-) diff --git a/best_practices/tests.rst b/best_practices/tests.rst index 0bbcbd6..82e07c5 100644 --- a/best_practices/tests.rst +++ b/best_practices/tests.rst @@ -1,32 +1,23 @@ -Tests +テスト ===== -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. +大まかに言うと、テストには2つの種類が存在します。ユニットテストは、特定の関数の入力と出力をテストすることができます。機能テストは、あなたが閲覧するサイト上のページに対して "ブラウザ" を制御し、リンクをクリックしたり、フォームに記入を行ったり、ページ上の特定の物事を確認することができます。 -Unit Tests +ユニットテスト ---------- -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`_. +ユニットテストは、Symfonyから独立したクラス内に存在すべき、あなたの "ビジネスロジック" をテストするために使用されます。この理由により、Symfonyは実際のところ、ユニットテストのためにあなたがどのツールを使用するかについて意見を持ちません。しかしながら、最もポピュラーなツールは `PhpUnit`_ と `PhpSpec`_ です。 -Functional Tests ----------------- +機能テスト +---------- -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: +真に優れた機能テストを作成することは困難であるため、一部の開発者は完全にこれを省略します。機能テストを省略しないでください!いくつかの *シンプルな* 機能テストを定義することで、いかなる大きなエラーであれ、それらをデプロイしてしまう前にすばやく発見できます。 .. best-practice:: - Define a functional test that at least checks if your application pages - are successfully loading. + 機能テストを定義することは、結局のところあなたのアプリケーションのページが正常にロードにされるかどうかチェックすることです。 -A functional test can be as easy as this: +機能テストは以下の例のように簡単です: .. code-block:: php @@ -51,28 +42,20 @@ A functional test can be as easy as this: ); } -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. +このコードは全ての与えられたURLが正しくロードできることをチェックし、それは各HTTPレスポンスのステータスコードが ``200`` から ``299`` の間であることを意味します。 +これは有用に見えないかもしれませんが、かかる労力がいかに少ないかを考慮すると、あなたのアプリケーションで行う価値があります。 -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"*. +コンピュータソフトウェアにおいて、この種のテストは `smoke testing`_ と呼ばれ、 *「将来のソフトウェアリリースを退けるほど深刻な、単純な失敗を発見にするための予備的なテスト」* で構成されます。 -Hardcode URLs in a Functional Test -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +機能テスト内のハードコードされたURL +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Some of you may be asking why the previous functional test doesn't use the URL -generator service: +皆さんのうち一部の方は何故、上記の機能テストがURLジェネレータサービスを使用していないのかを尋ねるかもしれません: .. best-practice:: + 機能テストにおいては、URLジェネレータを使用する代わりに、URLのハードコードが使用されます。 - 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: +テストページのURLを生成するために ``router`` サービスを使用した、以下の機能テストについて考えてみましょう。 .. code-block:: php @@ -85,26 +68,20 @@ generate the URL of the tested page: // ... } -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. +これは動作するでしょうが、一つ大きな欠点があります。もし開発者が ``blog_archives`` ルートのパスを誤って変更した場合、テストは未だ成功しますが、元の(古い)URLは動作しなくなるでしょう!これは、そのURLに対する全てのブックマークが壊れ、あなたが全ての検索エンジンのページランキングを失うことを意味します。 + +JavaScriptの機能テスト +~~~~~~~~~~~~~~~~~~~~~~ -Testing JavaScript Functionality --------------------------------- +組み込みの機能テストクライアントは素晴らしいですが、それをあなたのページで、任意のJavaScriptの振る舞いをテストするために使用することはできません。もしあなたがこのテストを行う必要がある場合、PHPUnitの内部から、 `Mink`_ ライブラリを使用することを検討してください。 -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. +もちろん、もしもあなたが大規模なJavaScriptフロントエンドを使用している場合、純粋なJavaScriptベースのテストツールを検討すべきです。 -Of course, if you have a heavy JavaScript frontend, you should consider using -pure JavaScript-based testing tools. +機能テストについてさらに学ぶ +~~~~~~~~~~~~~~~~~~~~~~~~~~ -Learn More about Functional Tests ---------------------------------- +`Faker`_ と `Alice`_ ライブラリを使用して、あなたのテストフィクスチャのため実運用に近いデータを生成することを検討してください。 -Consider using `Faker`_ and `Alice`_ libraries to generate real-looking data -for your test fixtures. .. _`Faker`: https://github.com/fzaninotto/Faker .. _`Alice`: https://github.com/nelmio/alice