Yii2 Codeception create a new test


(Shahzadthathal) #1

Hi there,

Today I go through with Yii2 Codeception tests, I see there are built in tests, I don’t understand the file structure.

Can you pls explain briefly how this tool is working?

When I execute commands:


codecept build

and


codecept run

What happens actually?

I see this output:


codecept build


Building Actor classes for suites: acceptance, unit, functional

 -> AcceptanceTesterActions.php generated successfully. 0 methods added

tests\codeception\frontend\AcceptanceTester includes modules: PhpBrowser, tests\codeception\common\_support\FixtureHelper

 -> UnitTesterActions.php generated successfully. 0 methods added

tests\codeception\frontend\UnitTester includes modules: 

 -> FunctionalTesterActions.php generated successfully. 0 methods added

tests\codeception\frontend\FunctionalTester includes modules: Filesystem, Yii2, tests\codeception\common\_support\FixtureHelper


codecept run


Codeception PHP Testing Framework v2.1.8

Powered by PHPUnit 5.3.4 by Sebastian Bergmann and contributors.


Tests\codeception\frontend.acceptance Tests (5) --------------------------------

Ensure that signup works (acceptance\SignupCest::testUserSignup)          Ok. 

Ensure that about works (AboutCept)                                       Ok

Ensure that contact works (ContactCept)                                   Ok

Ensure that home page works (HomeCept)                                    Ok

Ensure login page works (LoginCept)                                       Ok

--------------------------------------------------------------------------------


Tests\codeception\frontend.unit Tests (<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> --------------------------------------

Test contact (unit\models\ContactFormTest::testContact)                   Ok

Test send e (models\PasswordResetRequestFormTest::testSendEmailWrongUser) Ok.. 

Test send (models\PasswordResetRequestFormTest::testSendEmailCorrectUser) Ok.. 

Test reset wrong (unit\models\ResetPasswordFormTest::testResetWrongToken) Ok.. 

Test reset empty (unit\models\ResetPasswordFormTest::testResetEmptyToken) Ok.. 

Test reset cor (unit\models\ResetPasswordFormTest::testResetCorrectToken) Ok.. 

Test correct signup (unit\models\SignupFormTest::testCorrectSignup)       Ok.. 

Test not correct signu (unit\models\SignupFormTest::testNotCorrectSignup) Ok.. 

--------------------------------------------------------------------------------


Tests\codeception\frontend.functional Tests (5) --------------------------------

Ensure that signup works (functional\SignupCest::testUserSignup)          Ok. 

Ensure that about works (AboutCept)                                       Ok

Ensure that contact works (ContactCept)                                   Ok

Ensure that home page works (HomeCept)                                    Ok

Ensure login page works (LoginCept)                                       Ok

--------------------------------------------------------------------------------




Time: 26.13 seconds, Memory: 293.00MB


OK (18 tests, 84 assertions)

How I can create new test? I tried this: create a new method in frontend/controller/SiteController.php




public function actionMyTest(){

   return $this->render('myfile');

}



this is myfile.php


<?php


/* @var $this yii\web\View */


use yii\helpers\Html;


$this->title = 'My File';

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="site-my-file">

    <h2><?= Html::encode($this->title) ?></h2>


    <p>This is the About page. You may modify the following file to customize its content:</p>


    <code><?= __FILE__ ?></code>

</div>

and again I run commands:


codecept build

and


codecept run

but my new created method doesn’t show in test?

How I can test my new method?


 actionMyTest()

I really need to understand this tool, I checked also codeception original site.

Thank you for your time.


(Softark) #2

[color="#006400"]/* Moved from "General Discussions" to "Testing" */[/color]


#3

codecept build only needs to be run once on setup or config changes.

To create a new test simply create a new file, for example "MysiteCept.php" in "tests/codeception/acceptance/":


<?php

$I = new AcceptanceTester($scenario);

$I->wantTo('ensure that mytest page works');

$I->amOnPage(Yii::$app->homeUrl.'/site/mypage');

$I->see('My File');

if you run the following command, the new test will be run with all existing acceptance tests:


codecept run acceptance

Good book on yii2 testing:

Learning Yii Testing, Author: Matteo Pescarin, http://www.amazon.com/dp/1784392278?tag=gii20f-20


(Shahzadthathal) #4

Thanks it helped me.