2015-08-09 19 views
10

Gerçekten bununla bir tuğla duvara koşuyorum. Phpunit'teki testler arasında sınıf değerlerini nasıl geçersiniz?phpunit: Testler arasında değerleri nasıl geçebilirim?

Testi 1 -> setleri değeri,

Testi 2 -> Burada değerini

okur benim kodudur:

class JsonRpcBitcoinTest extends PHPUnit_Framework_TestCase 
{ 
    public function setUp(){ 
     global $configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort; 

     $this->bitcoindConn = new JsonRpcBitcoin($configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort); 
     $this->blockHash = ''; 
    } 

    /** 
    * @depends testCanAuthenticateToBitcoindWithGoodCred 
    */ 
    public function testCmdGetBlockHash() 
    { 
     $result = (array)json_decode($this->bitcoindConn->getblockhash(20)); 
     $this->blockHash = $result['result']; 
     $this->assertNotNull($result['result']); 
    } 

    /** 
    * @depends testCmdGetBlockHash 
    */ 
    public function testCmdGetBlock() 
    { 
     $result = (array)json_decode($this->bitcoindConn->getblock($this->blockHash)); 
     $this->assertEquals($result['error'], $this->blockHash); 
    } 
} 
testCmdGetBlock() ayarlanmış olmalıdır $this->blockHash değerini almıyor

testCmdGetBlockHash(). Neyin yanlış olduğunun anlaşılmasında yardımcı olunması çok takdir edilecektir.

cevap

18

setUp() yöntemi, her zaman testlerden önce çağrılır; bu nedenle, iki test arasında bir bağımlılık oluştursanız bile, setUp()'da belirtilen değişkenlerin üzerine yazılır. PHPUnit veri işleri geçen yolu diğerinin parametreye bir testin dönüş değerinden geçerli:

class JsonRpcBitcoinTest extends PHPUnit_Framework_TestCase 
{ 
    public function setUp(){ 
     global $configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort; 

     $this->bitcoindConn = new JsonRpcBitcoin($configRpcUser, $configRpcPass, $configRpcHost, $configRpcPort); 
     $this->blockHash = ''; 
    } 


    public function testCmdGetBlockHash() 
    { 
     $result = (array)json_decode($this->bitcoindConn->getblockhash(20)); 
     $this->assertNotNull($result['result']); 

     return $result['result']; // the block hash 
    } 


    /** 
    * @depends testCmdGetBlockHash 
    */ 
    public function testCmdGetBlock($blockHash) // return value from above method 
    { 
     $result = (array)json_decode($this->bitcoindConn->getblock($blockHash)); 
     $this->assertEquals($result['error'], $blockHash); 
    } 
} 

Yani, testler arasında daha durumunu kaydetmek o yöntemde daha fazla veri gerekirse. PHPUnit'in bu canı sıkıcı hale getirmesinin nedeninin bağımlı testler yapmaktan vazgeçeceğini tahmin ediyorum.

See the official documentation for details.

+0

Fantastic! Çok teşekkür ederim, bunu nasıl almadığım hakkında hiçbir fikrim yok. – Drazisil

6

Bir işlev içinde statik bir değişken kullanabilirsiniz ... PHP rahatsız edici hisseleri tüm örnekleri ile sınıf yöntemlerinin statik değişkenler ... Ama bu yardımcı olabilir cas de: p

protected function &getSharedVar() 
{ 
    static $value = null; 
    return $value; 
} 

... 

public function testTest1() 
{ 
    $value = &$this->getSharedVar(); 

    $value = 'Hello Test 2'; 
} 


public function testTest2() 
{ 
    $value = &$this->getSharedVar(); 

    // $value should be ok 
} 

NB: Bu iyi bir yol değil ama tüm testlerde bazı verilere ihtiyacınız varsa yardımcı olur ...

+0

Bu çalışmalardan etkilendim! –

+0

Sınıfa ihtiyacınız olduğunda veya sınıftaki tüm testler için yapılandırmayı ayarladığınızda çok yardımcı olun. Sınıftaki tüm testlerden yalnızca bir kez * setUpPage * gibi oturum içeriğine sahip bir yöntem yoktur. Em ben miyim? –

+0

Nihayetinde * public static işlevi setUpBeforeClass() * 'ı buldum, ancak test sınıfı başlatılmadan önce bu bir çağrılıyor. –