Codeception Testing with Yii Framework 2.0 [Day 3]

Today’s Objective

As a user I would like to run tests on the basic template for Yii Framework. (Continued)

Today’s method

Going to use the instructions from the yii2 basic template (after everything set up from yesterday) and just add any notes of my own or any issues I run into.

Instructions from basic template

Install faker extension by running the following from template root directory where “composer.json” is:

composer require --dev yiisoft/yii2-faker:*

Template root directory in this instance is “C:\wamp\www\codeception-test”

Create “yii2_basic_tests” database and update it by applying migrations:

codeception/bin/yii migrate

Ran into an error when trying to execute this instruction. My thought is that it’s only necessary if my application used the database (Which isn’t the case for the basic template). But this points out that I should learn how to properly use migrations for Yii Framework.

 

Very nifty to be able to set up a test database with a simple execution, think this will be another tutorial/learning series after this one.

Build the test suites:

codecept build

Executed from C:\wamp\www\codeception-test\tests

In order to be able to run acceptance tests you need to start a webserver. The simplest way is to use PHP built in webserver. In the “web” directory execute the following:

php -S localhost:8080

Since I already have a webserver set up, I chose to skip this step and instead set up the tests to use my webserver instead by editing

C:\wamp\www\codeception-test\tests\codeception.yml

replace

http://localhost:8080/index-test.php

to

http://localhost/codeception-test/web/index-test.php

There’s a way to remove the “web” part out of that, which I have, but it’s out of scope for this… possibly later ;)

Now you can run the tests with the following commands:

# run all available tests
codecept run
# run acceptance tests
codecept run acceptance
# run functional tests
codecept run functional
# run unit tests
codecept run unit

Found that acceptance tests failed, but functional and unit tests completed successfully. Believe the issue with my acceptance tests is thanks to a configuration setting and not using PHP built in webserver.

 

Needed to edit

C:\wamp\www\codeception-test\tests\codeception\acceptance.suite.yml

replace

http://localhost:8080

to

http://localhost/codeception-test/web

Conclusion

Hurrah! We’ve successfully run tests on the basic template for Yii Framework. With that completed there are a few questions I have:

  1. Is there a way to execute tests and view results from a browser? The reason for this question is thanks to the following:
    • Don’t enjoy the idea of executing command line arguments after each change
    • Don’t like the output from windows command line, believe it would look better in unix… but I’m not using unix.
  2. What’s the best practice for unit tests?
    • Does it entail testing function calls on model only, or controller and model. Believe this will be answered with a look into the current tests provided.
  3. What’s the best practice for acceptance/functional tests?
    • This is more my lack of understanding for the process than anything else and requires further education

And that’s about it. With those questions I can make an objective for tomorrow :)

Tomorrow’s Stumble

As a user I would like to create a new unit test, implement it and test it.

One thought on “Codeception Testing with Yii Framework 2.0 [Day 3]”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>