我在页面上有一个按钮,用作另一个页面的链接.因此,当用户单击该按钮时,它们将被重定向到右侧页面.
我正在使用PHPUnit的Selenium扩展,我想测试该按钮是否正确地重定向用户.这是我要测试的HTML:
<button onclick="window.location='/sponsor';" value="Continue" type="button" id="sponsorContinue" name="sponsorContinue">Continue</button>
Run Code Online (Sandbox Code Playgroud)
我已经尝试了很多不同的方法来点击按钮,但我似乎无法使它们中的任何一个工作:
$this->click("//button[@id='sponsorContinue']");
Run Code Online (Sandbox Code Playgroud)
此命令执行并不会引发任何错误,但不会重定向页面.我手动点击按钮时工作正常.我该怎么办?
我有一组测试,我想测试我的类在正确的时间抛出异常.在示例中,我的类使用__get()魔术方法,因此我需要测试在检索到无效属性时抛出异常:
function testExceptionThrownWhenGettingInvalidProperty() {
$obj = new MyClass();
$this->setExpectedException("Property qwerty does not exist");
$qwerty = $obj->qwerty;
}
Run Code Online (Sandbox Code Playgroud)
该类会抛出错误,但不是仅仅获取传递,而是捕获异常!
There was 1 error:
1) QueryTest::testExceptionThrownWhenGettingInvalidProperty
Exception: Property qwerty does not exist
Run Code Online (Sandbox Code Playgroud)
我之前使用过SimpleTest,$this->expectException(new Exception("Property qwerty does not exist"));工作得很好.我知道还有其他方法(@expectedException和try-catch),但是这个方法应该可以工作,看起来更清晰.我有什么想法可以使这项工作?
我以前用ruby/rspec编写了一些硒测试,发现它非常强大.现在,我正在使用Selenium和PHPUnit,并且有一些我缺少的东西,可能只是因为缺乏经验.在Ruby/RSpec中,我习惯于为每个测试用例定义一个"全局"设置,除此之外,我打开浏览器窗口并登录我的站点.
我认为PHPUnit在这里有点缺乏,1)你只有setUp()和tearDown(),每个单独的测试之前和之后运行,2)似乎实际的浏览器会话设置setUp()在测试和关闭之间之前tearDown().
这使得测试本身更加混乱,因为您明确必须在开始时打开页面,并在最后执行清理.在每一次测试中.对于每次测试关闭并重新打开浏览器似乎也是不必要的开销,而不是仅仅返回到登录页面.
有没有其他方法可以实现我正在寻找的东西?
我注意到当我使用模拟对象时,PHPUnit将正确报告执行的测试数量但错误地报告我正在进行的断言数量.事实上,每次我嘲笑它都算作另一个断言.一个包含6个测试的测试文件,7个断言语句和每个测试模拟一次报告6个测试,13个断言.
这是除了一个测试以外的所有测试的测试文件(这里用于说明),另外我介绍了另一个不存根以追踪此问题的测试.PHPUnit报告了2个测试,3个断言.我删除了dummy:1 test,2个断言.
require_once '..\src\AntProxy.php';
class AntProxyTest extends PHPUnit_Framework_TestCase {
const sample_client_id = '495d179b94879240799f69e9fc868234';
const timezone = 'Australia/Sydney';
const stubbed_ant = "stubbed ant";
const date_format = "Y";
public function testBlankCategoryIfNoCacheExists() {
$cat = '';
$cache_filename = $cat.'.xml';
if (file_exists($cache_filename))
unlink($cache_filename);
$stub = $this->stub_Freshant($cat);
$expected_output = self::stubbed_ant;
$actual_output = $stub->getant();
$this->assertEquals($expected_output, $actual_output);
}
public function testDummyWithoutStubbing() {
$nostub = new AntProxy(self::sample_client_id, '', self::timezone, self::date_format);
$this->assertTrue(true);
}
private function stub_FreshAnt($cat) {
$stub = $this->getMockBuilder('AntProxy')
->setMethods(array('getFreshAnt'))
->setConstructorArgs(array(self::sample_client_id, $cat, self::timezone, self::date_format))
->getMock();
$stub->expects($this->any())
->method('getFreshAnt') …Run Code Online (Sandbox Code Playgroud) 我有一个Zend Framework项目,并希望使用单元测试来测试它.
在tests文件夹中,我有phpunit.xml以下内容:
<phpunit bootstrap="./application/bootstrap.php" colors="true">
<testsuite name="Application Test Suite">
<directory>./</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../application/</directory>
<exclude>
<directory suffix=".phtml">../application/</directory>
<file>../application/Bootstrap.php</file>
<file>../application/controllers/ErrorController.php</file>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/reprot" charset="UTP-8"
yui="true" highlight = "true" lowUpoerBound="50" highLowerBound="80"/>
<log type="textdox" target="./log/testdox.html" />
</logging>
Run Code Online (Sandbox Code Playgroud)
我bootstrap.php在/ tests/application文件夹中有如下内容:
<?php
error_reporting(E_ALL | E_STRICT);
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path …Run Code Online (Sandbox Code Playgroud) 这不是一个问题,而是试图在PHPUnit上浪费我的其他时间.
我的问题是我的模拟对象,当在依赖测试中使用时,没有返回预期值.看来,PHPUnit的不保留相关测试之间的同一个对象,即使语法使它看起来像它.
有谁知道为什么PHPUnit这样做?这是一个错误吗?PHPUnit中的这类内容使得使用起来非常令人沮丧.
<?php
class PhpUnitTest
extends PHPUnit_Framework_TestCase
{
private $mock;
public function setUp()
{
$this->mock = $this->getMock('stdClass', array('getFoo'));
$this->mock->expects( $this->any() )
->method('getFoo')
->will( $this->returnValue( 'foo' ) );
}
public function testMockReturnValueTwice()
{
$this->assertEquals('foo', $this->mock->getFoo());
$this->assertEquals('foo', $this->mock->getFoo());
return $this->mock;
}
/**
* @depends testMockReturnValueTwice
*/
public function testMockReturnValueInDependentTest($mock)
{
/* I would expect this next line to work, but it doesn't! */
//$this->assertEquals('foo', $mock->getFoo());
/* Instead, the $mock parameter is not the same object as
* generated by …Run Code Online (Sandbox Code Playgroud) 我正在尝试在本教程之后向phpunit添加一个自定义断言,以验证作为字符串返回的复数(例如
"-123 + 456 1"
通过我正在测试的方法,对于实部和虚部都定义了精度.我已经将一个Complex.php类解析为实部和虚部,并将以下断言类组合为complexAssert.php:
require_once 'PHPUnit/Framework/Assert.php';
include_once getcwd().'/custom/Complex.php';
class complexAssert extends PHPUnit_Framework_Assert {
public function assertComplexEquals($expected, $actual, $message = '', $delta = 0)
{
$expectedComplex = new Complex($expected);
$actualComplex = new Complex($actual);
if (!($actualComplex->getReal() >= ($expectedComplex - $delta) &&
$actualComplex->getReal() <= ($expectedComplex + $delta))) {
return $this->fail($message);
}
if (!($actualComplex->getImaginary() >= ($expectedComplex - $delta) &&
$actualComplex->getImaginary() <= ($expectedComplex + $delta))) {
return $this->fail($message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的单元测试脚本:
require_once getcwd().'/custom/complexAssert.php';
require_once 'testDataFileIterator.php';
class EngineeringTest extends PHPUnit_Framework_TestCase
{ …Run Code Online (Sandbox Code Playgroud) 所以我想说我要比较两个DOMDocument对象.它们具有相同的内容,但订单和格式可能会关闭.例如,第一个输出此XML:
<responses>
<response id="12">
<foo>bar</foo>
<lorem>ipsum</lorem>
<sit>dolor</sit>
</response></responses>
Run Code Online (Sandbox Code Playgroud)
其他一项产出:
<responses>
<response id="12">
<lorem>ipsum</lorem><sit>dolor</sit>
<foo>bar</foo>
</response>
</responses>
Run Code Online (Sandbox Code Playgroud)
如您所见,它们包含相同的XML结构,但某些元素的顺序可能不同,格式完全是随机的.
如果我做:
$this->assertEquals();
Run Code Online (Sandbox Code Playgroud)
测试当然会失败.我不想只测试XML结构,还要测试内容.
有任何想法吗?
代码将解释一切:
<?php
class ATest extends PHPUnit_Framework_TestCase
{
public function testDestructorOnOriginalClass() {
$a = new A(); // It
unset($a); // works
echo " great!"; // great!
$this->expectOutputString('It works great!');
}
public function testDestructorOnMockedClass() {
$a = $this->getMock('A', array('someNonExistingMethod')); // It
unset($a); // works
echo " great!"; // great!
$this->expectOutputString('It works great!');
}
}
class A {
public function __construct()
{
echo "It";
}
public function __destruct()
{
echo " works";
}
}
Run Code Online (Sandbox Code Playgroud)
和输出:
# phpunit ATest.php
PHPUnit 3.7.13 by Sebastian Bergmann. …Run Code Online (Sandbox Code Playgroud) 我测试了我的工具包并得到了这个输出
PHPUnit 3.7.21
Configuration read from php-application-toolkit/dev/Test/phpunit.xml
............................................................... 63 / 119 ( 52%)
........................................................
Run Code Online (Sandbox Code Playgroud)
在某些时候,有一条新线,所有其他点都去那里.即使所有测试都正确,百分比也不是100%.
这里有什么不对?这有意义吗?
所有相关文件都在这里:http://github.com/sourcerer-mike/php-application-toolkit/tree/release-0.2
phpunit ×10
php ×8
unit-testing ×3
mocking ×2
selenium ×2
assert ×1
depends ×1
destructor ×1
exception ×1
selenium-rc ×1
tdd ×1
testing ×1