加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

phpunit – Dataprovider可以从setUp获得连接

发布时间:2020-12-13 13:31:31 所属栏目:PHP教程 来源:网络整理
导读:通过setUp()连接到数据库失败 class ChanTest extends PHPUnit_Framework_TestCase{ protected $db; protected function setUp() { $this-db = new CoreDatabase('unitest'); } /** * @dataProvider testProvider */ public function testData($a,$b,$c) {
通过setUp()连接到数据库失败
class ChanTest extends PHPUnit_Framework_TestCase
{
    protected $db;

    protected function setUp()
    {
        $this->db = new CoreDatabase('unitest');
    }

    /**
     * @dataProvider testProvider
     */
    public function testData($a,$b,$c)
    {
        $this->assertEquals($a + $b,$c);
    }

    public function testProvider()
    {
        $this->db->query('SELECT `a`,`b`,`c` FROM `units`');

        return $this->db->rows();
    }
}

连接到数据库本身工作

class ChanTest extends PHPUnit_Framework_TestCase
{
    protected $db;

    protected function setUp()
    {
        $this->db = new CoreDatabase('unitest');
    }

    public function testData($a,$c)
    {
        $this->db->query('SELECT `a`,`c` FROM `units`');

        foreach ($this->db->rows() as $item) {
            $this->assertEquals($item['a'] + $item['b'],$item['c']);
        }
    }
}

如果我通过dataProvider通过setUp函数连接数据库,它会响应致命错误:调用成员函数query(),但如果连接到数据库本身有效,dataProvider可以获取setUp函数的设置吗?

这是设计的:为了确定测试的数量,PHPUnit在实际运行测试(和setUp方法)之前运行dataProviders.

从manual on DataProviders:

Note:
All data providers are executed before both the call to the setUpBeforeClass static method and the first call to the setUp method. Because of that you can’t access any variables you create there from within a data provider. This is required in order for PHPUnit to be able to compute the total number of tests.

在你的情况下,我会为数据库使用单例/实例模式.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读