首先安装phpunit

  1. wget https://phar.phpunit.de/phpunit.phar
  2. chmod +x phpunit.phar
  3. mv phpunit.phar /usr/local/bin/phpunit
  4. composer require --dev phpunit/dbunit //add plugin

Run single test
create the test.php, Content of test file is following:

  1. <?php
  2. define('ROOT_PATH', dirname(__FILE__) . './'); //设定根目录路径常量
  3. set_include_path(ROOT_PATH); //所有要加载的文件都以根路径开始
  4. require_once 'vendor/autoload.php';
  5. use PHPUnit\Framework\TestCase;
  6. class failureTest extends TestCase
  7. {
  8. public function testOne()
  9. {
  10. $this->assertTrue(true);
  11. }
  12. /**
  13. * @depends testOne
  14. */
  15. public function testTwo()
  16. {
  17. $this->assertTrue(true);
  18. }
  19. }
  20. $test = new failureTest();
  21. $test->testTwo();

And then run PHPunit test:

  1. phpunit test.php

Run test suite

Configuration of test suite: demosuite.xml. demo is directory containing all tests. Test files must be named as *_test.php (suffix).

  1. <testsuites>
  2. <testsuite name="DemoTestSuite">
  3. <directory suffix="test.php">demo</directory>
  4. </testsuite>
  5. </testsuites>

Test suite runs with following commands:

  1. phpunit -c demosuite.xml --testsuite DemoTestSuite

https://phpunit.readthedocs.io/zh_CN/latest/writing-tests-for-phpunit.html#writing-tests-for-phpunit-test-dependencies

分类: web

标签:   php