我有什么:
WPKit/Module/AbstractFunctions.php
抽象类wp-content/themes/mytheme/modules/quiz/Functions.php
:use WPKitModuleAbstractFunctions; class Functions extends AbstractFunctions { ... }
-
wp-content/themes/mytheme/tests/quiz/QuizFunctionsTest.php
:require_once dirname(__FILE__) . "/../../modules/quiz/Functions.php"; class QuizFunctionsTest extends TestCase { public function testGetQuizByID() { # some code } }
运行phpunit QuizFunctionsTest.php
时,会出现以下错误:
PHP Fatal error: Class '%path%AbstractFunctions' not found in %path%/modules/quiz/Functions.php on line 18
我试着用require_once
补课,但没有用。在测试类之外,我的代码运行得很好。有什么想法吗?
TL;DR:我似乎无法让它工作,因为它是一个WordPress设置,所以我所做的是通过wget
安装PHPUnit,并使用WP-CLI进行主题测试,然后它就像一个魅力
首先,我从主题目录中的终端安装了PHPUnit
wget -O phpunit https://phar.phpunit.de/phpunit-7.phar
chmod +x phpunit
接下来我安装了WP-CLI:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
下一步设置测试套件:
wp scaffold theme-tests your-theme-slug
还要确保已安装svn
。
在我的情况下(我也发现有人提到了这一点(,我不得不删除/tmp/wordpress
和/tmp/wordpress-tests-lib
,并重新运行前一个命令两次,因为脚本不知何故没有下载所有内容。
请注意,您将在/tmp/wordpress
中有一个单独的WordPress设置。在我的主要WordPress设置中,我使用存储在wp-content
附近的WordPress根目录中的WPKit库,所以我必须将其复制到/tmp/wordpress
,否则我的PHPUnit测试无法找到我必须测试的类中包含的WPKit文件。
因此,当您运行wp scaffold
时,它会在主题根目录中创建bin
目录。从主题根目录运行以下命令以安装测试套件:
bin/install-wp-tests.sh <test-database-name> <user> <password> <host> <wordpress-version>
在我的情况下,它看起来像:
bin/install-wp-tests.sh wp_test root '' localhost latest
请确保使用空数据库,因为运行PHPUnit每次都会清除数据库,因此您可能会丢失数据。
在所有这些操作之后,只需运行PHPUnit,您就可以了:
./phpunit
它在your-theme/tests
目录中运行测试。默认情况下,它会忽略默认的测试文件,所以使用它来进行自己的简单测试,以检查一切是否正常。
Whew。
来源:
- https://www.kirstencassidy.com/testing-wordpress-plugins-wp-cli-phpunit/
- https://phpunit.de/getting-started/phpunit-7.html
- https://wp-cli.org/#installing
- https://www.smashingmagazine.com/2017/12/automated-testing-wordpress-plugins-phpunit/