php - 如何将 PHPUnit 与我的代码捆绑在一起?

标签 php unit-testing phpunit

我愿意:

  • 无论我的环境如何(以及是否安装了 PHPUnit 或 PEAR),都可以使用 PHPUnit 运行测试
  • 如果可能,在屏幕上显示测试结果

我该怎么做?我尝试下载代码 here并包括 Autoload.php 但它仍然有一些依赖性。也许有比尝试将其与我的代码捆绑在一起更好的方法...?

最佳答案

要在您的项目源文件中包含 PHPUnit,我建议遵循以下指南:

Using PHPUnit From a Git Checkout来自 PHPUnit Contributung 部分。

它会告诉您需要安装的所有包,并显示您为 phpunit 可执行文件构建运行器/包装器脚本。

#!/bin/bash
php -d include_path='.:../phpunit/:../dbunit/:../php-code-coverage/:../php-file-iterator/:../php-invoker/:../php-text-template/:../php-timer:../php-token-stream:../phpunit-mock-objects/:../phpunit-selenium/:../phpunit-story/:/usr/local/lib/php' ../phpunit/phpunit.php $*

您可以根据需要调整路径,或者如果您想将其包装在另一个脚本中,您也可以通过编程方式使用 phpunit

require '/path/to/phpunit/PHPUnit/Autoload.php';
PHPUnit_TextUI_Command::main();

这假设您有一个 phpunit.xml.dist 文件,或者您在调用包装器脚本时使用了正确的 cli 参数。


您还可以使用 pear 包并解压所有稳定版本,而不是从 git checkout 工作以节省一些磁盘和存储库空间。包装脚本和所有包含路径的工作是相同的:)


相关问题:

PHP - Is there a portable version of PHPUnit?

PHPUNIT without installation

关于php - 如何将 PHPUnit 与我的代码捆绑在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12666479/

相关文章:

unit-testing - 在 PHPUnit/DBUnit 中关闭外键约束

php - 如果屏幕分辨率 <1000 像素,则显示不同的站点

php - 创建一个包含元素名称和值的 json 数组,并使用 ajax 将其提供给 php

python - 确保单元测试中的代码覆盖率?

unit-testing - 使用 zend test dbadapter 和 zend db 表摘要

php - 如何模拟接口(interface)的一种方法?

PHP 图像处理 GD JPEG 质量

php - 为什么 mysqli left join 返回 1 行?

Python "in"不检查类型?

phpunit 不工作