2015-05-21 61 views
2

我正在用Codeception構建API驗收測試。如何使用Codeception進行API驗證測試,以鉤入beforeSuite事件?

我熟悉單元測試,並且在運行該類的所有測試之前,我在這些類的所有邏輯中使用了setUp方法。

但是我沒有在Acceptance Tests中找到類似的東西。

請注意,我使用的是「類」方法,而不是程序方式。

所以我有這樣一類...

class ResourceCest { 
    public function _beforeSuite(ApiTester $I) 
    { 
     // Ideally this would work, but it doesn't. 
    } 

    public function _before(ApiTester $I) 
    { 
     $I->am('Api Tester'); 
    } 
    public function somethingThatIWantToExecute(ApiTester $I) 
    { 
     $I->sendGet('something'); 
     // etc 
    } 
} 

我可以像設置一個方法,但隨後Codeception執行它作爲一個測試,因此在運行測試時,輸出的東西。

回答

4

您不應該在Cest類中定義_beforeSuite。相反,你應該使用_support中的Helper類。

假設你有一個叫做api的套件,你應該在_support裏有一個ApiHelper.php類。在那裏,你可以定義你的方法,例如:

<?php 
namespace Codeception\Module; 

// here you can define custom actions 
// all public methods declared in helper class will be available in $I 

class ApiHelper extends \Codeception\Module 
{ 
    public function _beforeSuite($I) { 
     var_dump($I); 
     die(); 
    } 
} 

這應該有所斬斷。

+2

不適合我。取而代之的是'$ I'被定義爲'AcceptanceTester',我在'_beforeSuite'方法中得到了一個配置數組 – FelikZ

+0

$ settings array被傳入_beforeSuite。如果你想要$,那麼你做$ I = $ this ;. – dwenaus