我是测试的新手,我正在使用PHPUnit来编写测试.所有网站都是使用MVC模式设计的.
我想在我的控制器上测试每个方法,问题是这些方法通过$ _POST变量接收参数.我该如何覆盖这个变量?
在此先感谢亚历杭德拉
我有一组复杂的PHPUnit测试,其中一些涉及连接到世界各地的服务器,无论出于何种原因,有时会超时.
当服务器超时时,我不想让测试失败,我只想在实际将其标记为失败之前重试该测试一次或多次.
现在,我知道这可能不是处理我手头情况的最好方法.一个更好的解决方案是修复服务器.但是,这是我现在无法控制的.
所以,我真正喜欢的是一种告诉PHPUnit重新测试每个失败的测试用例X次的方法,并且只有在每次失败时才将其标记为失败.
有任何想法吗?
编辑:你们中的许多人都提出了有用的建议,我没有这样做.我明白了,谢谢.但是,具体而言我要创建一个测试套件来测试整个系统的操作,包括远程服务器.我理解使用来自外部的"模拟"响应测试我的代码的某些部分的概念......但如果我的部分测试测试"完整堆栈",我也会在晚上睡得更好.
当我从命令行运行phpunit时,控制字符将被打印出来而不是像控制字符那样.看看这个:
PHPUnit 3.6.5 by Sebastian Bergmann.
Configuration read from app\phpunit.xml.dist
...
Time: 1 second, Memory: 12.00Mb
‹[30;42m‹[2KOK (3 tests, 3 assertions)
‹[0m‹[2K
Run Code Online (Sandbox Code Playgroud)
我认为这些符号‹[30;42m<是某种控制字符,应该由控制台以不同的方式使用(定位光标,删除字符等)
这可能有什么不对?
我正在使用PHPUnit/DBUnit来解决一些真正的速度问题.任何扩展都PHPUnit_Extensions_Database_TestCase需要永远运行.通过189次测试,该套件大约需要8-9分钟.我有点希望最多需要30秒;-)
看起来将数据库恢复到其初始状态是花费时间的过程,因此我们使数据集尽可能小并限制每个测试用例所需的表数.我正在使用灯具并尽可能地分享.
我可以使用任何设置或修改来加快执行速度吗?看看MySQL服务器在整个测试过程中做了什么,似乎发生了大量的截断/插入,但是将测试数据集打包到临时表中然后只需为每个测试选择它们会更快吗?
我正在使用的驱动程序是带有XML测试数据集的PDO/MySQL.
我有一个测试方法依赖于另一个本身在PHPUnit中使用数据提供程序的方法:
/**
* @dataProvider getFields
*/
public function testCanDoSomeStuff($parm1, $parm2) {
$result = my_func($parm1, $parm2);
$this->assertNotNull($result);
return $result;
}
/**
* @depends testCanDoSomeStuff
*/
public function testCanDoSomeMoreStuff($result) {
$this->assertNotNull($result);
}
Run Code Online (Sandbox Code Playgroud)
我也有getFields()数据提供程序功能,这里不需要显示.
依赖于数据提供者传递的第一个测试 - $result不是null.
我希望测试结果将作为$result参数传递给依赖测试.但是,该testCanDoSomeMoreStuff函数接收NULL参数并且测试失败.
更新
这个简单的测试用例演示了这个问题
class MyTest extends PHPUnit_Framework_TestCase {
/**
* @dataProvider myFunc
*/
public function testCanDoSomeStuff($value) {
$this->assertNotNull($value);
return $value;
}
/**
* @depends testCanDoSomeStuff
*/
public function testCanDoSomeMoreStuff($value) {
$this->assertNotNull($value);
}
/**
* Data provider function …Run Code Online (Sandbox Code Playgroud) 传统上,Symfony 2.0 bundle目录结构有一个Tests应该放置测试的目录.
Acme
-> SomeBundle
-> Controller
-> Entity
-> Service
-> ...
-> Tests
-> Controller
-> Entity
-> Service
-> ...
-> OtherBundle
-> ...
Run Code Online (Sandbox Code Playgroud)
我知道测试工作即使它们与他们测试的类没有相同的命名空间.
namespace Acme\SomeBundle\Service;
/**
* Some service.
*
* @author varchar
* @since August 1, 2012
*/
class SomeService
{
}
Run Code Online (Sandbox Code Playgroud)
对于上面的类,我(对于一个)通常会在以下名称空间下进行测试(受目录结构的影响):
namespace Acme\SomeBundle\Tests\Service;
Run Code Online (Sandbox Code Playgroud)
现在,这可行.
但顺便说一句,我使用Netbeans IDE,它找不到测试类,除非它在同一名称空间下(当你只想测试一个文件时这是值得注意的).嗯,这可能只是一个Netbeans标准(或其他东西).
但是,无论如何将测试置于与被测试类相同的命名空间中是否合适?这样做有什么优点吗?
1)将测试放在与被测试的类相同的命名空间中是否合适?
2)这样做有什么优点吗?
所以,我有一个页面上有一个带有"Create"值的按钮.当我单击"创建"按钮而不填写任何字段时,它会验证表单并在同一页面上显示错误消息.当我在浏览器中这样做时,它工作正常,但当我这样做时phpunit,它有意想不到的结果,我不知道为什么.
这是我的集成测试:
public function testCreateValidation()
{
$this->visit(route('patients.indexes.create', $this->patient->id));
$this->press('Create');
$this->seePageIs(route('patients.indexes.create', $this->patient->id));
}
Run Code Online (Sandbox Code Playgroud)
这就是结果:
There was 1 failure:
1) Tests\Integration\IndexControllerTest::testCreateValidation
Did not land on expected page [http://localhost/patients/69/indexes/create].
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/patients/69/indexes/create'
+'http://localhost/patients'
/vagrant/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:141
/vagrant/tests/Integration/IndexControllerTest.php:51
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它被重定向到patients页面.
以下是create正在测试的Laravel 方法:
public function create($id)
{
$index = $this->indexes->newInstance();
$patient = $this->patients->findOrFail($id);
return view('patient.index.create', ['index' => $index, 'patient' => $patient]);
}
Run Code Online (Sandbox Code Playgroud)
以下是该create视图的相关部分:
<?= Form::open(['route' => array('patients.indexes.store', $patient->id), 'class' …Run Code Online (Sandbox Code Playgroud) 在尝试运行任何PHPUnit 测试时,我总是No tests executed!在我的 MacOS 机器上收到一条消息。在这台特定机器上重现的一种简单方法是安装 Laravel 的新实例并运行默认测试:
$ composer create-project --prefer-dist laravel/laravel blog
$ cd blog
$ vendor/bin/phpunit
=> No tests executed!
Run Code Online (Sandbox Code Playgroud)
预期的输出将是OK (2 tests, 2 assertions)。
据我所知,这不是 PHPUnit 配置问题,因为默认的 Laravel 代码应该可以工作,其他框架和我尝试的任何代码都会出现同样的问题,不同的 PHPUnit 版本(8.5 和 9.4)也存在同样的问题上面列出的确切步骤会在 Ubuntu VM 以及另一台运行 Catalina 的 Mac 上返回预期的输出。
实际上,我怀疑这不是 PHPUnit 问题,而是 MacOS 问题或PHP 配置问题,以后可能会以其他形式出现在其他工具或项目中。
PHPUnit 曾经在这台机器上工作得很好,但是我实际上已经有几个星期/几个月没有使用它了。自从我上次(成功)在这台 Mac 上使用任何 PHPUnit 以来,唯一改变的是升级到 MacOS Big Sur 并安装(然后卸载)Homebrew。
问题出现 PHPUnit 找不到任何测试套件。在新的 Laravel 安装中运行时vendor/bin/phpunit …
我正在使用 Laravel 8 并测试我的应用程序,我正在运行
php artisan test --coverage-html reports
Run Code Online (Sandbox Code Playgroud)
测试运行成功。问题是没有生成覆盖率报告。我已经在这里查看了答案,据说解决方案将以下内容添加到我的phpunit.xml
<logging>
<log type="coverage-html" target="tests/coverage" showUncoveredFiles="true"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
Run Code Online (Sandbox Code Playgroud)
那是行不通的。我也尝试过
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
<report>
<html outputDirectory="tests/reports/coverage"/>
</report>
</coverage>
Run Code Online (Sandbox Code Playgroud)
依然没有; 下面是我的phpunit.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
<directory suffix="Test.php">./packages/okotieno</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="tests/coverage" showUncoveredFiles="true"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<php> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 WebTestCase 应用功能测试。
class CalculatorControllerTest extends WebTestCase
{
public function testSumPost()
{
$client = static::createClient();
$client->request('GET', '/calculator/sum');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,出现以下错误消息:
You cannot create the client used in functional tests if the "framework.test" config is not set to true
Run Code Online (Sandbox Code Playgroud)
我的测试框架:
framework:
test: ~
session:
storage_id: session.storage.mock_file
router:
resource: "%kernel.root_dir%/config/routing_test.yml"
profiler:
collect: false
Run Code Online (Sandbox Code Playgroud)
我的 phpunit.xml:(我也尝试在 APP_ENV 中测试而不是 dev & 调试 1 而不是 0)
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions …Run Code Online (Sandbox Code Playgroud) phpunit ×10
php ×9
laravel ×2
unit-testing ×2
command-line ×1
dbunit ×1
laravel-5 ×1
macos ×1
namespaces ×1
pdo ×1
symfony ×1
symfony4 ×1
windows-xp ×1