Codeception (安装)

来源:http://codeception.com/install  

注意:打开Codeception的官网需要翻墙

1. 下载

下载地址:http://codeception.com/thanks

或者使用命令行

wget http://codeception.com/codecept.phar

通过composer安装

php composer.phar require "codeception/codeception:*"

注意 Codeception2.0需要PHP 5.4。如果你的PHP版本是5.3,需要安装Codeception1.8.x

2. 安装

在当前的codecept.phar目录,打开命令行执行

php codecept.phar bootstrap

初始化目录结构后。会发现多出了test目录和codeception.yml文件。

很明显codeception.yml是配置文件。可以设置测试过程中的输出路径,数据库信息等。

3. 创建测试

我们来生成一个验收测试,来模拟一个真实的用户访问网站。

php codecept.phar generate:cept acceptance Welcome

Codeception scenario tests are called Cepts.

Codeception 的场景测试被称作Cepts。

4. 写测试代码

打开刚刚生成的tests/acceptance/WelcomeCept.php

<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that frontpage works');
$I->amOnPage('/'); 
$I->see('Home');
?>

该测试会检测当前网站的首页是否包含了Home文字

5. 配置验收测试

确保本地服务器正在运行。打开tests/acceptance.suite.yml 。

修改{YOUR APP'S URL}的内容,比如我的是 http://yii2demo.finley.com/

class_name: AcceptanceGuy 
modules: 
enabled: [PhpBrowser, AcceptanceHelper]
config: 
  PhpBrowser:
      url: '{YOUR APP'S URL}'
          

6. 运行

执行

php codecept.phar run

通过验收测试我们可以检测HTML页面内容,点击链接,提交表单,提交POST和GET请求。更复杂的测试需要安装Selenium。

查看测试结果:

Suite acceptance started 
Trying to ensure that frontpage works (WelcomeCept.php) - Ok
Suite functional started
Suite unit started

Time: 1 second, Memory: 21.00Mb
OK (1 test, 1 assertions)
原文地址:https://www.cnblogs.com/mafeifan/p/4297729.html