目前我们运行代码来在setUpBeforeClass中设置数据库.但是,这会在测试每个测试类之前运行.是否可以在运行任何测试之前运行一次代码,并且在所有测试完成后也可以运行一些代码?
如何登录内部测试以便能够执行特定于用户的操作?
我正在尝试(很长一段时间,在PHP聊天室的fellas的帮助下)成功地将PHPUnit与PhpStorm集成.
我已将phpunit.xml文件设置如下:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "bootstrap.php" >
<testsuites>
<testsuite name="Lamed Test Suite">
<directory>Custom/*</directory>
</testsuite>
</testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
并成功配置PHP风暴以从该文件中读取.
问题是,我在运行测试时在PhpStorm的控制台中收到以下错误:
D:\Websites\php\php.exe C:\fakepath\ide-phpunit.php --bootstrap D:\Websites\htdocs\lamed\tests\boostrap.php --configuration D:\Websites\htdocs\lamed\tests\phpunit.xml
Testing started at 23:51 ...
Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Neither "Lamed Test Suite.php" nor "Lamed Test Suite.php" could be opened.' in …Run Code Online (Sandbox Code Playgroud) 因此,每当我在PHPUnit中遇到意外异常时(例如由于完整性检查而无法插入到db中),我的测试会失败,并且在没有运行tearDownAfterClass()函数的情况下会出错.这将使我的数据库处于凌乱的状态,因此我必须手动去清理它.有没有办法确保tearDownAfterClass()始终执行?
我在Symony2中创建了一个非常简单的REST控制器,其中包含控制器操作中的数据库插入/更新/删除.
是否有一种很好的方法可以为这些控制器操作编写单元/集成测试而不会污染生产数据库?我是否必须使用不同的环境 - 或者是否有来自框架供应商的建议方法?
电流控制器示例:
public function postAction()
{
$json = $this->getRequest()->getContent();
$params = json_decode($json);
$name = $params->name;
$description = $params->description;
$sandbox = new Sandbox();
$sandbox->setName($name);
$sandbox->setDescription($description);
$em = $this->getDoctrine()->getManager();
$em->persist($sandbox);
$em->flush();
$response = new Response('/sandbox/'.$sandbox->getId());
$response->setStatusCode(201);
return $response;
}
Run Code Online (Sandbox Code Playgroud)
目前的测试示例:
class SandboxControllerTest extends WebTestCase
{
public function testRest()
{
$client = static::createClient();
$crawler = $client->request('POST', '/service/sandbox', array(), array(), array(), json_encode(array('name' => 'TestMe', 'description' => 'TestDesc')));
$this->assertEquals(
201, $client->getResponse()->getStatusCode()
);
}
}
Run Code Online (Sandbox Code Playgroud) 我试图用Mockery模拟Eloquent模型.模型正在Controller中注入
__construct(Post $model){$this->model=$model}
Run Code Online (Sandbox Code Playgroud)
现在我find()在控制器中调用该函数
$post = $this->model->find($id);
Run Code Online (Sandbox Code Playgroud)
这里是PostsController的测试
class PostsTest extends TestCase{
protected $mock;
public function setUp() {
parent::setUp();
$this->mock = Mockery::mock('Eloquent', 'Posts'); /*According to Jeffrey Way you have to add Eloquent in this function call to be able to use certain methods from model*/
$this->app->instance('Posts', $this->mock);
}
public function tearDown() {
Mockery::close();
}
public function testGetEdit()
{
$this->mock->shouldReceive('find')->with(1)->once()->andReturn(array('id'=>1));
$this->call('GET', 'admin/posts/edit/1');
$this->assertViewHas('post', array('id'=>1));
}
}
Run Code Online (Sandbox Code Playgroud)
运行PhpUnit给我错误:
Fatal error: Using $this when not in object context in ...\www\l4\vendor\mockery\mockery\library\Mockery\Generator.php(130) : …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的小型框架应用程序编写一些PHPUnit测试,但是在文档中没有看到任何指向完整请求和断言响应的方法(包含文本或200状态,或者什么,真的).
有没有办法做到这一点,任何人都找到/使用过?
我的任务是调查持续集成,我正在研究的一件事是Gitlab CI.
我已经设置了Gitlab,Gitlab CI和两个跑步者,但我绝对坚持如何真正使用它.我怎么能做一些事情,比如创建一个单元测试,将其推送到repo,并让其中一个跑步者测试它?
或者我完全忽略了这一点?我是这个CI的新手(因为我昨天被要求这样做,这是我第一次接触CI)所以如果我错过了这一点,请告诉我并指出我一些资源.
非常感谢.
我在写信-
<?xml version="1.0" encoding="UTF-8"?>
Run Code Online (Sandbox Code Playgroud)
<testsuites>
<testsuite name="My Test Suite">
<directory suffix=".php">Test/Case/Model</directory>
<exclude>Test/Case/Model/Behavior</exclude>
</testsuite>
</testsuites>
Run Code Online (Sandbox Code Playgroud)
但它不排除覆盖率报告中的行为.如何从覆盖率报告中排除此目录或文件?
我有一个TestSuite,我需要将其标记为跳过(整个测试套件 - 而不是套件中的特定测试用例).
class AllTests
{
public static function suite()
{
// this does not work same as within TestCase:
// throw new \PHPUnit_Framework_SkippedTestError("Out of order");
$Suite = new \PHPUnit_Framework_TestSuite(__NAMESPACE__);
$Suite->addTestSuite(translators\AllTests::cls());
$Suite->addTestSuite(TlScopeTest::cls());
$Suite->addTestSuite(TlNsTest::cls());
$Suite->addTestSuite(TlElementTest::cls());
$Suite->addTestSuite(TlItemTest::cls());
$Suite->addTestSuite(LangCodeTest::cls());
$Suite->addTestSuite(TlElemClassTagTest::cls());
return $Suite;
}
}
Run Code Online (Sandbox Code Playgroud)
如你所见,抛出PHPUnit_Framework_SkippedTestError异常不起作用.它不会被PHPUnit捕获,并且会将执行中断为任何未捕获的异常(这是可以理解的,因为suite()在构建测试层次结构时,在实际运行测试之前调用该方法).
我见过一个名为的异常类PHPUnit_Framework_SkippedTestSuiteError,但不知道如何利用它.有任何想法吗?
我有一个TestSuite,它聚合了许多测试用例以及其他测试套件.几乎所有的测试都失败了,因为我在代码的核心部分做了一些改变.
问题是这个包不是重要的,并且计划在以后修复.在那之前,我必须为每个其他软件包运行测试,但是当我这样做时,PHPUnit输出会充满来自相关软件包的错误.这会强迫我每次检查是否有任何其他软件包发生故障.
正如您可能怀疑的那样,这很容易受到人为错误的影响,即我可能会错过一个实际上非常重要的失败.
我只能运行我目前正在使用的测试套件,但是我无法控制一个软件包中的更改是否导致其他软件包出现故障.
我不想评论那个测试套件,因为我担心我(或者会在我之后接管代码的人)可能完全忘记它.
phpunit ×10
php ×4
unit-testing ×4
symfony ×2
cakephp ×1
git ×1
gitlab ×1
integration ×1
laravel ×1
laravel-4 ×1
phpstorm ×1
slim ×1
symfony-2.1 ×1