我正在使用phpunit测试Zend Framework上的购物车,结帐,付款流程.我正在ShoppingCartController通过向购物车添加产品进行测试,ShoppingCart模型通过将产品ID存储在Zend会话命名空间中来处理产品添加,然后在另一个测试中我想测试产品是否已添加.同一个ShoppingCartModel从同一个Zend Session命名空间变量中检索添加的产品列表.
添加产品测试看起来像这样并且运行良好,并且var_dump($_SESSION)已添加到调试并正确显示产品:
public function testCanAddProductsToShoppingCart() {
$testProducts = array(
array(
"product_id" => "1",
"product_quantity" => "5"
),
array(
"product_id" => "1",
"product_quantity" => "3"
),
array(
"product_id" => "2",
"product_quantity" => "1"
)
);
Ecommerce_Model_Shoppingcart::clean();
foreach ($testProducts as $product) {
$this->request->setMethod('POST')
->setPost(array(
'product_id' => $product["product_id"],
'quantity' => $product["product_quantity"]
));
$this->dispatch($this->getRouteUrl("add_to_shopping_cart"));
$this->assertResponseCode('200');
}
$products = Ecommerce_Model_Shoppingcart::getData();
$this->assertTrue($products[2][0]["product"] instanceof Ecommerce_Model_Product);
$this->assertEquals($products[2][0]["quantity"],
"8");
$this->assertTrue($products[2][1]["product"] instanceof Ecommerce_Model_Product);
$this->assertEquals($products[2][1]["quantity"],
"1");
var_dump($_SESSION);
}
Run Code Online (Sandbox Code Playgroud)
第二个测试尝试通过询问模型来检索产品var_dump($_SESSION),在测试开始时已经为null. …
我如何在phpunit中进行===/strict等于比较?
在PHPUnit Selenium 2测试用例中,通过指定其类来选择元素很简单:
$element = $this->byClassName("my_class");
Run Code Online (Sandbox Code Playgroud)
但是,即使有两项my_class,选择器也只选择其中一项(可能是第一项).我该如何选择所有这些?我很感激allByClassName:
$elements = $this->allByClassName("my_class");
foreach($elements as $element) {
doSomethingWith($element);
}
Run Code Online (Sandbox Code Playgroud)
allByClassNamePHPUnit Selenium 2扩展中有什么类似的东西吗?
我试图在虚拟Ubuntu 12.04(64位服务器)安装上通过HHVM运行PHPUnit单元测试.测试通常使用位于我的tests目录中的phpunit.xml文件运行,该文件包含一个用于处理自动加载的引导文件,并且测试在普通的php安装上运行良好.但是,我一直在:
HipHop Fatal error: File not found: File/Iterator/Autoload.php in /usr/share/php/PHPUnit/Autoload.php on line 64
Run Code Online (Sandbox Code Playgroud)
运行时:
hhvm -f /usr/bin/phpunit /path/to/my/testsDirectory/SomeTest.php
Run Code Online (Sandbox Code Playgroud)
我还没有弄清楚如何使用bootstrap或配置文件在hhvm下运行phpunit ...任何帮助将不胜感激.
我有一个小PHP项目,它使用PHPUnit进行单元测试和覆盖.我想用cobertura XML格式生成覆盖率报告.
我可以使用任何工具或插件来实现这一目标吗?
任何帮助表示赞赏..
我已将测试分成不同的组,这样除非绝对必要,否则我不会运行最慢的测试。
然而,有些仍然很慢。
因此,我很想在整套测试完成运行之前查看失败或出错的测试的名称。
例如,当前当我运行时phpunit tests/Unit/ --verbose,我看到这样的输出:
PHPUnit 7.5.1 by Sebastian Bergmann and contributors.
Runtime: PHP 7.2.14-1+ubuntu16.04.1+deb.sury.org+1 with Xdebug 2.6.0
Configuration: /home/vagrant/Code/myproject/phpunit.xml
........F.....
Run Code Online (Sandbox Code Playgroud)
但即使我看到“F”,我也需要等待很长时间才能看到哪个测试失败了。
我希望能够允许测试继续运行,但同时开始调查该特定测试以了解其失败的原因。
如何?
我最近配置了 VS code 以使用 xdebug 调试 PHP。它与我的应用程序代码一起可靠地工作,但是当我使用 PHPunit 运行单元测试时,我的断点被忽略。
我的服务器在一个流浪盒子内运行。
我的php.ini文件包含以下几行:
[xdebug]
zend_extension="/usr/lib/xdebug/xdebug-2.2.7/modules/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_connect_back=1
xdebug.remote_autostart=1
Run Code Online (Sandbox Code Playgroud)
我使用PHP 调试VS 代码扩展。
这是我的launch.json配置:
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"pathMappings": {
"/var/www/mysite.local/": "${workspaceFolder}"
},
"port": 9000,
"log": true
}
Run Code Online (Sandbox Code Playgroud)
我的单元测试运行良好,例如,从内部/var/www/mysite.local,我可以运行:
phpunit --filter myTestMethod path/to/my/test/file/myTest.php
Run Code Online (Sandbox Code Playgroud)
但是当测试运行时,测试本身中的断点始终被忽略,我无法弄清楚为什么。
有人遇到过类似的问题吗?在正常应用程序请求和单元测试的上下文中调试的工作方式是否有区别?
正在Laravel Passport 用于构建 API,我相应地删除了网络路由及其防护
如何测试用户注销?
这是我到目前为止:
Logout Test
/**
* Assert users can logout
*
* @return void
*/
public function test_logout()
{
// $data->token_type = "Bearer"
// $data->access_token = "Long string that is a valid token stripped out for brevety"
$response = $this->json('POST', '/api/logout', [], [
'Authorization' => $data->token_type . ' ' . $data->access_token
]);
$response->assertStatus(200);
}
Run Code Online (Sandbox Code Playgroud)
routes/api.php
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
Run Code Online (Sandbox Code Playgroud)
控制器方法使用AuthenticatesUserstrait,因此保留默认功能
/**
* Log the user out of the application.
*
* @param \Illuminate\Http\Request $request
* …Run Code Online (Sandbox Code Playgroud) 我写了测试:
$response = $this->get('/something');
$response->assertStatus(200);
Run Code Online (Sandbox Code Playgroud)
它显示此错误:
Error: Call to undefined method Tests\Unit\SomeTest::assertStatus()
Run Code Online (Sandbox Code Playgroud)
我找到了解决问题的方法(代码如下),但我想使用第一种方法:
$response = $this->get('/something');
$this->assertEquals(200, $this->response->status());
Run Code Online (Sandbox Code Playgroud)
为什么第一个代码不起作用?
当我尝试在我的 PhpStorm 中运行测试时,我看到:
PHPUnit 8.5.2 由 Sebastian Bergmann 和贡献者编写。
警告:不推荐使用类名调用
这可能是 PhpStorm 如何处理 PHPUnit 自动加载器脚本的问题,并希望文件名与测试相同。
有转机吗?