@@ -2,57 +2,99 @@ <h1>Unit Testing</h1>
22
33< ul >
44 < li > < a href ="index.php?page=unit-tests#general "> < span > General</ span > </ a > </ li >
5- < li > < a href ="index.php?page=unit-tests#installation "> < span > Installation and usage</ span > </ a > </ li >
5+ < li > < a href ="index.php?page=unit-tests#tests_installation "> < span > Installation of PHPUnit</ span > </ a > </ li >
6+ < li > < a href ="index.php?page=unit-tests#tests_running "> < span > Running tests</ span > </ a > </ li >
67</ ul >
78< br />
89
910
1011< a name ="general "> </ a >
12+ < h3 > General: < a class ="hashlink " href ="#general "> ¶</ a > </ h3 >
1113< p >
1214 Because the ApPHP Framework testing is built on top of PHPUnit, we strongly recommended that you go through the PHPUnit documentation
1315 first to get the basic understanding on how to write a unit test.
1416
1517 We summarize in the following the basic principles of writing a unit test in ApPHP.
16- </ p >
1718
18- .........
19+ < ul >
20+ < li >
21+ A unit test is written in terms of a class < code > AbcTest</ code > which extends from < code > TestCase</ code > ,
22+ where < code > AbcTest</ code > stands for the class being tested.
23+
24+ For example, to test the < code > CArray</ code > class, we would name the corresponding unit test as CArrayTest by convention.
25+ The base class < code > TestCase</ code > is meant for generic unit tests. Because < code > PHPUnit_Framework_TestCase</ code >
26+ is the ancestor class for these classes, we can use all methods inherited from this class.
27+ </ li >
28+ < li >
29+ The unit test class is saved in a PHP file named as < code > AbcTest.php</ code > .
30+ It may be stored under the directory < code > tests/framework</ code > for framework tests or
31+ < code > tests/protected</ code > for application tests.
32+ </ li >
33+ < li >
34+ The test class mainly contains a set of test methods named as < code > testAbc</ code > , where < code > Abc</ code > is
35+ often the name of the class method to be tested.
36+ </ li >
37+ < li >
38+ A test method usually contains a sequence of assertion statements (e.g. assertTrue, assertEquals) which serve
39+ as checkpoints on validating the behavior of the target class.
40+ </ li >
41+ </ ul >
42+ </ p >
43+ < br >
1944
2045
21- < a name ="installation "> </ a >
46+ < a name ="tests_installation "> </ a >
47+ < h3 > Installation and usage: < a class ="hashlink " href ="#tests_installation "> ¶</ a > </ h3 >
2248< p >
2349 To install and run Unit test you have to perform following actions:
2450</ p >
2551
2652< ol >
27- < li >
28- Make sure you added following to < strong > composer.json</ strong > file:
29- < pre >
30-
31- "scripts": {
32- "tests-result": "phpunit --colors=always --log-junit test-results.xml",
33- "tests": "phpunit --colors=always",
34- "test": "phpunit --colors=always --filter"
35- }
36- </ pre >
37-
38- </ li >
3953 < li >
4054 Install composer on your server.
4155 < a class ="external " href ="https://getcomposer.org " target ="_blank " rel ="noopener noreferrer "> https://getcomposer.org</ a >
56+ < br > < br >
4257 </ li >
4358 < li >
4459 After Composer is installed, install PHPUnit by< br >
4560 < pre > composer require phpunit/phpunit --dev</ pre >
46- < br > < br >
47-
48- you may also remove it and re-install:< br >
61+ < br >
62+ you may also remove it and re-install again:< br >
4963< pre > composer remove phpunit/phpunit --dev
5064composer update
5165</ pre >
52-
5366 </ li >
5467 < li >
55- Run tests by....
68+ Make sure you added following to < strong > composer.json</ strong > file:
69+ < pre >
70+ "scripts": {
71+ "tests-result": "phpunit --colors=always --log-junit test-results.xml",
72+ "tests": "phpunit --colors=always",
73+ "test": "phpunit --colors=always --filter"
74+ }
75+ </ pre >
76+ </ li >
77+ < li >
78+ Create test files in < code > tests/protected</ code > directory and write your tests.
5679 </ li >
5780</ ol >
81+ < br >
82+
83+
84+ < a name ="tests_running "> </ a >
85+ < h3 > Running tests: < a class ="hashlink " href ="#tests_running "> ¶</ a > </ h3 >
86+ < ol >
87+ < li >
88+ To run all your tests use following command in command line:
89+ < pre > composer tests</ pre >
90+ </ li >
91+ < li >
92+ To run specific test use following command:
93+ < pre > composer test AbcTest</ pre >
94+ </ li >
95+ < li >
96+ To run specific test from the specific test file use following command:
97+ < pre > composer test AbcTest::testXyz</ pre >
98+ </ li >
5899
100+ </ ol >
0 commit comments