cvs: peardoc /ja/package/php/phpunit intro.xml run.xml testcase.xml

From: Date: Sat, 05 Apr 2003 05:31:39 +0000
Subject: cvs: peardoc /ja/package/php/phpunit intro.xml run.xml testcase.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-605@lists.php.net to get a copy of this message
hirokawa Sat Apr 5 00:31:39 2003 EDT Added files: /peardoc/ja/package/php/phpunit intro.xml run.xml testcase.xml Log: added phpunit/*.xml

Index: peardoc/ja/package/php/phpunit/intro.xml +++ peardoc/ja/package/php/phpunit/intro.xml <?xml version="1.0" encoding="utf-8" ?> <!-- $Revision: 1.1 $ --> <refentry id="packages.php.phpunit.intro"> <refnamediv> <refname>チュートリアル</refname> <refpurpose> PHPUnitに関する簡易チュートリアル </refpurpose> </refnamediv> <refsect1> <title>試験フレームワークの紹介</title> <para> <classname>PHPUnit</classname>は、関数やクラスのテストを自動的に行 うテストアプリケーションを作成する簡単な仕組みを提供します。 PHPUnitは、<ulink url="http://www.junit.org">JUnit</ulink>からヒン トを得ています。Kent BeckとErich Gammaが、 <ulink url="http://xprogramming.com">eXtreme Programming</ulink> 用のツールとしてJUnitを作成しました。XPの要素の1つは、 小さなソフトウエアコンポーネントを可能な限り頻繁に、そして、早期に 試験し、アプリケーション全体を設定、試験する際にAPIのバグやエラーを を修正しないことです。 PHPUnitから利益を得るためにXPに移行する必要はありません。 PHPUnitは、クラスまたは関数の集まりをテストするための有効なツールで あり、デバッグを際限なく続けることを避ける手助けとなります。 </para> </refsect1> <refsect1 id="packages.php.phpunit.intro.workroutine"> <title>実行手順</title> <para> 通常、クラスを作成し、<function>echo</function> または <function>var_dump</function> を用いてシステム化されていないいくつ かのテストを行い、全てがうまく行くことを願います。PHPUnitによる恩恵 を得るためには、この流れを再考する必要があります。最善の方法は、以 下のように行うことです。 <itemizedlist> <listitem> <para>1. クラス/APIを設計</para> </listitem> <listitem> <para>2. テスト用ツールを作成</para> </listitem> <listitem> <para>3. クラス/APIを実装</para> </listitem> <listitem> <para>4. テスト用ツールを実行</para> </listitem> <listitem> <para>5. 失敗またはエラーを修正し、再度#4へ進む</para> </listitem> </itemizedlist> この手順は、多くの時間を必要とするように見えますが、この印象は誤り です。PHPUnitを用いてテスト用ツールを作成するには数分しかかからず、 テスト用ツールを実行するのには、数秒しかかかりません。 </para> </refsect1> <refsect1 id="packages.php.phpunit.intro.design"> <title>クラスの設計</title> <para> 簡単な例として、文字列処理クラスから始めてみましょう。まず、文字列 処理を行う一連の関数を関数の宣言を以下のように作成します。 <programlisting role="php"> <![CDATA[ ---- string.php ---- <?php class String { //contains the internal data var $data; // constructor function String($data) { $this->data = $data; } // creates a deep copy of the string object function copy() { } // adds another string object to this class function add($string) { } // returns the formated string function toString($format) { } } ?> ]]> </programlisting> </para> </refsect1> <refsect1 id="packages.php.phpunit.intro.testsuite"> <title>テスト用スーツの作成</title> <para> Now we can create a test suite, which checks every function of your string class. A test suite is normal PHP class inherited from <classname>PHPUnit_TestCase </classname> containing test functions, identified by a leading 'test' in the function name. In the test function an expected value has to be compared with the result of the function to test. The result of this compare must delegate to a function of the <function>assert*</function>-family, which decides if a function pass or fail the test. <programlisting role="php"> <![CDATA[ ---- testcase.php ---- <?php require_once 'string.php'; require_once 'PHPUnit.php'; class StringTest extends PHPUnit_TestCase { // contains the object handle of the string class var $abc; // constructor of the test suite function StringTest($name) { $this->PHPUnit_TestCase($name); } // called before the test functions will be executed // this function is defined in PHPUnit_TestCase and overwritten // here function setUp() { // create a new instance of String with the // string 'abc' $this->abc = new String("abc"); } // called after the test functions are executed // this function is defined in PHPUnit_TestCase and overwritten // here function tearDown() { // delete your instance unset($this->abc); } // test the toString function function testToString() { $result = $this->abc->toString('contains %s'); $expected = 'contains abc'; $this->assertTrue($result == $expected); } // test the copy function function testCopy() { $abc2 = $this->abc->copy(); $this->assertEquals($abc2, $this->abc); } // test the add function function testAdd() { $abc2 = new String('123'); $this->abc->add($abc2); $result = $this->abc->toString("%s"); $expected = "abc123"; $this->assertTrue($result == $expected); } } ?> ]]> </programlisting> </para> </refsect1> <refsect1 id="packages.php.phpunit.intro.firstrun"> <title>The first test run</title> <para> Now, we can run a first test. Execute this PHP program. Make sure that the paths are correct. <programlisting role="php"> <![CDATA[ ---- stringtest.php ---- <?php require_once 'testcase.php'; require_once 'PHPUnit.php'; $suite = new PHPUnit_TestSuite("StringTest"); $result = PHPUnit::run($suite); echo $result -> toString(); ?> ]]> </programlisting> </para> <para> If you call this script through commandline, you will get this output: <programlisting> <![CDATA[ TestCase stringtest->testtostring() failed: expected true, actual false TestCase stringtest->testcopy() failed: expected , actual Object TestCase stringtest->testadd() failed: expected true, actual false ]]> </programlisting> Every function fails the test, because your string functions didn't returned what we defined as the expected value. </para> <para> If you want to call the script through your browser, you have to put the script in a correct html page and call <function>$result->toHTML </function> instead of <function>$result->toString</function>. </para> </refsect1> <refsect1 id="packages.php.phpunit.intro.implement"> <title>Implementation</title> <para> Ok, let's start with implementation of the our string class. <programlisting role="php"> <![CDATA[ ---- string.php ---- <?php class String { //contains the internal data var $data; // constructor function String($data) { $this->data = $data; } // creates a deep copy of the string object function copy() { $ret = new String($this->data); return $ret; } // adds another string object to this class function add($string) { $this->data = $this->data.$string->toString("%ss"); } // returns the formated string function toString($format) { $ret = sprintf($format, $this->data); return $ret; } } ?> ]]> </programlisting> </para> <para> Implementing is done and run the test again: <programlisting> ~> <command>php -f stringtest.php</command> TestCase stringtest->testtostring() passed TestCase stringtest->testcopy() passed TestCase stringtest->testadd() failed: expected true, actual false </programlisting> D'oh! the last test failed! We did a typing mistake. Change line 16 in <filename>string.php</filename> to <programlisting role="php"> <![CDATA[ $this->data = $this->data.$string->toString("%s"); ]]> </programlisting> and run the test again: <programlisting> ~> <command>php -f stringtest.php</command> TestCase stringtest->testtostring() passed TestCase stringtest->testcopy() passed TestCase stringtest->testadd() passed </programlisting> Everything is ok now! </para> </refsect1> <refsect1 id="packages.php.phpunit.intro.conclusion"> <title>Conclusion</title> <para> Hm, such a big expenditure for testing three simple functions? Don't forget, this is a small example. Think about bigger, complexer API's like database abstraction or basket classes in a shop application. PHPUnit is an excellent tool to detect errors in the implementation. Maybe you have a big class used in a few applications and you want to reimplement the class. With a test suite, you can easily check and fix the new implementation in a short session. </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> Index: peardoc/ja/package/php/phpunit/run.xml +++ peardoc/ja/package/php/phpunit/run.xml <!-- $Revision: 1.1 $ --> <refentry id="package.php.phpunit.run"> <refnamediv> <refname>PHPUnit::run()</refname> <refpurpose>runs a TestSuite and returns a TestResult object.</refpurpose> </refnamediv> <refsynopsisdiv> <funcsynopsis> <funcsynopsisinfo> require_once 'PHPUnit.php'; </funcsynopsisinfo> <funcprototype> <funcdef>object <function>PHPUnit::run</function></funcdef> <paramdef> object <parameter>$suite</parameter> </paramdef> </funcprototype> </funcsynopsis> </refsynopsisdiv> <refsect1 id="package.php.phpunit.run.desc"> &title.desc; <para> Executes the given test suite </para> </refsect1> <refsect1 id="package.php.phpunit.run.parameter"> &title.param; <para> <itemizedlist> <listitem> <para> <parameter>object $suite</parameter> - an object of <link linkend="packages.phpunit.phpunit-testsuite">PHPUnit_TestSuite</link> </para> </listitem> </itemizedlist> </para> </refsect1> <refsect1 id="package.categorie.mypackage.myfunction.return"> &title.returns; <para> <parameter>&amp;PHPUnit_Result</parameter> - A reference to a <link linkend="packages.phpunit.phpunit-result">PHPUnit_Result</link>. </para> </refsect1> <refsect1 id="packages.php.phpunit.run.example"> &title.example; <example> <title>Using <function>PHPUnit::run</function></title> <programlisting role="php"> <![CDATA[ <?php require_once "PHPUnit.php"; $suite = New PHPUnit_TestSuite('Mathtest'); $result = PHPUnit::run($suite); echo $result->toHtml(); ?> ]]> </programlisting> </example> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> Index: peardoc/ja/package/php/phpunit/testcase.xml +++ peardoc/ja/package/php/phpunit/testcase.xml <refentry id="package.php.phpunit.testcase"> <refnamediv> <refname><classname>TestCase</classname></refname> <refpurpose>A TestCase defines the fixture to run multiple tests.</refpurpose> </refnamediv> <refsect1 id="package.php.phpunit.testcase.desc"> &title.desc; <para> &class.testcase; To define a TestCase 1) Implement a subclass of PHPUnit_TestCase. 2) Define instance variables that store the state of the fixture. 3) Initialize the fixture state by overriding setUp(). 4) Clean-up after a test by overriding tearDown(). Each test runs in its own fixture so there can be no side effects among test runs. </para> </refsect1> </refentry> &package.php.phpunit.testcase.example; &package.php.phpunit.testcase.counttestcases; <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
« previous php.pear.doc (#605) next »