在开始拍摄我之前,只是想知道我对单元测试完全陌生,所以我什至不知道我在这里发布的问题是否可以实现。
所以,我喜欢做的是测试一个类的构造函数方法是否获得了一定数量的参数。我怎样才能做到这一点 ?
我的基本课程如下:
class Time {
public function __construct() {
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试如下:
require dirname(dirname(__FILE__)) . "/time.php";
require dirname(dirname(__FILE__)) . "/vendor/autoload.php";
class TimeTests extends PHPUnit_Framework_TestCase {
protected $classInstance;
protected function setUp()
{
$this->classInstance = new Time();
}
public function testConstructorExists() {
$this->assertTrue ( method_exists( $this->classInstance , '__construct' ), 'Constructor method does not exists' );
}
}
Run Code Online (Sandbox Code Playgroud)
目前第一个测试正常工作,但我不知道如何测试构造函数参数。
对于这个构造函数,我喜欢有三个整数参数,那么,我该如何正确测试参数是否存在?
我正在考虑的一种可能的解决方案是在构造函数中使用 Throw Exception,但我不知道这是否正确。
请问有什么帮助吗?:)
在 PHPUnit 中使用 Symfony2,如何在断言中使用 OR 条件?
在我的情况下,客户端请求可以返回代码200OR 302,但assertEquals期望只有一种可能性。如果代码不是200AND ,有什么方法可以抛出异常302吗?
private function check($method, $url, $code)
{
echo "Expected code [".$code."] => URL ".$url."\n";
$this->client->request($method, $url);
$this->assertEquals($code, $this->client->getResponse()->getStatusCode());
}
Run Code Online (Sandbox Code Playgroud) 我已经从 Laravel 5.0 升级到 5.1
测试套件工作正常,我可以运行 phpunit 命令。但是,当我开始使用 api 测试进行测试时,总是会遇到 foreach 错误。
class ExampleTest extends TestCase {
public function testLoginCredentials()
{
$this->post('/srv/plc/auth/login', ['data' => 'some data'])
->seeJson([
'authorized' => true,
]);
}
}
Run Code Online (Sandbox Code Playgroud)
上面看起来像文档:http : //laravel.com/docs/5.1/testing#testing-json-apis
如果我通过 phpunit 运行我的测试,我会收到以下错误:
There was 1 error:
1) ExampleTest::testBasicExample
ErrorException: Invalid argument supplied for foreach()
/Applications/XAMPP/xamppfiles/htdocs/w/server/vendor/framework/src/Illuminate/Support/Arr.php:423
/Applications/XAMPP/xamppfiles/htdocs/w/server/vendor/framework/src/Illuminate/Support/helpers.php:301
/Applications/XAMPP/xamppfiles/htdocs/w/server/vendor/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php:365
/Applications/XAMPP/xamppfiles/htdocs/whennn/server/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php:352
/Applications/XAMPP/xamppfiles/htdocs/whennn/server/tests/ExampleTest.php:17
/Applications/XAMPP/xamppfiles/lib/php/PHPUnit/TextUI/Command.php:188
/Applications/XAMPP/xamppfiles/lib/php/PHPUnit/TextUI/Command.php:126
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
Run Code Online (Sandbox Code Playgroud)
如果我使用 $this->get 执行 get 请求,则会收到相同的错误。与其他端点相同的错误。
$this->visit 工作正常。
我正在尝试在 Ubuntu 14.04 上进行 WordPress 插件测试,我正在设置 WP-CLI,但我无法下载必要的文件(includes/functions.php)。
这是我正在使用的命令:
bash bin/install-wp-tests.sh wordpress_test root '' localhost latest
Run Code Online (Sandbox Code Playgroud)
这是输出:
+ install_wp
+ '[' -d /tmp/wordpress/ ']'
+ return
+ install_test_suite
++ uname -s
+ [[ Linux == \D\a\r\w\i\n ]]
+ local ioption=-i
+ '[' '!' -d /tmp/wordpress-tests-lib ']'
+ cd /tmp/wordpress-tests-lib
+ '[' '!' -f wp-tests-config.php ']'
+ download https://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php /tmp/wordpress-tests-lib/wp-tests-config.php
++ which curl
+ '[' /opt/lampp/bin/curl ']'
+ curl -s https://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php
+ sed -i 's:dirname( __FILE__ ) . '\''/src/'\'':'\''/tmp/wordpress/'\'':' /tmp/wordpress-tests-lib/wp-tests-config.php
+ sed …Run Code Online (Sandbox Code Playgroud) 任何人都可以帮助我理解为什么这些单元测试在通过本地设置时在 CI 上失败的原因。
存储库位于https://github.com/jabranr/test-ci
测试在https://travis-ci.org/jabranr/test-ci
测试在 CI 中失败,并显示一个致命错误: PHP Fatal error: Class 'JRI\JabranCI\Exception\FooBarException' not found in /home/travis/build/jabranr/test-ci/test/exception/FooBarExceptionTest.php on line 11
该软件包使用 PSR-4 自动加载。这是composer.json和phpunit 配置。先感谢您!
我正在尝试开始 PHPUnit 测试。
我正在使用 composer 加载 PHPUnit 4.5 、Yii 1.1.14 和我们构建的一些自定义 Yii 包。在这些自定义包中,我们使用 Yii 类自动加载一些设置一些别名的文件。
在运行我们的应用程序时,我们手动包含基础 Yii 文件,然后运行 Composer 生成的自动加载。
问题是,当我们运行 PHPUnit 时,composer 自动加载首先运行。即使在使用 include 指定引导程序文件时:
bin/phpunit --bootstrap carcass/phpunit.bootstrap.php
Run Code Online (Sandbox Code Playgroud)
导致以下异常:
Fatal error: Class 'Yii' not found
Run Code Online (Sandbox Code Playgroud)
事实上,即使在--解析选项之前,自动加载似乎也已运行:
bin/phpunit --help
Run Code Online (Sandbox Code Playgroud)
导致同样的错误。删除自动加载允许 PHPunit 运行。
有没有办法解决?
我尝试在我们的主 composer.json 中为 Yii 基础文件放置一个自动加载,但是子包的自动加载首先运行......同样的错误。
我还尝试在每个子包中为 Yii 基本文件放置一个自动加载..但是当作曲家使用require. 我也不是这个选项的忠实拥护者,因为它严格地定义了 Yii 定义来自于不需要知道的子包的地方。
我正在尝试过滤 PHPUnit 测试套件。当我尝试使用 来按测试名称进行过滤时--filter,将执行与指定字符串完全匹配的测试,但任何以该字符串开头的测试也将执行。
例如。如果我有两个测试,testExample 和 testExampleTwo,下面会运行这两个测试:
phpunit --filter testExample tests/Example.php
Run Code Online (Sandbox Code Playgroud)
我怎样才能让 phpunit 坚持我给它的确切字符串?我试过了,/testExample$/但没有运行任何测试。我正在使用 PHPUnit 3.7。
我今天开始在 PhpStorm 上使用 PHPUnit 测试。我可以测试几乎所有的功能,除了需要数据库连接的功能。
功能:
public function getProductID()
{
$this->product_id = $this->db->GetOne("select product_id from table1 where id = {$this->id}");
return $this->product_id['product_id'];
}
Run Code Online (Sandbox Code Playgroud)
在我的测试用例中,出现错误:
PHP 致命错误:在 null 上调用成员函数 GetOne()
我已经定义:
global $_db;
$this->db = $_db;
Run Code Online (Sandbox Code Playgroud) 我正在使用 Laravel 创建一个项目,今天我遇到了一个问题,我找不到如何以“正确”的方式解决。
我有这个迁移类:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddCityAndCountryColumnToClient extends Migration {
public function up() {
Schema::table('clients', function (Blueprint $table) {
$table->string('city')->nullable()->default(null);
$table->string('country', 2)->nullable()->default(null);
});
}
public function down() {
Schema::table('clients', function (Blueprint $table) {
$table->dropColumn('country');
$table->dropColumn('city');
});
}
}
Run Code Online (Sandbox Code Playgroud)
我有一堆基于该表“客户端”模型的测试,它们运行得非常好,但是在我添加了这个迁移之后,我的测试每次从一开始就失败了。
现在,我发现如果我将该迁移类修改为:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddCityAndCountryColumnToClient extends Migration {
public function up() {
Schema::table('clients', function (Blueprint $table) {
$table->string('city')->nullable()->default(null);
$table->string('country', 2)->nullable()->default(null);
});
}
public function down() {
Schema::table('clients', function (Blueprint $table) {
// $table->dropColumn('country');
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试对我的服务进行单元测试,其中包含 symfony2 的依赖 Finder 组件(http://symfony.com/doc/current/components/finder.html)。
我越来越 :
[异常] Mock_Finder_91776c5c::getIterator() 返回的对象必须是可遍历的或实现接口 Iterator
服务 :
public function getFile($fileName, $path = '/')
{
if($this->wrapper == null || $this->connection == null)
throw new LogicException("call method setFTP first");
// get file on ftp server
$this->connection->open();
$downloadComplete = $this->wrapper->get($this->tmpDir . $fileName, $path . $fileName);
$this->connection->close();
if($downloadComplete == false)
return false; // TODO exception ?
// return file downloaded
$this->finder->files()->in(__DIR__);
foreach ($this->finder as $file) {
return $file;
}
return false; // TODO exception ?
} …Run Code Online (Sandbox Code Playgroud)