单元测试CodeIgniter与Simpletest - 很少的测试 [英] Unit Testing CodeIgniter with Simpletest - very few tests

查看:182
本文介绍了单元测试CodeIgniter与Simpletest - 很少的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的开发团队,我们决定尝试单元测试。我们使用Simpletest。然而,这是一条艰难的道路。一个星期后,我只创建了1个单元测试来测试某个帮助文件。而已。其余(控制器,模型,视图,库)没有单元测试。我计划不测试他们中的大多数。例如,视图太轻易无法测试,所以我继续测试。接下来,控制器。我计划我的控制器不做复杂的东西,所以它只是在模型和视图之间传递信息。

On our development team, we decided to give Unit Testing a try. We use Simpletest. However, it's been a tough road. After a week, I only have created 1 unit test that tests a certain helper file. That's it. The rest (controllers, models, views, libraries) don't have unit tests yet. And I plan to not test a majority of them. Views, for example, are too trivial to test, so I pass up on testing that. Next, the controllers. I plan my controllers to not do complex stuff, so that it only does passing of information between the models and the views. I'd move those more complex stuff to libraries or helpers.

现在为我的问题:

1)我做错了吗?到目前为止,没有什么我可以看到,可以是错误的,所以它需要一个单元测试。大多数东西(现在)只是CRUD。

2)我们真的需要单元测试控制器吗?由于控制器的作业只是对在View和Model之间传递的数据的轻微处理,我发现在单元测试中没有主动性。

3)如果我使用WebTestCase测试控制器,那仍然被认为是单元测试?还是已经是集成测试?

4)假设你让我测试我的控制器,我该如何测试呢?据我所知,CI通过index.php跟随Front Controller模式,所以我将如何处理(mock?)?

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

推荐答案

你做错了什么吗?我不这么认为。

Are you doing something wrong? I don't think so.

我们真的需要单元测试控制器吗?我不。也许我应该。看起来很多工作。

Do we really need to unit test controllers? I don't. Maybe I should. Seems like a lot of work, though.

如果我使用WebTestCase来测试控制器,那仍然被认为是单元测试?还是已经是集成测试? WebTestCase将是一个有趣的方法来测试控制器,如果可以检测到一些有意义的输出;例如,检测到在调用/某些/特定/路径时没有发生错误。

If I use WebTestCase to test for controllers, would that be still considered a Unit Test? Or is it already an integration test? WebTestCase would be an interesting approach to testing controllers if some meaningful output could be detected; for example, detecting that no error has occurred when calling /some/specific/path.

假设你让我测试我的控制器,我该如何测试?这是一个艰难的。

Suppose you got me to test my controller, how would I test it? That's a tough one. You would presumably need to initialize part of the application environment in order to do anything worthwhile.

大多数文章/书籍都会告诉您在之前定义测试 >你开始编码。也许我已经尝试过,但我通常太不耐烦。这似乎阻碍了快速原型设计,但也许定义单元测试是快速原型的一种方式。

Most articles/books tell you to define your tests before you start coding. Maybe I've tried that, but I'm usually too impatient. It seems to get in the way of quick prototyping, but maybe defining the unit tests is a way of quick prototyping.

我发现决定使用PHP测试是一个挑战。我想你必须选择你的战斗。如果一个方法返回特定类型的值是至关重要的,那么就测试一下。如果在实例化一个对象时自动发生很多事情,你也可以测试一下。

I've found that deciding what to test with PHP is a challenge. I think you have to pick your battles. If it's critical that a method return a value of specific type, then test for that. If a lot of things happen automatically when instantiating an object, you could test for that too.

一般来说,我所做的 - 对或错 - 是一切工作,然后创建一些基本测试,然后根据我遇到的任何问题根据需要添加测试。这个想法是永远不会有重复的问题,每个测试将确保应用程序在其生命周期中的行为相同。

In general, what I do -- right or wrong -- is get everything working, then create some basic tests and then add tests as needed based on any problems I encounter. The idea is to never have a repeat problem and each test will insure that the application behaves the same through its life-cycle.

至于具体细节,我使用Zend框架,但它会在CodeIgniter类似。我也使用SimpleTest(但是用我自己的类封装)。我可能或可能不单元测试模型,我从来没有实现过控制器或视图的测试;它似乎太多的工作和太少的好处。大多数框架早期失败,因此问题通常是显而易见的。但是任何通用的库代码都使得更容易检测的目标和错误,特别是逻辑错误。设置测试以确保一切正常工作,以便特定于框架的代码遇到很少的问题。

As for specifics, I'm using Zend Framework, but it will be similar in CodeIgniter. I also use SimpleTest (but with my own class wrapper). I may or may not unit test models and I have never implemented tests for controllers or views; it's seems like too much work and too little benefit. Most frameworks "fail early" and so issues are typically obvious. But any common library code make for easier targets and bugs here -- especially logic bugs -- are harder to detect. Set up your tests to make sure things work as expected so that your framework-specific code encounters few problems.

这篇关于单元测试CodeIgniter与Simpletest - 很少的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆