phpunit: How do I pass values between tests?

前端 未结 4 544
礼貌的吻别
礼貌的吻别 2021-01-03 19:48

I\'m really running into a brick wall with this. How do you pass class values between tests in phpunit?

Test 1 -> sets value,

Test 2 -> reads value

H

4条回答
  •  攒了一身酷
    2021-01-03 20:19

    You can use a static variable within a function... PHP annoyingly shares static variables of class methods with all the instances... But in this cas it can help :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: this is NOT the good way but it helps if you need some data in all your tests...

提交回复
热议问题