我在页面上有一个按钮,用作另一个页面的链接.因此,当用户单击该按钮时,它们将被重定向到右侧页面.
我正在使用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) 我想知道 - 有没有一个工具可以查看我的PHP代码并自动为它生成一个PHPUnit测试?或者是否有任何其他语言的工具,我可能能够移植到PHP?
我不是在谈论生成骨架.我认为工具可以查看标记化的PHP并通过方法确定代码路径,然后自动为每个代码路径生成测试,创建模拟并根据需要设置"期望"调用.
即使目前没有工具,也许这样的任务是可行的还是我忽略了什么?
我有一个直接派生自PHPUnit_Framework_TestCase的PHPUnit测试用例.在这个类的测试中,我需要获得一些服务对象的模拟.此服务对象是由抽象基类定义的类型.该基类包含具体和抽象方法.我想得到一个完整的模拟事物(即所有方法被嘲笑).我的问题是如何做到这一点.
- > getMock给我一个错误,因为抽象方法没有被模拟,只有具体的
- > getMockForAbstractClass模拟抽象方法,但不是具体方法
我该如何嘲笑他们?
(我正在使用PHPUnit 3.7.13)
我测试了我的工具包并得到了这个输出
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
在场景运行时是否可以记录或输出任何用户数据?我知道php代码在每次运行时执行两次,如何在第二步中看到变量的值?
考虑以下:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HomeRouteTest extends TestCase
{
public function testVisitTheHomePage()
{
$response = $this->call('GET', '/');
$this->assertEquals(200, $response->status());
}
public function testVisitTheAboutPage()
{
$response = $this->call('GET', '/about');
$this->assertEquals(200, $response->status());
}
}
Run Code Online (Sandbox Code Playgroud)
离开了,而不是我见过记录>.>,做类似的事情:
$response = $this->call('GET', 'home.about');
$this->assertEquals(200, $response->status());
Run Code Online (Sandbox Code Playgroud)
或者....你是怎么做到的?
我得到的错误是:
vagrant@scotchbox:/var/www$ phpunit
PHPUnit 4.8.21 by Sebastian Bergmann and contributors.
FF
Time: 3.41 seconds, Memory: 14.25Mb
There were 2 failures:
1) HomeRouteTest::testVisitTheHomePage
Failed asserting that 404 matches expected 200.
/var/www/tests/HomeRouteTest.php:12
2) HomeRouteTest::testVisitTheAboutPage
Failed asserting that 404 …Run Code Online (Sandbox Code Playgroud) phpunit ×10
php ×7
unit-testing ×4
testing ×3
selenium ×2
codeception ×1
exception ×1
laravel-5 ×1
laravel-5.2 ×1
mocking ×1
selenium-rc ×1
tdd ×1