WordPress.org

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Logig:WordPress单元测试(安装 PHPUnit)

WordPress单元测试(安装 PHPUnit)

转载自 http://giuliodaprela.com/unit-tests-in-wordpress-installing-phpunit/

PHPunit 是一个 PHP 单元测试框架,我们将在 WordPress 中基于它测试 PHP 代码。我在网上找到三篇关于“如何安装和配置PHPUnit”的教程,这个过程很容易让你产生困惑,因为他们使用了不同的。。。

首先要明白的是,你不能单独使用PHPUnit。The first important aspect to understand regarding unit tests in WordPress is that you cannot use PHPunit alone. 如果你试图使用常规的PHPUnit方法,通常会失败。If you try to test your code using the standard PHPunit methods they will fail most of the time. 原因是,这些方法没有加载WordPress核心代码。所以你调用WordPress核心方法的时候会失败。The problem is that these methods don’t run the WordPress core files, so every time you call a WordPress core function your test fails. 几年前,为了解决这个问题,NB 写了一个库来hold住所有测试用例。To overcome this problem a few years ago Nikolay Bachiyski wrote a library that handled all of the tests that the WordPress core codebase uses. 这个库写的很好,被WordPress官方集成到了核心代码里面,现在已经成为WP的官方测试库。This library has been so much appreciated that it’s been integrated into core, and now it’s the official WordPress testing library.以至于NB的Github再也没更新过,所以不要以Github上的这个库为准。 As a result Nikolay’s repository on Github is not updated anymore, so don’t take his Github repo as a reference. In the end, to have working unit tests under WordPress you’ll have to install the WordPress tests library on top of the PHPunit test framework.为了在WP下进行单元测试,你需要安装。。。