我使用phpunit开发了一组测试用例,用于测试开发站点和生产站点.唯一的区别是域名.如何将域从命令行传递到phpunit测试用例,以便我不需要修改每个文件?
我将PHPUnit测试放入现有项目中.全局常量变量被广泛使用.在我的单元测试函数失败,因为全局变量为null.这是一个失败测试的例子
static $secret_key = "a secret Key";
class secret_key_Test extends PHPUnit_Framework_TestCase
{
function test_secret_key()
{
global $secret_key;
$this->assertEquals($secret_key, "a secret Key");
}
}
>> Failed asserting that 'a secret Key' matches expected null
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激
更新:我尝试删除静态和添加
protected $backupGlobals = FALSE;
Run Code Online (Sandbox Code Playgroud)
对班级宣言没有成功.
因此,每当我在PHPUnit中遇到意外异常时(例如由于完整性检查而无法插入到db中),我的测试会失败,并且在没有运行tearDownAfterClass()函数的情况下会出错.这将使我的数据库处于凌乱的状态,因此我必须手动去清理它.有没有办法确保tearDownAfterClass()始终执行?
我正在做一些重载到使用CaptureEntirePageScreenshotToString函数的PHPUnit的Selenium扩展,我想只打印截图的路径,因为我们只在传递--verbose或--debug时才去.
例如,
phpunit --debug ./tests
然后在我的代码中的某处(这是psudo代码)
if (--debug)
echo "Screenshot: /path/to/screenshot.png
Run Code Online (Sandbox Code Playgroud)
建议?
我正在尝试为我的小型框架应用程序编写一些PHPUnit测试,但是在文档中没有看到任何指向完整请求和断言响应的方法(包含文本或200状态,或者什么,真的).
有没有办法做到这一点,任何人都找到/使用过?
我有一个TestSuite,我需要将其标记为跳过(整个测试套件 - 而不是套件中的特定测试用例).
class AllTests
{
public static function suite()
{
// this does not work same as within TestCase:
// throw new \PHPUnit_Framework_SkippedTestError("Out of order");
$Suite = new \PHPUnit_Framework_TestSuite(__NAMESPACE__);
$Suite->addTestSuite(translators\AllTests::cls());
$Suite->addTestSuite(TlScopeTest::cls());
$Suite->addTestSuite(TlNsTest::cls());
$Suite->addTestSuite(TlElementTest::cls());
$Suite->addTestSuite(TlItemTest::cls());
$Suite->addTestSuite(LangCodeTest::cls());
$Suite->addTestSuite(TlElemClassTagTest::cls());
return $Suite;
}
}
Run Code Online (Sandbox Code Playgroud)
如你所见,抛出PHPUnit_Framework_SkippedTestError异常不起作用.它不会被PHPUnit捕获,并且会将执行中断为任何未捕获的异常(这是可以理解的,因为suite()在构建测试层次结构时,在实际运行测试之前调用该方法).
我见过一个名为的异常类PHPUnit_Framework_SkippedTestSuiteError,但不知道如何利用它.有任何想法吗?
我有一个TestSuite,它聚合了许多测试用例以及其他测试套件.几乎所有的测试都失败了,因为我在代码的核心部分做了一些改变.
问题是这个包不是重要的,并且计划在以后修复.在那之前,我必须为每个其他软件包运行测试,但是当我这样做时,PHPUnit输出会充满来自相关软件包的错误.这会强迫我每次检查是否有任何其他软件包发生故障.
正如您可能怀疑的那样,这很容易受到人为错误的影响,即我可能会错过一个实际上非常重要的失败.
我只能运行我目前正在使用的测试套件,但是我无法控制一个软件包中的更改是否导致其他软件包出现故障.
我不想评论那个测试套件,因为我担心我(或者会在我之后接管代码的人)可能完全忘记它.
我在Mac OS上使用XAMPP,尝试使用PHPUnit的代码覆盖检查,这需要XDebug.xdebug.so文件附带XAMPP所以我已经为PHP启用了Xdebug:
// /Applications/XAMPP/xamppfiles/etc/php.ini
[xdebug]
zend_extension="/Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so"
xdebug.remote_enable=1
Run Code Online (Sandbox Code Playgroud)
现在,当我从Web服务器检查时,输出中有一个Xdebug部分phpinfo().
但是当我试图检查代码覆盖时,我收到一个错误:
$ phpunit --coverage-html report
PHPUnit 3.7.31 by Sebastian Bergmann.
The Xdebug extension is not loaded. No code coverage will be generated.
Run Code Online (Sandbox Code Playgroud)
我认为CLI使用的不同于php.iniApache.要查找php.iniCLI使用的文件,我执行了以下操作:
$ php -i | grep "php.ini"
Configuration File (php.ini) Path => /etc
Run Code Online (Sandbox Code Playgroud)
但是没有,/etc/php.ini所以我复制了一个:
$ sudo cp /Applications/XAMPP/xamppfiles/etc/php.ini /etc/php.ini
Run Code Online (Sandbox Code Playgroud)
现在我正在检查php -m模块,但没有Xdebug,我收到此错误:
$ php -m
Failed loading /Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so: dlopen(/Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so, 9): Symbol not found: _zend_execute_ex
Referenced …Run Code Online (Sandbox Code Playgroud) 库:"aws/aws-sdk-php":"2.*"
PHP版本:PHP 5.4.24(cli)
composer.json
{
"require": {
"php": ">=5.3.1",
"aws/aws-sdk-php": "2.*",
...
},
"require-dev": {
"phpunit/phpunit": "4.1",
"davedevelopment/phpmig": "*",
"anahkiasen/rocketeer": "*"
},
...
}
Run Code Online (Sandbox Code Playgroud)
我们已经制作了一个AwsWrapper来获取功能操作:uploadFile,deleteFile ...
你可以阅读这个类,依赖注入要进行单元测试.
专注于构造函数和内部$ this-> s3Client-> putObject(...)调用uploadFile函数.
<?php
namespace app\lib\modules\files;
use Aws\Common\Aws;
use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;
use core\lib\exceptions\WSException;
use core\lib\Injector;
use core\lib\utils\System;
class AwsWrapper
{
/**
* @var \core\lib\Injector
*/
private $injector;
/**
* @var S3Client
*/
private $s3Client;
/**
* @var string
*/
private $bucket;
function __construct(Injector $injector = null, S3Client $s3 = null)
{ …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的弹出对话框,由我的Laravel应用程序中的JavaScript驱动.实际上,在单击时,会在弹出窗口div中添加一个类,该类使用CSS过渡将其不透明度从0更改为1.
这是我的测试:
public function testCantFindCodePopup()
{
$customer = $this->createCustomer();
$this->fillOutEmailAndDob($customer);
$this->visit('/product-codes/new')
->dontSee("Still can't find your code?");
$this->click('Can\'t find your code?');
sleep(0.5);
$this->see("Call customer service");
}
Run Code Online (Sandbox Code Playgroud)
过渡需要300毫秒,所以我认为sleep500毫秒将解决问题,但没有骰子.
实际上,测试在dontSee("Still can't find your code?")部分失败,即使该文本位于弹出窗口内部,该弹出窗口已display: none在加载时设置.
我做错了什么,或者PHPUnit不知道像capybara那样的CSS和JavaScript (因为它在无头浏览器中运行).
如果我不能将PHPUnit用于这种类型的集成测试,那么我可以使用类似的东西吗?请注意,我在PHPUnit中有大约70个其他测试,所以无论其他工具是什么,它都不能代替批发; 理想情况下,它与我的PHPUnit测试一起存在.
刀片模板的相关部分:
<div class="form-control">
<label for="product-code">Enter Your{{$second}}Code</label>
<input type="text" id="product-code" name="product-code" />
</div>
<button class="btn btn-dark" type="submit">Submit</button>
<span class="label-explanation js__popup-toggle">Can't find your code?
<div class='popup'>
<span class="popup__close">×</span>
<img src="/assets/images/find-code-pop-up.png" alt="[alt text]" /> …Run Code Online (Sandbox Code Playgroud) 要使用项目的PHPUnit运行我的测试,我执行以下操作:php vendor/bin/phpunit tests/SomeClassTest.php在下面的类声明中,它可以正常工作:
class SomeClassTest extends PHPUnit_Framework_TestCase {
public function test_someMethod() {}
}
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时它失败了:
use PHPUnit\Framework\TestCase;
class SomeClassTest extends TestCase {
public function test_someMethod() {}
}
Run Code Online (Sandbox Code Playgroud)
我明白了PHP Fatal error: Class 'PHPUnit\Framework\TestCase' not found......