【问题标题】:How to share fixtures between tests in PHPUnit如何在 PHPUnit 中的测试之间共享固定装置
【发布时间】:2015-10-02 13:53:34
【问题描述】:

我正在做 WordPress 功能测试,所以我阅读了 HTML 并使用 Mastermind/HTML5 来转换测试。但是,现在的测试变得越来越慢,因为每次测试加载 HTML 文档大约需要 1 秒。我想在测试之间共享夹具,所以我不必为每个测试进行解析。但是我有一个限制,从中获取 html 的方法是在父类中,它是非静态方法

https://core.trac.wordpress.org/browser/trunk/tests/phpunit/includes/testcase.php?rev=32953#L328

我有什么选择在测试之间共享夹具。

这是我的示例代码

class Testcase extends WP_UnitTestCase {

    public function setUp() {
        parent::setUp();
    }

    public function get_dom( $path ) {
        $html = $this->go_to( $path ); // I cannot change this method
        // do some html parsing and return DOM
    }

}

这是我的示例测试

class Testcase1 extends Testcase {
     public setUp(){
          $this->dom = $this->get_dom('/')
     }
     public test_1() {
     }

     public test_2() {
     }
}

我正在考虑将方法 get_dom 设为静态,因此它只会被调用一次,但据我所知静态方法不能调用非静态方法。我对么?如果是的话,我可以在测试之间共享夹具吗?

【问题讨论】:

    标签: php wordpress performance unit-testing


    【解决方案1】:

    你的意思是缓存数据“dom”吗?试试这个:

    public function setUp() {
         static $dom;
         if (!isset($dom)) {
             $dom = $this->get_dom('/');
         }
    
         $this->dom = $dom;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-21
      • 2011-10-01
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多