ride / test
PHPUnit、Selenium 和 Ride 之间的桥梁
Requires
- php: ^7.0
- facebook/webdriver: ^1.3
- fzaninotto/faker: ^1.6
- larapack/dd: ^1.1
- phpunit/phpunit: ^5.0
- phpunit/phpunit-selenium: ^3.0
- ride/app: ^1.0
- ride/lib-cli: ^1.0
- ride/lib-orm: ^1.0
- ride/lib-system: ^1.1
- se/selenium-server-standalone: ^3.0
This package is auto-updated.
Last update: 2024-09-13 00:37:40 UTC
README
这个库在 PHPUnit、Selenium 和 Ride 之间架起了桥梁。
composer require --dev ride/test
安装
此模块尚未触发 composer 安装后脚本。因此,您应该手动将 dist/
文件夹复制到项目根目录。您还应该修改 parameters.php
文件,让 Ride 知道何时使用测试环境。关于这一点稍后介绍。
测试环境配置
您可以使用 application/config/test/
用于特定于测试的参数、路由和依赖。
测试环境使用 SQLite 数据库。每个测试从主模板复制此数据库,确保每个测试用例都有一个干净的数据库实例。您可以使用 SQLite 浏览器向干净的测试数据库添加数据:application/test/clean.test.db
。您还可以创建其他数据库,并将它们用作单个测试用例的模板。这是通过扩展 RideTestCase
并调用 $this->cleanDatabase('path/to/my/clean/test.db')
来实现的。当前测试用例使用的数据库可以在 application/test/test.db
中找到。
创建干净数据库
此库启用 testdb generate
命令。当运行时,该命令将根据 MySQL 连接生成 SQLite 数据库。可以按模型复制特定模型数据,并指定一个文件以保存 SQLite 数据库。
cli testdb generate --file=test/clean.test.db Dealer User TaxonomyTerm Mail
运行此命令在早期开发阶段非常有用,当时您仍在更新 models.xml。
PHPUnit 测试
您的测试类应该保存在 application/test
文件夹中。它们可以扩展正常的 PHPUnit TestCase,或者扩展 ride\testing\RideTestCase
。这样做使 Ride 的 System
和 DependecyInjector
可用于您的测试。请注意,当您重写这些方法时,您应该始终调用 parent::setUp()
和 parent::tearDown()
。
可以使用以下命令运行测试。
vendor/bin/phpunit # Or, if PHPUnit is installed globally phpunit # Or, a specified test file phpunit application/test/statik/orm/model/MyModelTest.php
Selenium 测试
与 PHPUnit 测试一样,有一个可扩展 Selenium 测试类的类可用:ride\testing\SeleniumRideTestCase
先决条件
Selenium 需要 Java 和 JDK 来运行;您可以在以下位置找到当前 JDK 的下载链接:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html。
此外,您还需要一个特定的浏览器来运行测试。某些版本的 Firefox 可以直接使用,但有些不行。如果您在 Mac 上且想确保安全,此库的 dist/bin
中包含了一个可用的 Chrome 驱动程序。如果您想使用其他驱动程序,您可以在这里查找:http://www.seleniumhq.org/about/platforms.jsp。
要告诉 Ride 在运行 Selenium 测试时必须使用测试环境,您应该在环境检测后向 application/config/parameters.php
中添加以下行:
if (file_exists(__DIR__ . '/../../.selenium')) { $environment = "test"; }
运行测试
Selenium 服务器在运行测试时应始终在后台运行。服务器在测试过程中还会记录大量有用的信息。
# Run Selenium in the background, with the provided Chrome driver. ./selenium-server-chrome.sh # Or run selenium with your own driver. sh vendor/se/selenium-server-standalone/bin/selenium-server-standalone -Dwebdriver.chrome.driver=bin/chromedriver
# Run all selenium tests. phpunit --testsuite selenium # Or run a specific test. phpunit --testsuite selenium --filter test_inquiry_form_submit