Unit Testing CodeIgniter with Simpletest - very few tests

In our development team, we decided to try Unit Testing. We use Simpletest. However, it was not an easy road. After a week, I created only 1 unit test, which checks for a specific helper file. It. The rest (controllers, models, views, libraries) do not yet have unit tests. And I plan not to test most of them. Representations, for example, are too trivial to test, so I refuse testing. Next, the controllers. I plan that my controllers will not do complex things, so this is only the transfer of information between models and views. I would move these more complex things to libraries or helpers.

Now for my questions:

1) Am I doing it wrong? So far, there is nothing more that I can see that this may be erroneous, so a unit test will be needed. Most of the stuff (right now) is just CRUD.
2) Do we really need unit test controllers? Since the controller job is just a little processing of the data transferred between the View and Model, I find very little initiative in unit testing.
3) If I use WebTestCase for testing controllers, will this be considered Unit Test? Or is it already an integration test?
4) Suppose you made me test my controller, how would I test it? As far as I know, CI follows the Front Controller pattern via index.php, so how can I handle (mock?) What?

+4
source share
2 answers

Are you doing something wrong? I do not think so.

Do we really need unit test controllers? I do not. Maybe I should. It seems like a lot of work.

If I use WebTestCase for testing controllers, will this be considered Unit Test? Or is it already an integration test? WebTestCase will be an interesting approach to testing controllers if any significant output is detected; for example, detecting an error when calling / some / specific / path.

Suppose you made me test my controller, how would I test it? That's cool. You will probably need to initialize part of the application environment in order to do something worthwhile.

In most articles / books, you need to define your tests before starting coding. I may have tried this, but I'm usually too impatient. This seems to interfere with rapid prototyping, but perhaps defining unit tests is a way to quickly prototype.

I found that deciding what to test with PHP is a problem. I think you need to fight. If it is critical that the method returns a value of a particular type, then check for this. If a lot of things happen when you instantiate an object, you can also check this out.

In general, what I am doing - right or wrong - is to get everything to work, and then create basic tests, and then add tests as needed based on any problems I encounter. The idea is to never have a problem with repetition, and each test ensures that the application behaves the same throughout its life cycle.

As for the features, I am using the Zend Framework, but it will be similar to CodeIgniter. I also use SimpleTest (but with my own class wrapper). I can or cannot model unit test, and I have never implemented tests for controllers or views; It seems like too much work and too little good. Most frameworks crash early, and therefore the problems are usually obvious. But any common library code makes easier goals and errors here - especially logical errors - harder to detect. Set up your tests to make sure everything works as expected so that your system-specific code encounters several problems.

+3
source

If you are still open with a suggestion for another unit test for CodeIgniter, I suggest you try Toast . I found it easy to use and did not interfere with my development process.

I use only unit test to test my library, helper and model. My controller does not have a lot of code, it only receives the post and uri parameter, disinfects it with trim or intval, passes it to the library or model, and then passes the result for viewing.

The view has almost no testing code, as it displays everything in the browser. Basically, it just requires debugging css and js.

A model almost always needs testing because it deals with data. Without unit test, it was difficult for me to track some errors, especially with complex calculations.

The library and assistant perform a repetitive task, so it needs a unit test to make sure that the logic in it works correctly.

I hope this help.

+4
source

All Articles