bdvs / wordpress-integration-tester
WordPress的非用户/基于UI的集成测试
dev-master
2021-07-16 09:23 UTC
This package is auto-updated.
Last update: 2024-09-16 23:18:46 UTC
README
WordPress的自动集成测试框架。
为什么需要?
在创建WordPress插件时,有两种类型的测试:
- 单元测试 - 通常使用PHPUnit
- 集成测试 - 手动进行,使用Selenium或其他工具测试基于用户的交互
然而,还有一种介于两者之间的第三种情况
- 单元测试,但当你的一些函数依赖于数据库中的数据存在时,无论是由于用户交互的结果,还是更常见的是,由另一个第三方插件放置的数据,可能是基于通过WordPress CMS输入的设置和内容。
WordPress集成测试器应运而生 - 允许使用DB中的数据进行WordPress插件的自动化集成测试。
安装
通过Composer: composer require bdvs/wordpress-integration-tester:dev-master
(注意:在开发期间仅提供dev-master)
依赖
- WP CLI
入门
- 创建一个新的插件来存放测试,或将包直接添加到插件中
- 创建一个扩展WIT Test类的测试类
- 在终端中运行
$ wp wit run
- 查看结果
创建一个扩展WIT Test类的测试类
/**
*
* My first WIT Test Class
*
**/
class My_First_WIT_Test_Class extends WIT_Test {
/**
*
* Run set up before each test
*
* @param void
* @return void
*
**/
protected function __setup() {
//do stuff before tests start
//note that functions that start with __ are not considered test functions
parent::__setup();
}
/**
*
* Close down and reset post-test
*
* @param void
* @return void
*
**/
protected function __close_down() {
//do stuff when tests are done
//note that functions that start with __ are not considered test functions
//e.g. reload the database as it was before you ran tests...
$this->__load_db( plugin_dir_path( __FILE__ ) . 'dbs/test-db.sql' );
parent::__close_down();
}
/**
*
* My first WIT Test
*
* @param void
* @return void
*
**/
public function my_first_wit_test() {
$this->__are_equal( array(
'expected' => true,
'actual' => true,
'description' => 'Tests that pass show a pass in the console',
));
$this->__are_equal( array(
'expected' => true,
'actual' => false,
'description' => 'Tests that fail show a fail in the console',
));
}
}
在终端中运行wp wit run并查看结果
有了上述类并包含在内,在终端中运行 $ wp wit run
将输出如下表格
类 | 测试 | 成功 |
---|---|---|
My_First_WIT_Test_Class | 通过的控制台显示为通过 | 通过 |
My_First_WIT_Test_Class | 失败的控制台显示为失败 | 失败 |
贡献
如果您有兴趣为该项目做出贡献或有任何问题或反馈,请在此存储库中提出问题。