codeception (4)Yii2下创建Acceptance Tests(验收测试)

以登录为例创建一个验收测试,名为IndexPage
图片描述
打开文件里的的代码是

<?php 
use tests\codeception\frontend\AcceptanceTester;
$I = new AcceptanceTester($scenario);
$I->wantTo('perform actions and see result');

文件创建成功了,接下来我们要想一下登陆的流程
1.访问登陆页面
2.填写登陆信息
3.点击登陆按钮
既然是验收测试,那我们测试的数据肯定不止一种情况,并且页面上应该返回错误提示,才算正确
1.什么都不填,直接提交,页面上应该提示不能为空
2.填写错误的信息,提交,页面上应该提示,用户名或密码不正确
3.填写正确的信息,提交,跳转至能明显看出用户是已登录状态的页面

访问登陆页面,假设我们的登陆地址是http://login.test.com,我们找到对应的suite.yml文件,
每种actor都对应一个suite.yml文件,在tests/codeception/frontend下有一个名为acceptance.suite.yml的文件,这个文件就是验收测试的配置文件,我们更改url

# Codeception Test Suite Configuration

# suite for acceptance tests.
# perform tests in browser using the Selenium-like tools.
# powered by Mink (http://mink.behat.org).
# (tip: that's what your customer will see).
# (tip: test your ajax and javascript by one of Mink drivers).

# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.

class_name: AcceptanceTester
modules:
    enabled:
    -PhpBrowser
    -tests\codeception\common\_support\FixtureHelper
# you can use WebDriver instead of PhpBrowser to test javascript and ajax.
# This will require you to install selenium. See http://codeception.com/docs/04-AcceptanceTests#Selenium
# "restart" option is used by the WebDriver to start each time per test-file new session and cookies,
# it is useful if you want to login in your app in each test.
#        - WebDriver
    config:
        PhpBrowser:
# PLEASE ADJUST IT TO THE ACTUAL ENTRY POINT WITHOUT PATH INFO
            url: http://login.test.com
#        WebDriver:
#            url: http://localhost:8080
#            browser: firefox
#            restart: true

配置好了登陆地址,我们就可以开始写代码了

$I = new AcceptanceTester($scenario);
$I->wantTo('perform actions and see result');
$I->amOnPage('/');   
$I->see('登录'); //找到登录两个字,说明访问的登陆地址是正确的

测试第一种情况,什么都不填

$I->amGoingTo('submit login form with no data');
$I->fillField('input[name="LoginForm[t_email]"]', '');
$I->fillField('input[name="LoginForm[t_password]"]', '');
$I->click('登录');//点击登录按钮
$I->expectTo('see validations errors');
$I->see('用户名不能为空。', '.help-block');//希望看到用户名的错误提示
$I->see('密码不能为空。', '.help-block');//希望看到密码的错误提示

第二种情况,填写错误的登陆信息

$I->amGoingTo('try to login with wrong credentials');
$I->fillField('input[name="LoginForm[t_email]"]', 'admin');
$I->fillField('input[name="LoginForm[t_password]"]', 'wrong');
$I->click('登录');//点击登录按钮
$I->expectTo('see validations errors');
$I->see('用户名或密码不正确。', '.help-block');//希望看到的错误提示

第三种情况,填写正确的信息

$I->amGoingTo('try to login with correct credentials');
$I->fillField('input[name="LoginForm[t_email]"]', 'info@aim-china.com');
$I->fillField('input[name="LoginForm[t_password]"]', '888888');
$I->click('登录');
$I->expectTo('see that user is logged');
$I->seeLink('退出');//希望看到退出链接
$I->dontSeeLink('登录');//不希望看到登录链接

运行测试
图片描述
全部运行成功,在测试中,只要有一个断言不成功,就会显示失败,

在执行测试的时候,可以生成报告
将运行结果以xml的形式保存下来,黄字的部分就是xml的位置
codecept run acceptance IndexPageCept --xml --html

生成的报告会在tests/codeception/frontend/_output下
图片描述

执行下面这句可以知道更多codeception的指令

codecept help run

参考文章:http://blog.csdn.net/huoba1/article/details/41719699

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值