How do you unittest your modules?

Hello

I have been using Yii unittest implementation and it works great without changing any of the defaults. Recently I started writing some modules within my application. But so far I cannot access any of the module’s models within the unit test framework.

I am fairly certain that my config/main.php which is included in config/test.php imports my module. It works when you access the controller actions from a browser.

Given the above two claims, what am I missing. Can some one point me in the right direction?

Note: I have also noticed that none of the logging (trace/log) functions work when you are running in unit test mode for a normal non-module based application. Could this be related?

hello - i think your problem is, that you haven’t defined the include-path of that modules

either you need to specify it inside:

protected/config/main.php and set the import-path

(note: that is something different, than specifying the existance of your module inside modules…




return array('import'=>array('application.modules.hello.models.*'))



or (so did i) set the import in the beginning of your test with:

Yii::import(‘application.modules.hello.models.*’);

the reason why it works for non-module models is, cause the config already imports them with:

application.models.*

You are right. Thanks for the explanation. For some reason I had the believe that the ‘module’ config option took care of that.

If I am unit testing my modules, is it a good idea to place the testing scripts inside the related module?

If I were to place the testing scripts together inside related module, is it possible to run both module test and non-module test together?

I am also interested in solution that allows me to place unit tests for module inside module directory and test everything together (app + all modules that have tests).

Does anybody have an idea or such solution already working?

I’m using phing for “manual continuous-integration” ;).

For each module, I put tests/phpunit.xml and tests/bootstrap.php in the module and write there the unit test files.

In my /build.xml, I put




        <target name="test" description="Launch the unittests">

                <phpunit bootstrap="protected/tests/bootstrap.php" printsummary="false">

                        <formatter type="plain" usefile="false"/>

                        <batchtest>

                                <fileset dir="protected/tests/">

                                        <include name="unit/*Test.php"/>

                                </fileset>

                                <fileset dir="protected/modules/">

                                        <include name="*/tests/unit/*Test.php"/>

                                </fileset>

                        </batchtest>

                </phpunit>

        </target>



So, I just have to type phing test to test the modules and the main application.

But now, the update to PHPUnit-3.5.0 broke the integration in Phing, I’ll have to return to the previous PHPUnit.

@Eliovir: nice example - will give it a chance :)

thanks.

Updating from phing-2.4.1 to phing-2.4.4 solved the problem.

Is it possible to test whether a module’s properties are set correctly?

Because while running unit tests, Yii::app()->controller->module is not available.

Yii::app()->controller is available in a controller.

Did you try


Yii::app()->getModule('moduleName')

?

Yes it worked. Thank you.

Check this out guys

http://radzserg.com/2011/12/21/unit-tests-for-yii-modules/

Thanks logity, it work fine…