Open fr-sato opened 10 years ago
本課題は工数見積計13.25時間で行います。
課題No. | タスク | 見積時間(h) |
---|---|---|
課題1 | インストール | 0.25 |
課題1 | テスト記述 | 1.5 |
課題2 | 調査・まとめ | 2.5 |
課題3 | テスト項目洗い出し | 1 |
課題3 | テスト記述 | 3 |
課題3 | デバッグ | 3 |
バッファ | 2 | |
計 | 13.25 |
$ wget https://phar.phpunit.de/phpunit.phar
--2014-08-05 04:54:02-- https://phar.phpunit.de/phpunit.phar
Resolving phar.phpunit.de (phar.phpunit.de)... 188.94.27.25
Connecting to phar.phpunit.de (phar.phpunit.de)|188.94.27.25|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3225296 (3.1M) [application/octet-stream]
Saving to: ‘phpunit.phar’
100%[=========================================================================================>] 3,225,296 913KB/s in 3.5s
2014-08-05 04:54:08 (913 KB/s) - ‘phpunit.phar’ saved [3225296/3225296]
$ chmod +x phpunit.phar
$ sudo mv phpunit.phar /usr/local/bin/phpunit
$ cd /usr/local/bin/
/usr/local/bin
$ ls -al
drwxr-xr-x 2 root root 4096 Aug 5 04:55 .
drwxr-xr-x 15 root root 4096 Mar 19 08:07 ..
-rwxr-xr-x 1 root root 458 Mar 19 06:53 chef-apply
-rwxr-xr-x 1 root root 459 Mar 19 06:53 chef-client
-rwxr-xr-x 1 root root 468 Mar 19 06:53 chef-service-manager
-rwxr-xr-x 1 root root 458 Mar 19 06:53 chef-shell
-rwxr-xr-x 1 root root 457 Mar 19 06:53 chef-solo
-rwxr-xr-x 1 root root 472 Mar 19 06:53 chef-zero
-rwxr-xr-x 1 root root 464 Mar 19 06:53 coderay
-rwxr-xr-x 1 root root 464 Jun 5 03:09 compass
-rwxr-xr-x 1 root root 1006588 Mar 19 09:48 composer
-rwxr-xr-x 1 root root 4399 Mar 19 06:51 erb
-rwxr-xr-x 1 root root 460 Mar 19 06:53 erubis
-rwxr-xr-x 1 root root 548 Mar 19 06:51 gem
-rwxr-xr-x 1 root root 471 Mar 19 06:53 htmldiff
-rwxr-xr-x 1 root root 320 Mar 19 06:51 irb
-rwxr-xr-x 1 root root 453 Mar 19 06:53 knife
-rwxr-xr-x 1 root root 3164435 Mar 19 09:48 laravel
-rwxr-xr-x 1 root root 468 Mar 19 06:53 ldiff
-rwxr-xr-x 1 root root 452 Mar 19 06:53 ohai
-rwxr-xr-x 1 root root 2363 Mar 19 07:27 pcre-config
-rwxr-xr-x 1 root root 87553 Mar 19 07:27 pcregrep
-rwxr-xr-x 1 root root 177896 Mar 19 07:27 pcretest
-rwxr-xr-x 1 root root 817 Mar 19 08:08 pear
-rwxr-xr-x 1 root root 838 Mar 19 08:08 peardev
-rwxr-xr-x 1 root root 754 Mar 19 08:08 pecl
lrwxrwxrwx 1 root root 24 Mar 19 08:08 phar -> /usr/local/bin/phar.phar
-rwxr-xr-x 1 root root 14823 Mar 19 08:08 phar.phar
-rwxr-xr-x 1 root root 28324813 Mar 19 08:07 php
-rwxr-xr-x 1 root root 28238508 Mar 19 08:07 php-cgi
-rwxr-xr-x 1 root root 2591 Mar 19 08:08 php-config
-rwxr-xr-x 1 root root 4522 Mar 19 08:08 phpize
-rwxrwxr-x 1 ec2-user ec2-user 3225296 Jul 18 07:16 phpunit
-rwxr-xr-x 1 root root 448 Mar 19 06:53 pry
-rwxr-xr-x 1 root root 452 Mar 19 06:53 puma
-rwxr-xr-x 1 root root 455 Mar 19 06:53 pumactl
-rwxr-xr-x 1 root root 454 Mar 19 06:53 rackup
-rwxr-xr-x 1 root root 1233 Mar 19 06:51 rake
-rwxr-xr-x 1 root root 452 Mar 19 06:52 rdoc
-rwxr-xr-x 1 root root 1716874 Mar 19 07:50 re2c
-rwxr-xr-x 1 root root 479 Mar 19 06:53 restclient
-rwxr-xr-x 1 root root 450 Mar 19 06:52 ri
-rwxr-xr-x 1 root root 8023186 Mar 19 06:51 ruby
-rwxr-xr-x 1 root root 452 Jun 5 03:09 sass
-rwxr-xr-x 1 root root 460 Jun 5 03:09 sass-convert
-rwxr-xr-x 1 root root 452 Jun 5 03:09 scss
-rwxr-xr-x 1 root root 452 Mar 19 06:53 shef
-rwxr-xr-x 1 root root 300 Mar 19 06:51 testrb
# gem install rspec
Fetching: rspec-support-3.0.3.gem (100%)
Fetching: rspec-core-3.0.3.gem (100%)
Fetching: rspec-expectations-3.0.3.gem (100%)
Fetching: rspec-mocks-3.0.3.gem (100%)
Fetching: rspec-3.0.0.gem (100%)
Successfully installed rspec-support-3.0.3
Successfully installed rspec-core-3.0.3
Successfully installed rspec-expectations-3.0.3
Successfully installed rspec-mocks-3.0.3
Successfully installed rspec-3.0.0
5 gems installed
Installing ri documentation for rspec-support-3.0.3...
Installing ri documentation for rspec-core-3.0.3...
Installing ri documentation for rspec-expectations-3.0.3...
Installing ri documentation for rspec-mocks-3.0.3...
Installing ri documentation for rspec-3.0.0...
Installing RDoc documentation for rspec-support-3.0.3...
Installing RDoc documentation for rspec-core-3.0.3...
Installing RDoc documentation for rspec-expectations-3.0.3...
Installing RDoc documentation for rspec-mocks-3.0.3...
Installing RDoc documentation for rspec-3.0.0...
課題1:テスト実行結果(PHPUnit)
~/public_html/training/44_test on feature/44_test[!?]
$ phpunit tests/AdderTest
PHPUnit 4.1.4 by Sebastian Bergmann.
...
Time: 77 ms, Memory: 2.75Mb
OK (3 tests, 3 assertions)
工数見積やり直します。 本課題は工数見積計20時間で行います。
課題No. | タスク | 見積時間(h)旧 | 新 |
---|---|---|---|
課題1 | インストール | 0.25 | 1(実際にかかった時間) |
課題1 | テスト記述 | 1.5 | 5(うちPHPUnitテストまでで2時間) |
課題2 | 調査・まとめ | 2.5 | 3 |
課題3 | テスト項目洗い出し | 1 | 1 |
課題3 | テスト記述⇔修正 | 6 | 7 |
バッファ | 2 | 3 | |
計 | 13.25 | 20 |
実行結果こちらです。 仕様のようですが、このOKの信じられなさをどうにかする方法はないでしょうか。 http://phpunit.de/manual/4.2/ja/incomplete-and-skipped-tests.html
~/public_html/training/44_test on feature/44_test[!?]
$ phpunit --color tests/AdderTest.php
PHPUnit 4.1.4 by Sebastian Bergmann.
...
Time: 56 ms, Memory: 2.75Mb
OK (3 tests, 3 assertions)
http://phpunit.de/manual/current/ja/writing-tests-for-phpunit.html
この辺の、OKな場合と失敗した場合の出力例を見てみるのと、3 tests, 3 assertions
あたりの意味を調べてみるとよいと思います。
これでテストがパスする場合はテスト自体が実行されてないことになる、とのこと。
$ phpunit tests/AdderTest.php
PHPUnit 4.1.4 by Sebastian Bergmann.
FFF
Time: 52 ms, Memory: 2.50Mb
There were 3 failures:
1) AdderTest::testAdd with data set #0 (1, 2, 3)
/home/ec2-user/public_html/training/44_test/tests/AdderTest.php:19
2) AdderTest::testAdd with data set #1 (1, -2, -1)
/home/ec2-user/public_html/training/44_test/tests/AdderTest.php:19
3) AdderTest::testAdd with data set #2 (1, 1.5, 2.5)
/home/ec2-user/public_html/training/44_test/tests/AdderTest.php:19
FAILURES!
Tests: 3, Assertions: 0, Failures: 3.
Tests
:テスト実行回数
Assertions
:実行結果の検証(アサーション)が行われた回数
Failures
:失敗したテストの数
という認識でよろしいでしょうか…。
プロダクトコードに意図的に不具合を混入してみて、それでもテストがパスするようであればテスト側に間違いがある。
$ phpunit tests/AdderTest.php
PHPUnit 4.1.4 by Sebastian Bergmann.
FFF
Time: 56 ms, Memory: 3.00Mb
There were 3 failures:
1) AdderTest::testAdd with data set #0 (1, 2, 3)
Failed asserting that 2 matches expected 3.
/home/ec2-user/public_html/training/44_test/tests/AdderTest.php:19
2) AdderTest::testAdd with data set #1 (1, -2, -1)
Failed asserting that -2 matches expected -1.
/home/ec2-user/public_html/training/44_test/tests/AdderTest.php:19
3) AdderTest::testAdd with data set #2 (1, 1.5, 2.5)
Failed asserting that 1.5 matches expected 2.5.
/home/ec2-user/public_html/training/44_test/tests/AdderTest.php:19
FAILURES!
Tests: 3, Assertions: 3, Failures: 3.
$ phpunit tests/AdderTest.php
PHPUnit 4.1.4 by Sebastian Bergmann.
...
Time: 65 ms, Memory: 2.75Mb
OK (3 tests, 3 assertions)
class Bird {
public function isBird() {
return true;
}
public function canFly() {
return true;
}
}
class Penguin extends Bird {
public function canFly() {
return false;
}
}
class PenguinTest extends PHPUnit_Framework_Testcase
{
public function setUp()
{
$penguin = new Penguin;
}
//testIsBirdは省略
public funtion testCanFly()
{
$this->assertFalse($this->penguin->canFly());
}
}
class DescribePenguin extends \PHPSpec\Context
{
//itShouldBeBirdは省略
public function itShouldNotCanFly()
{
$penguin = new Penguin;
$this->spec($penguin)->shouldNot->canFly();
}
}
確認しました、OKです。
@fr-sasaki 確認よろしくです。
※果てしなくある気がしてなりません。
$form_data_trimmed
(配列。POSTデータから前後の空白を除いた入力値が入っている)required
:対象にデータが存在するか?max
:全角49字・50字・51字で試してそれぞれバリデータが弾くか否か?regex_full_width_chars
:全角文字の正規表現に対象の文字列がマッチするか?regex
(郵便番号のバリデートで使用):数値のみの正規表現にマッチするか?size
:n-1字, n字, n+1字の入力でn字のみバリデータが通るか?exists
:DBに指定カラムが存在するか?email
:emailの書式に合う|合わない値を入れてみて通るか?requied_if
:特定のキーに期待する値が入っているか?OKです! PHPSpec と RSpec は記述方法が結構違うので、時間があれば RSpec についても調べてみてください。
今回はViewのテストは書かなくてもよいです。 機能を担保するテストを書くように意識してみてください。 下記記事も参考にしてみましょう。 http://blog.yuyat.jp/archives/1386
テストを書きにくい箇所などが出てきたら、設計を変更するのもアリです。 ひとまずはこの項目で進めてください!
いきなりDBからデータを取得する項目から書き始めた為、TestCase.phpを弄る。
俺がLaravel 4でユニットテストするときのTestCaseクラス - Qiita http://qiita.com/ngmy/items/c1487991d48ddba9688d
http://stackoverflow.com/questions/21726963/laravel-fatal-error-for-testcase-not-found
laravelのrootdirで$ phpunit app/tests/
叩きましょうというお話。
■目的
テスティングフレームワークのインストールと簡単なテストコードの記述をおこない、自動ユニットテストの基礎を学びます。 xUnit系、xSpec系の記述の違いについても簡単に触れます。
■課題1
数値の加算を行う簡単なプログラムがある。 RSpecおよびPHPUnitをインストールし、以下のことを検証するテストを記述してください。
■課題2
テスティングフレームワークはxUnit系とxSpec系に大別できます。PHPUnitはxUnit系であり、RSpecはxSpec系です。 両者の違いを調べ、テストコードの可読性やメンテナンス性、テストのドキュメントとしての価値という観点から説明してください。 ヒント: 歴史的にはxUnit系のほうが古いので、主にxSpec系が台頭した理由を考えるという方向性で調べてみてください。
■課題3
作成したフォームに対し必要なテスト項目を考え、実際にテストを記述して下さい。