使用 Simpletest 对 CodeIgniter 进行单元测试 - 很少的测试

发布于 2024-08-11 05:50:56 字数 501 浏览 3 评论 0原文

在我们的开发团队中,我们决定尝试一下单元测试。我们使用简单测试。然而,这是一条艰难的路。一周后,我只创建了 1 个单元测试来测试某个帮助文件。就是这样。其余的(控制器、模型、视图、库)还没有单元测试。我计划不测试其中的大多数。例如,视图对于测试来说太微不足道了,所以我放弃了对其进行测试。接下来是控制器。我计划我的控制器不做复杂的事情,这样它只在模型和视图之间传递信息。我会将那些更复杂的东西移至库或助手中。

现在我的问题是:

1)我做错了吗?到目前为止,我没有发现任何可能出错的地方,因此需要进行单元测试。大多数东西(现在)只是 CRUD。
2)我们真的需要对控制器进行单元测试吗?由于控制器的工作只是对视图和模型之间传递的数据进行少量处理,因此我发现对其进行单元测试很少有主动性。
3)如果我使用WebTestCase来测试控制器,这仍然被认为是单元测试吗?或者它已经是集成测试了?
4)假设你让我测试我的控制器,我将如何测试它?据我所知,CI 通过index.php 遵循前端控制器模式,那么我将如何处理(模拟?)呢?

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.

Now for my questions:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦情居士 2024-08-18 05:50:56

如果您仍然对 CodeIgniter 的另一个单元测试持开放态度,我建议您尝试 Toast。我发现它很容易使用,并且不会对我的开发过程造成太大干扰。

我只使用单元测试来测试我的库、助手和模型。我的控制器没有太多代码,只获取 post 和 uri 参数,使用 trim 或 intval 对其进行清理,将其传递给库或模型,然后将结果传递给视图。

视图几乎没有需要测试的代码,因为它将所有内容显示到浏览器。大多数情况下,它只需要 css 和 js 调试。

模型几乎总是需要测试,因为它处理数据。如果没有单元测试,我发现很难追踪一些错误,特别是在复杂的计算中。

库和助手执行重复性任务,因此需要进行单元测试以确保其中的逻辑正确执行工作。

我希望这有帮助。

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

I only use unit test to test my library, helper, and model. My controller not have much code, only get post and uri parameter, sanitizing it using trim or intval, pass it to library or model, then pass the result to view.

View almost have none code to test, since it display everything to browser. Mostly, it just need css and js debugging.

Model almost always need testing, since it dealing with data. Without unit test, I found it difficult to trace some bug, specially with a complex computation.

Library and helper do a repetitive task, so it need a unit test to make sure the logic in it is doing the work correctly.

I hope this help.

靑春怀旧 2024-08-18 05:50:56

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

我们真的需要对控制器进行单元测试吗?我不知道。也许我应该。不过,看起来工作量很大。

如果我使用 WebTestCase 来测试控制器,这仍会被视为单元测试吗?或者它已经是集成测试了?如果可以检测到一些有意义的输出,WebTestCase 将是一种测试控制器的有趣方法;例如,检测调用/some/specific/path时没有发生错误。

假设你让我测试我的控制器,我将如何测试它?这是一项艰难的任务。您可能需要初始化部分应用程序环境才能做任何有价值的事情。

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

我发现决定用 PHP 测试什么是一个挑战。我认为你必须选择你的战斗。如果某个方法返回特定类型的值至关重要,那么请对其进行测试。如果实例化对象时会自动发生很多事情,您也可以对此进行测试。

一般来说,我所做的(无论对错)是让一切正常工作,然后创建一些基本测试,然后根据我遇到的任何问题根据需要添加测试。我们的想法是永远不会出现重复的问题,并且每次测试都将确保应用程序在其生命周期中表现相同。

至于具体细节,我使用的是 Zend Framework,但在 CodeIgniter 中它会类似。我还使用 SimpleTest (但使用我自己的类包装器)。我可能会也可能不会对模型进行单元测试,并且我从未对控制器或视图实现过测试;似乎工作太多而收益太少。大多数框架都会“早期失败”,因此问题通常是显而易见的。但任何通用库代码都会使目标变得更容易,并且这里的错误(尤其是逻辑错误)更难以检测。设置测试以确保一切按预期工作,以便特定于框架的代码很少遇到问题。

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.

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文