更新:有$stub = $this->createMock('Config');这个例子的作品,但我得到一个警告:
好的,但不完整,跳过或有风险的测试!测试:1,断言:0,风险:1.
在视频教程中,此示例无需任何警告即可运行.是否可以修复此警告?
我找不到为什么我收到此错误以及如何解决它.此代码来自视频教程.在视频中它可以工作.也许是一个错字?
错误:
c:\ laragon\www\phpunitλphpunit--colors tests\DateFormatterTest.php由Sebastian Bergmann和贡献者提供的PHPUnit 6.0.0.
E 1/1(100%)
时间:35毫秒,内存:4.00MB
有1个错误:
1)DateFormatterTest :: testFormattingDatesBasedOnConfig错误:调用未定义的方法DateFormatterTest :: getMock()
C:\ laragon\WWW\PHPUnit的\测试\ DateFormatterTest.php:10
错误!测试:1,断言:0,错误:1.
这是我的代码:
config.php文件
<?php
class Config {
public function get() {
return 'd-m-Y';
}
}
Run Code Online (Sandbox Code Playgroud)
DateFormatter.php
class DateFormatter {protected $ config;
public function __construct (Config $config) {
$this->config = $config;
}
public function getFormattedDate($timestamp) {
return date($this->config->get('date.format'), $timestamp);
}
Run Code Online (Sandbox Code Playgroud)
}
DateFormatterTest.php
<?php
use PHPUnit\Framework\TestCase;
require_once 'C:\laragon\www\phpunit\src\DateFormatter.php';
require_once 'C:\laragon\www\phpunit\src\Config.php';
class DateFormatterTest extends TestCase { …Run Code Online (Sandbox Code Playgroud) 有人可以帮助我理解为什么我的环境变量没有在travis.ci的phpunit测试中读取吗?
所以我正在尝试使用travis为我正在研究的php/javascript应用程序编写一些自动测试.然而,当我编写一个测试来检查从travis读取到phpunit的环境变量时,它们会失败.这意味着(据我所知),环境变量无法被phpunit读取,或者它们没有被正确地传递给travis测试.
.travis.yml
language: php
php:
- '7.0'
- '7.1'
before_install:
- echo "extension=ldap.so" >>php --ini | grep "Loaded Configuration" | sed -e "s|.:\s||"``
install:
- cd test
- npm install
- cd ..
script:
- echo $API_BASE_URL
- phpunit test/build_tests.php
notifications:
on_success: never
on_failure: never
Run Code Online (Sandbox Code Playgroud)
phpunit测试文件
<?php
use PHPUnit\Framework\TestCase;
class build_tests extends TestCase
{
public function testForEnv()
{
$this->assertEquals(isset($_ENV['API_BASE_URL']), true);
$this->assertEquals(isset($_ENV['DRINK_SERVER_URL']), true);
$this->assertEquals(isset($_ENV['LOCAL_DRINK_SERVER_URL']), true);
$this->assertEquals(isset($_ENV['RATE_LIMIT_DROPS_DROP']), true);
$this->assertEquals(isset($_ENV['DEBUG']), true);
$this->assertEquals(isset($_ENV['DEBUG_USER_UID']), true);
$this->assertEquals(isset($_ENV['DEBUG_USER_CN']), true);
$this->assertEquals(isset($_ENV['USE_LOCAL_DRINK_SERVER']), true);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
travis出口环境变量
$ …Run Code Online (Sandbox Code Playgroud) Ich检查了网络上的几个来源,还有堆栈溢出,但到目前为止还无法解决.
继承我的配置:
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="../../src/test.bootstrap.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
stopOnRisky="false"
verbose="true"
>
<!--printerFile="vendor/whatthejeff/emoji-phpunit-resultprinter/src/Emoji/PHPUnit/ResultPrinter.php"-->
<!--printerClass="Emoji\PHPUnit\ResultPrinter"-->
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_DIR" value="src/" />
<env name="BOOTSTRAP_CLEAR_CACHE_ENV" value="testing"/>
</php>
<loggin>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-crap4j" target="build/logs/crap4j.xml"/>
<log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/>
</loggin>
<testsuites>
<testsuite name="Project Test Suite">
<directory>src/*/*Bundle/Tests</directory>
<directory>src/*/Bundle/*Bundle/Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>../src</directory>
<exclude>
<directory>src/*/*Bundle/Resources</directory>
<directory>src/*/*Bundle/Tests</directory>
<directory>src/*/Bundle/*Bundle/Resources</directory>
<directory>src/*/Bundle/*Bundle/Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
由于Build Server的限制,我需要使用以下命令调用phpunit:
./build/bin/phpunit.phar -c …Run Code Online (Sandbox Code Playgroud) 有没有办法使用PHPUnit我有一个indexTest.php的测试文件夹里面测试我的index.php文件中的路由?
无脂指南提供了用于模拟路由请求和POSTS的代码片段.如果我直接在我的测试文件中使用其中的任何功能生成路由,我只能设法得到这样的测试.
我想要的是模拟带有令牌的路由,允许它从index.php中的路由和控制器运行,并测试应该通过运行路由设置的f3变量.
<?php
class indexTest extends \PHPUnit_Framework_TestCase
{
public function test()
{
$f3 = Base::instance();
// Don't write to STDOUT
$f3->set('QUIET', true);
$f3->route('GET /path', function(){ echo 'TEXT'; });
$this->assertNull($f3->mock('GET /path'));
$this->assertSame('TEXT', $f3->get('RESPONSE'));
$f3->route('GET /verify/@answer/@value',
function($f3, $params){
$errors = array();
$answer = $params['answer'];
$value = $params['value'];
$prefix = substr($answer, 0, 3); //pre, ans, pos
$id = (int)substr($answer, 3); //question id number (1, 2, 3, 4)
//$value is the input value from user
$result = check_id($prefix, $id, $value);
if($result !== true){ …Run Code Online (Sandbox Code Playgroud) 也许这只是我,但@depends似乎没有像我期望的那样工作.我的代码:
<?php
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
/*
* @depends testFunc1
*/
public function testFunc2()
{
exit('TEST FUNC 2 called');
}
public function testFunc1()
{
exit('TEST FUNC 1 called');
}
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,phpunit MyTest.php我希望看到,TEST FUNC 1 called但我明白了TEST FUNC 2 called.因为它似乎只是按照它们出现在脚本中的顺序运行测试,无论@depends属性如何,这确实引出了一个问题:@depends实际上做了什么?
我正在运行PHPUnit 5.7.20.
我正在尝试使用PHPUnit中的通用数据提供程序来运行一些测试。
参见以下测试:
namespace AppBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use AppBundle\Tests\DataProvider\XmlDataProvider;
class DefaultControllerTest extends WebTestCase
{
/**
* @dataProvider XmlDataProvider::xmlProvider
* @covers ReceiveController::receiveAction()
* @param string
*/
public function testReceive($xml)
{
$client = static::createClient([], ['HTTP_HOST' => 'mt.host']);
$client->request(
'POST',
'/receive',
[],
[],
[],
$xml
);
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我需要一个外部数据提供程序类:
namespace AppBundle\Tests\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class XmlDataProvider extends WebTestCase
{
/**
* @dataProvider
*/
public static function xmlProvider()
{
return array([
'xml1' => '<?xml version="1.0" encoding="UTF-8"?><myTestableXml></myTestableXml>'
]);
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行phpunit时,我得到:
1)警告为AppBundle …
我一直试图找出@ticketPHPUnit中的注释:
/**
* @ticket
* @return bool
*/
public function annotationTest()
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
我想用它来将功能链接到我们的票务软件中的票证,但我担心可能会有不同的用途.
文档(链接)中有一个条目,但它没有内容.我一直试图通过在线搜索找到更多,虽然我似乎无法找到答案.我认为它必须有目的吗?
什么是@ticket用于注释?
我运行phpunit时出现此错误
错误:调用未定义的方法测试\ Feature\ViewConcertListingTest :: see()
这是我的代码:
class ViewConcertListingTest扩展TestCase {使用DatabaseMigrations;
/** @test */
public function user_can_view_a_concert_listing()
{
// Arrange
// Create a concert
$concert = Concert::create([
'title' => 'The Red Chord',
'subtitle' => 'with Animosity and Lethargy',
'date' => Carbon::parse('December 13, 2016 8:00pm'),
'ticket_price' => 3250,
'venue' => 'The Mosh Pit',
'venue_address' => '123 Example Lane',
'city' => 'Laraville',
'state' => 'ON',
'zip' => '17916',
'additional_information' => 'For tickets, call (555) 555-5555'
]);
// Act
// View the concert listing
$this->get('/concerts/' …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试我的laravel应用程序,运行phpunit,但得到一个错误:
Error: Call to undefined method Illuminate\Hashing\BcryptHasher::driver()
Run Code Online (Sandbox Code Playgroud)
laravel 5.5,PHPUnit 7.0
我的用户测试:
namespace Tests\Feature;
use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class UserTest extends TestCase
{
use RefreshDatabase;
/** @test */
function name_should_not_be_too_long()
{
$response = $this->post('/users', [
'name' => str_repeat('a', 51),
'email' => $this->user->email,
'password' => 'secret',
]);
$response->assertStatus(302);
$response->assertSessionHasErrors([
'name' => 'The name may not be greater than 50 characters.'
]);
}
Run Code Online (Sandbox Code Playgroud)
}
我想从测试套件中排除或包括某些测试。我想通过注释/组对此进行一些控制,而不是在其中命名特定文件或文件夹phpunit.xml
我已经尝试过类似的操作,但是它似乎忽略了<groups>和/或<include>
<testsuites>
<testsuite name="Unit">
<directory>Unit</directory>
</testsuite>
<testsuite name="IntegrationFirstRound">
<directory>Integration/</directory>
<include><!-- I want to ONLY include this group -->
<group>first-round</group>
</include>
</testsuite>
<testsuite name="IntegrationOther">
<directory>Integration/</directory>
<exclude><!-- I want to EXCLUDE this group, run all others -->
<group>first-round</group>
</exclude>
</testsuite>
</testsuites>
Run Code Online (Sandbox Code Playgroud)
我不想将测试移到其他文件夹以适应此情况,并且我不想从CLI多次调用phpunit,我希望可以通过xml配置获得所需的结果。
phpunit ×10
php ×6
laravel ×2
symfony ×2
unit-testing ×2
annotations ×1
composer-php ×1
depends ×1
laravel-5.5 ×1
tdd ×1
travis-ci ×1