可以说我有以下代码块:
$i = 1;
if ($i > 1) {
$this->methodOne();
} else {
$this->methodTwo();
}
Run Code Online (Sandbox Code Playgroud)
如何检查我的PHPUnit测试中是否从已测试的类调用methodOne或methodTwo?
我有一些作为cronjobs运行的PHP脚本.没有课程,只有少数几个功能.
我喜欢用PHPUnit测试脚本,以确保一切正常,但似乎我需要重写php作为一个类,我不想做(或知道如何).
例如,如果我有一个脚本,它唯一的功能是将1和2加在一起,
<?php
$a=1;
$b=2
$c=$a+$b;
?>
Run Code Online (Sandbox Code Playgroud)
如何用phpunit测试$ c == 3?
谢谢,唐
我用phpunit进行了'没有测试'
这条线有效
$ phpunit install/InstallDbTest.php
...
<result expected>
...
$ cat suites/installtests.xml
<phpunit>
<testsuites>
<testsuite name="database">
<file>install/InstallDbTest.php</file>
</testsuite>
</testsuites>
</phpunit>
$ phpunit -c suites/installtests.xml
PHPUnit 4.7.4 by Sebastian Bergmann and contributors.
Time: 130 ms, Memory: 11.25Mb
No tests executed!
Run Code Online (Sandbox Code Playgroud)
有谁能告诉我我做错了什么?
提前致谢!
我有以下文件夹结构,我希望在不同的目录中存在多个测试套件.
- project
- app
- app-A
- tests
- (functional tests here)
- app-B
- tests
- (functional tests here)
- domains
- domain-A
- tests
- (unit tests here)
- domain-B
- tests
- (unit tests here)
Run Code Online (Sandbox Code Playgroud) 我在这里待了好几天都没有成功。运行中Laravel 5.1。据我了解的Laravel文档。
tests目录中提供了ExampleTest.php文件。安装新的Laravel应用程序后,只需在命令行上运行phpunit即可运行测试。
好的,所以运行后phpunit我发现它尚未全局安装且位于中vendor/bin/phpunit。继续运行vendor/bin/phpunit会为我提供一个包含phpunit所有命令选项的屏幕。它实际上没有运行任何测试。
我搜索了更多内容,然后发现一则帖子提到可能是一个symlink问题。我没有按照帖子中的说明进行操作。从内存,我是Laravel从作曲家安装的,所以symlink无论如何都不应该是一个问题。
这些说明涉及删除各种文件。我确实注意到作曲家在更新时正在从其缓存中提取文件。因此,我清除了其缓存,然后再次执行该过程。仍然没有成功。
什么测试没有运行?我机智。特别是因为它应该很容易在包装盒中运行。
我测试的目的是检查函数返回错误404,或者更准确,功能创建呼叫服务器(我使用狂饮),我希望服务器返回404错误,但我无法找到任何@expectedException这有道理因为它是一个错误,但有谁知道如何用phpunit测试这个案例??? 谢谢
如果它可能有帮助,这就是函数的样子(最后一次尝试):
public function testApi_deleteExecuted()
{
$path = '/adserver/src/public/api/rule/288';
$client = new Client(['base_uri' => 'http://10.0.0.38']);
$response = $client->get($path);
$data = json_decode($response->getBody());
$code = $response->getStatusCode();
$this->assertEquals($code, 404);
}
Run Code Online (Sandbox Code Playgroud) 当我尝试测试这个类时,使用PSR-4自动加载Composer:
namespace User;
use User\Contracts\UserId;
use User\Contracts\User as UserContract;
class User implements UserContract
{
private $id;
public function __construct(UserId $id)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
}
Run Code Online (Sandbox Code Playgroud)
使用PHPUnit:
use User\Contracts\UserId;
class UserTest extends \PHPUnit_Framework_TestCase
{
public function test_the_identifier_can_be_used_as_string()
{
$identifier = m::mock(UserId::class);
$identifier->shouldReceive('__toString')->once()->andReturn('foo');
$user = new User($identifier);
$this->assertSame('foo', (string) $user->getId());
}
}
Run Code Online (Sandbox Code Playgroud)
我在控制台中收到此错误
PHP Fatal error: Cannot use User\Contracts\UserId as UserId because the name is already in use in /src/User/User.php on line 5
Run Code Online (Sandbox Code Playgroud)
这是文件/文件夹结构: …
这是一个PHPUnit测试:
class EqualsTest extends PHPUnit_Framework_TestCase
{
public function testSuccess()
{
$this->assertEquals(array('01', 'a', 'b'), array(1, 'a', 'b'));
}
}
Run Code Online (Sandbox Code Playgroud)
'01'是一个字符串,1是一个整数,但是测试没有失败! assertEquals不比较类型...如何克服这个问题?
我想在我的本地Windows机器上运行phpunit测试..(使用wamp)..
我已经全局安装了composer ..并且phpunit安装在我的本地目录中..
文件夹结构..
_________tests
| |
| |______PagesTest.php
|
|________vendor
|
|______bin
|
|_____phpunit
Run Code Online (Sandbox Code Playgroud)
在我的localroot(www)..我创建了一个phpunit.xml文件..
<?xml version="1.0" encoding="UTF-8"?>
<phpunit convertWarningsToExceptions="true"
convertNoticesToExceptions="true"
convertErrorsToExceptions="true"
backupStaticAttributes="false"
processIsolation="false"
stopOnFailure="false"
backupGlobals="false"
syntaxCheck="false"
colors="true">
<testsuites>
<testsuite name="Punta Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
在Cmd我试过:
C:\wamp\www\vendor\bin>phpunit PagesTest
Run Code Online (Sandbox Code Playgroud)
输出是
Cannot open file PagesTest.php
Run Code Online (Sandbox Code Playgroud)
我不知道如何使用composer运行这个PagesTest.php文件
在一个项目中,我发现了以下代码行:
protected function save($content, $path)
{
// ...
if (($handler = @fopen($path, 'w')) === false) {
throw new Exception('...');
}
// ...
if (@fwrite($handler, $content) === false) {
throw new Exception('...');
}
// ...
@fclose($handler);
}
Run Code Online (Sandbox Code Playgroud)
我想用PHPUnit测试此方法,但是我对正确的测试用例有些困惑。如果我传递的不正确$path或$path具有错误权限的正确信息(例如0444),那么一切都会在第一个例外处停止。如果我$path以正确的权限传递了正确的权限,那么PHP也将能够写入该文件,并且不会达到第二个异常。
那么有没有办法在不重写此方法的情况下测试第二个异常?
还是最好同时检查fopen和同时检查fwrite一种情况并为两者都使用一种例外情况?
还是最好的选择是将此方法分为两种-一种用于打开,一种用于书写-分别测试?
php ×10
phpunit ×10
composer-php ×2
unit-testing ×2
assert ×1
codeception ×1
exception ×1
laravel-5 ×1
testing ×1
windows ×1