codeception 基础使用[简译]

来源:互联网 发布:手机淘宝店怎么改头像 编辑:程序博客网 时间:2024/05/28 19:24
codeception 基础使用[简译]
当我们使用 codeception 页面对象代码时,这些代码扩展自 BasePage 类。这样可以减少些重复。yii2-app-basic 和 yii2-app-advanced 里都有。


TestCase 类有一些常见的单元测试功能,如应用创建前测试,消毁后测试。也可以配置应用类。TestCase 类扩展自 Codeception\TestCase\Case ,所有方法和用例都是可用的。你也可以使用 codeception 模块和事件在你的测试代码中。


取得 codeception 模块
如果你要使用 codeception 模块和帮助在你的单元测试中,你要这样做:


<?php
#in your unit-test
$this->getModule('CodeHelper'); #or some other module


你也可以使用 actor 方法:


<?php
$this->unitTester->someMethodFromModule();


Codeception 事件
调用事件:


<?php
use Codeception\Event\TestEvent;


public function testSomething()
{
    $this->fire('myevent', new TestEvent($this));
}


这个事件可以调用模块和帮助。如果你的测试在组中。事件名称后面要跟着写组名,例如:myevent.somegroup。


简单的测试方法


执行测试方法:


tests\unit\models\UserTest::setUpBeforeClass();


    tests\unit\models\UserTest::_before();


        tests\unit\models\UserTest::setUp();


            tests\unit\models\UserTest::testSomething();


        tests\unit\models\UserTest::tearDown();


    tests\unit\models\UserTest::_after();


tests\unit\models\UserTest::tearDownAfterClass();


如果执行简单测试方法不要忘了调用其父类。


原文:https://github.com/yiisoft/yii2-codeception/blob/master/docs/guide/basic-usage.md

0 0
原创粉丝点击