使用 phpunit 测试遗留代码

发布于 2024-10-30 20:40:53 字数 101 浏览 5 评论 0原文

我有一个遗留代码库,我需要使用 PHPUnit 测试该代码。所以我想根据您的经验寻求建议。我应该首先测试哪些课程?还是优先?

我应该从简单/小班开始还是从基础/超级班开始?

I have a legacy codebase and I need to test that code with PHPUnit. So I am asking for suggestions based on your experiences. Which classes I should test first? Or give the priority?

Should I start with the easy/small classes or with the base/super class?

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

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

发布评论

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

评论(3

遮了一弯 2024-11-06 20:40:53

我对向现有代码库引入单元测试的一般建议如下:

  1. 开始测试一些非常简单的类,以了解编写测试的感觉
  2. ,甚至可以再次重写这些测试并制作适当的“这就是我们应该这样做”的示例其中
  3. 选取整个系统中最大和最卑鄙的类之一,并尽可能地对该类进行测试。这是向团队中的每个人(也许还有管理层)展示代码库单元测试可以解决并且可行的重要一步。

之后,我建议您专注于三件事:

  • 确保新代码得到测试
  • 如果您修复了错误,则在修复之前创建一个测试以“证明”错误实际上已修复
  • 当您触摸/更改旧代码时,使用测试作为工具代码,以便您获得更好的测试覆盖率。

PHPUnit 将为您提供一份代码覆盖率报告,显示您的代码库的测试情况。看到这个数字在一个月内从 0.3% 上升到 5% 再到 20% 是一件很酷的事情,但这并不是一个真正强大的动力。

为了确保您测试新代码,我建议使用 PHP_Change_Coverage 作为 在此博客文章中描述

这个工具将为您提供帮助很多生成有意义的覆盖率报告,因为它只显示新创建的代码为未经测试,而不是您周围的所有旧内容。

有了这个,你手头就有了一些东西,可以很容易地“很早就获得高百分比并继续测试新东西”,同时为所有旧东西创建测试。

PHP 变更覆盖范围之前:
http://qafoo.com/blog/images/phpccov_without_timerange.png

及之后:
http://qafoo.com/blog/images/phpccov_with_timerange.png

My general suggestion for introducing unit testing to an existing codebase would be the following:

  1. Start of testing some very simple classes to get a feel for writing tests
  2. Maybe even rewrite those tests again and make proper "this is how we should do it" examples out of them
  3. Take the one of the biggest and meanest classes in the whole system and get that class tested as best as you can. This is an important step to show everyone in your team (and maybe management) that unit testing your codebase CAN WORK OUT and is doable.

After that I'd suggest that you focus on three things:

  • Make sure new code gets tests
  • If you fix a bug create a test before fixing it to "prove" the bug is actually fixed
  • Use tests as a tool when you touch/change old code so you get better an better test coverage.

PHPUnit will provide you with a CodeCoverage Report showing you how well your codebase is tested. It can be pretty cool to see the number rise from 0.3% to 5% to 20% over the course of month but it not a really strong motivator.

To make sure you test NEW code I'd suggest using PHP_Change_Coverage as described in this blog posting

This tool will help you a lot generating meaningful coverage reports as it only shows NEWLY CREATED CODE as UNTESTED and not all the old stuff you have lying around.

With that you have something at hand that makes it really easy to "get a high % very early and keep testing the new stuff" while you create tests for everything old.

Before PHP Change Coverage:
http://qafoo.com/blog/images/phpccov_without_timerange.png

and After:
http://qafoo.com/blog/images/phpccov_with_timerange.png

停顿的约定 2024-11-06 20:40:53

系统中通常有太多代码,无法作为第一步进行全部测试。但大部分代码已经可以工作了。

我将从最近修改的方法开始。据推测,该软件的其余大部分都在某种程度上起作用,并且测试不太可能发现与新代码或新修订的代码中发现的错误一样多的错误。

如果您没有工作(我怀疑在不久的将来,如果您附近有 1 个或多个开发人员正在积极工作),您可以升级到使用已修改方法的方法根据软件指标以及对安全系统操作至关重要的方法(使用密码登录、存储客户费用数据等)具有较高复杂性。

帮助决定下一步考虑测试什么的一种方法是使用测试覆盖率工具。通常,人们使用它来确定软件的测试情况如何,但是如果您没有进行很多已知的测试,它会告诉您:您的代码没有经过很好的测试:-{因此,尽早运行它是没有意义的在您的测试构建过程中。 (当您进行更多测试时,您和您的经理最终会想知道这一点)。然而,测试覆盖率工具也倾向于提供已执行或未作为测试一部分执行的代码的完整列表,并且提供了关于您应该测试节点的线索:具有的代码>没有被行使。

我们的 SD PHP 测试覆盖率工具 可与 PHP 配合使用,并通过以下方式提供此信息:和交互式查看器以及生成的报告。它将告诉您哪些方法、类、文件和子系统(按目录层次结构)已经过测试以及测试的程度。如果名为“login.php”的文件尚未经过测试,您将能够轻松看到这一点。与简单地根据您对代码的了解进行猜测相比,这种明确的视图可以更轻松地智能地决定下一步要测试什么。

There's often too much code in a system to test it all as a first step. But most of that code already works.

I'd start with methods that were modified recently. Presumably most of the rest of the software works to some degree, and testing that won't likely find as many errors as will be found in new or newly revised code.

Should you run out of work (I doubt it in the near future if you have 1 or more developers actively working near you), you can move up to methods that use the methods that were modified, to methods that have high complexity according to software metrics, and to methods that are critical for safe system operation (login with password, storage of customer charge data, etc.)

One way to help decide what to consider testing next is to use a test coverage tool. Normally one uses this to determine how well tested the software is, but if you don't have many tests you already know with it will tell you: your code isn't very well tested :-{ So there's no point in running it early in your test construction process. (As you get more tests you and your managers will eventually want to know this). However, test coverage tools also tend to provide complete lists of code that has been exercised or not as part of your tests, and that provides a clue as to what you should test node: code that has not been exercised.

Our SD PHP Test Coverage tool works with PHP and will provide this information, both through and interactive viewer and as a generated report. It will tell you which methods, classes, files, and subystems (by directory hierarchy) have been tested and to what degree. If the file named "login.php" hasn't been tested, you'll be able to easily see that. And that explicit view makes it much easier to intelligently decide what to test next, than simply guessing based on what you might know about the code.

蓝戈者 2024-11-06 20:40:53

看看 PHPure

他们的软件记录了正在运行的 php 网站的输入和输出,然后自动编写 phpunit 测试对于不访问外部源(数据库、文件等)的函数。

这不是 100% 的解决方案,但这是一个很好的开始

Have a look at PHPure,

Their software records the inputs and outputs from a working php website, and then automatically writes phpunit tests for functions that do not access external sources (db, files etc..).

It's not a 100% solution, but it's a great start

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