Unit testing components

Has anyone ever tried to unit test a component? I’m getting a ‘Class not found’ error when I add use app\components\MyComponent in my test file.

Is there some place I need to add app\components to the codeception path or something?

Still struggling with your old problem ;)


Maybe "app" is not declared while running tests?

Yiis app alias is set automatically when running as webapp, maybe not while running the tests?!

Here are some test examples:

No, app is declared. app/models/Something works perfectly.

Unit tests for apps work as well, so it’s not that.

Do you (or does anyone) know where include paths are defined for tests? Maybe it’s just a question of adding app\components to a config file or something? If so, which?

(and as for my old problem: ignoring _before and _after did the trick. Oh well)


most likely it is just a namespace problem. Here is a small example for unit testing components:

If you are using basic template:

  1. Inside the root of your project create components folder and store your components there. For example if the name of my project is basic, my component MyComponent.php will be in:


And you will have to namespace it with

namespace app\components;

  1. inside the tests/codeception/unit folder create components folder and store your components unit tests there.

So if you have MyComponentsTest class, you will store it there. Make sure you namespace it correctly and that you are using your basic/components/MyComponent.php correctly.

namespace tests\codeception\unit\components;

use app\components\MyComponent;

If you are using advanced template, you must know that there is no app namespace. Instead your root namespaces are frontend, backend, common…

So if you want to create frontend components, you will create components folder inside frontend folder and namespace your classes with frontend\components and use it accordingly.

Thanks for this overview, exactly what I was looking for!