API测试
目录说明
codecept包目录:D:\tests\codecept.phar
测试项目的目录:D:\tests\test1
命令优化
执行命令格式:PHP_PATH CODECEPT_PATH [-command] arg.....
通常我们会设置个php
的环境变量执行命令的代码变为php CODECEPT_PATH [-command] arg.....
,感觉这样也还是太麻烦了,我们可以再新建一个环境变量名为cept
,值为PHP_PATH CODECEPT_PATH
,这样我们每次执行命令的格式为
%cept% [-command] arg
这样使用就方便很多
建立项目
D:\tests> %cept% bootstrap test1 --namespace test1
配置
配置D:\tests\test1\api.suite.yml
,可能你并没有看到有这个配置文件,创建测试项目的时候并没有自动生成这个API测试组件的配置文件,这是你需要生成这个文件,并且使用ApiTester
类
D:\tests> %cept% g:suite api -c D:\tests\test1
命令g:suite
生成新的组件,参数-c
指定项目的配置文件
接下来配置api.suite.yml
,codeception提供了REST, SOAP, XML-RPC这三个接口类型的模块,按需使用
class_name: ApiTester
namespace: \test1\api
modules:
enabled:
- \test1\Helper\Api
- PhpBrowser:
url: 'http://user.ufx.com/'
- Asserts
- Db:
dsn: 'mysql:host=localhost;dbname=youfangxin'
user: 'root'
password: root
# dump: tests/_data/dump.sql
配置参数详解:
namespace
:设置命名空间很重要,generate命令生成文件的时候会根据这个配置生成命名空间
生成Cest类
D:\tests>%cept% g:cest api Base -c test1
运行之后会在D:\tests\test1\tests\api
目录生成BaseCest
类,作为公共的类。我们打开这个文件看代码
<?php
namespace test1;
use test1\ApiTester;
class BaseCest
{
public function _before(ApiTester $I)
{
}
public function _after(ApiTester $I)
{
}
// tests
public function tryToTest(ApiTester $I)
{
}
}
执行测试
%cept% run D:\tests\test1\api