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]];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题