dongli8466 2018-08-30 10:28
浏览 190

在setupBeforeClass之后有没有办法执行dataprovider函数?

I have a unit test class in which I want to instantiate a object from another class in order to that I used setUpBeforeClass() fixtures of phpunit. So if I will use that recently instantiated object directly in test function then its working fine.

If i'll use this object into another function which had been created for data providers. So that object sets to null cause providers always execute first.

Is there a way to call dataProviders just before the test runs, instead?

require_once('Dashboard.php');
Class Someclass extends PHPUnit_Framework_TestCase {

    protected static $_dashboard;
    public static function setUpBeforeClass()
    {
        self::$_dashboard = new Dashboard();
        self::$_dashboard->set_class_type('Member');
    }
    /**
     * Test Org Thumb Image Existense
     * param org profile image : array
     * @dataProvider getOrgProfileImages
     */
    public function testFieldValidation($a,$b){
        //If I call that object function here it will give the result.
        //$members = self::$_dashboard->get_members();
       //var_dump($members); Printing result as expected
        $this->assertTrue(true);
    }

    public function getOrgProfileImages() : array {
        //var_dump(self::$_dashboard);
        $members = self::$_dashboard->get_members();
        $tmp_array = ['2','2'];
        return $tmp_array;

    }

    public static function tearDownAfterClass()
    {

        self::$_dashboard = null;
    }
}

Error: The data provider specified for Someclass::testFieldValidation is invalid. Call to a member function get_members() on null

Please help to mitigate this issue.

  • 写回答

2条回答 默认 最新

  • douwen6274 2018-08-30 21:30
    关注

    Note: since I don't have the source of your Dashboard class, I'm using a random number in the examples below instead

    Providers are invoked before any tests are run (and before any hooks, including beforeClass have a chance to run). By far the easiest way to achieve what you're after is to populate that static property on the class load:

    use PHPUnit\Framework\TestCase;
    
    /** @runTestsInSeparateProcesses enabled */
    class SomeTest extends TestCase
    {
        public static $_rand = null;
    
        public function provider()
        {
            $rand = self::$_rand;
            var_dump(__METHOD__, getmypid(), 'provided rand', $rand);
            return ['rand' => [$rand]];
        }
    
        /** @dataProvider provider */
        public function testSomething($rand)
        {
            $this->expectNotToPerformAssertions();
            var_dump(__METHOD__, getmypid(), 'tested with', $rand);
        }
    
        /** @dataProvider provider */
        public function testSomethingElse($rand)
        {
            $this->expectNotToPerformAssertions();
            var_dump(__METHOD__, getmypid(), 'tested with', $rand);
        }
    }
    
    // this runs before anything happens to the test case class
    // even before providers are invoked
    
    SomeTest::$_rand = rand();
    

    Or you could instantiate you dashboard in the provider itself, on the first call:

    public function provider()
    {
        // Instantiate once
        if (null === self::$_rand) {
            self::$_rand = rand();
        }
        $rand = self::$_rand;
        var_dump(__METHOD__, getmypid(), 'provided rand', $rand);
        return ['rand' => [$rand]];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备