我想在特定Cest中的所有测试之前运行一些东西,然后在所有测试运行后清理它,类似于PHPUnit中的setUpBeforeClass和tearDownAfterClass方法.
有没有一种方法可以在Codeception中做这样的事情?
小智 6
在 Codeception 人员为您提供可靠的方法之前,我现在对这个问题有一个粗略的解决方案。
只需在所有现有演员(测试用例)之上创建另一个演员,如下所示:
class MyCest
{
function _before(AcceptanceTester $I)
{
$I->amOnPage('/mypage.php');
}
public function _after(AcceptanceTester $I)
{
}
function beforeAllTests(AcceptanceTester $I,\Page\MyPage $myPage,\Helper\myHelper $helper){
//Do what you have to do here - runs only once before all below tests
//Do something with above arguments
}
public function myFirstTest(AcceptanceTester $I){
$I->see('Hello World');
}
function afterAllTests(){
//For something after all tests
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将 beforeAllTests 函数设为 public 但不受保护,也不应以“_”开头,因为它会在所有测试之前运行。
另一组在所有测试开始前只运行一次的函数,应该在 /tests/_support/Helper/Acceptance.php 中实例化以供接受等等。在这个你可以调用函数:
// HOOK: used after configuration is loaded
public function _initialize()
{
}
// HOOK: before each suite
public function _beforeSuite($settings = array())
{
}
Run Code Online (Sandbox Code Playgroud)
更多功能请访问:https : //codeception.com/docs/06-ModulesAndHelpers#Hooks
从 Codeception 的角度来看,Cest 类只是一堆 Cept 场景。没有对象作用域,也没有类之前/之后的挂钩。
我的建议是改用 Test 格式并使用 PhpUnit 挂钩。
测试格式扩展了 PHPUnit_Framework_TestCase,因此 setUpBeforeClass 应该可以工作。