Gii should generate Unit tests as well

As the title says I think its a good idea if the generators would not only generate the production code itself but also the initial unit tests. Its quite a big burden and from a more philosophical point of view also in injury of the agile manifesto if I generate 10.000 lines of code without generating the tests for it, as well as writing the tests for this 10.000 lines of code upfront because that would violate "Write only as much test code as needed to fail". In my opinion the only solution really is generating the unit tests aside. What do you think?

I think that’s a good idea. But I wouldn’t include any tests, just create the basic “skeleton” for own tests including fixture files for models (again, no data needed, just a fixture array that can easily be changed by the developer). These are the steps you would do anyway in 99,9% of all cases. Why no initial test creation? Because then you are effectively testing Yii if I understand you correctly, not your own code and one should assume that Yii is properly tested already.

EDIT: Wait. There are parts that are frequently customized by the developer and therefore could need an initial test like attributeLabels() etc.

I’d guess the Yii unit tests are testing the code generators and Yii classes itself. But everything generated for you is not Yii core but part of your own projects code base afterwards. Consequently there should be generated unit tests that covers all the generated code. Of course if you never ever touch the code being generated there would be no added value above testing the validity of the generators itself. This is a quite unrealistic scenario, and as soon as you change anything in the generated production code, you are in danger of breaking something at an unexpected place. Therefore changing generated code instantly leads to the need for unit test coverage of the generated code.

Furthermore I agree with you that it would be an amazingly helpful feature, to generate fixture skeletons for ActiveRecords as well.

I see and I am totally with you.

don’t think this problem should be solved by core team, gii is extensible so yii users can solve such particular problems (as phpunit/codeception/behat test mocks generation) and share their solutions as extensions

some people prefer to extend generated classes with their own, in this case unit tests for generated classes are rather undesirable

I don’t think so. There are many reasons why this should be done, the two most important:

  • it’s a feature that every (serious) application needs

  • it enforces people - who don’t already do so - to create unit tests, leading to more stable software overall.

The problem is that it forces me to use PHPUnit - and I really don’t like it much.

Extend Gii if you want.

That’s exactly the reason why it’s extendable.

I don’t want anything enforced on me. ;)

Well, there can be few issues. What if somebody follow TDD practice, which means that tests should be generated first, so, generating tests when models/controllers are created will not help.

I also think it is more safe when you generating tests manually, because sometime you don’t want to test some functions/features, that might already be tested, or for example you want to have two test function for one say model function(for example with regular and with invalid parameters, in case when your tests are very big, and you want to split them into two tests).

I think I know a solution: PHPUnit has an addition called sekeleton generator. All it does is to generate a skeleton full of test methods marked as incomplete. I think gii could provide a template featuring the very same, as the skeleton generator’s code is pretty simple. It forces you to nothing but will allow you to rapidly create the skeletons out of gii without having to touch PHPUnit.

use yiic