使用 phpunit 测试遗留代码
我有一个遗留代码库,我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对向现有代码库引入单元测试的一般建议如下:
之后,我建议您专注于三件事:
PHPUnit 将为您提供一份代码覆盖率报告,显示您的代码库的测试情况。看到这个数字在一个月内从 0.3% 上升到 5% 再到 20% 是一件很酷的事情,但这并不是一个真正强大的动力。
为了确保您测试新代码,我建议使用 PHP_Change_Coverage 作为
在此博客文章中描述
这个工具将为您提供帮助很多生成有意义的覆盖率报告,因为它只显示
新创建的代码为未经测试
,而不是您周围的所有旧内容。有了这个,你手头就有了一些东西,可以很容易地“很早就获得高百分比并继续测试新东西”,同时为所有旧东西创建测试。
PHP 变更覆盖范围之前:
及之后:
My general suggestion for introducing unit testing to an existing codebase would be the following:
After that I'd suggest that you focus on three things:
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:
and After:
系统中通常有太多代码,无法作为第一步进行全部测试。但大部分代码已经可以工作了。
我将从最近修改的方法开始。据推测,该软件的其余大部分都在某种程度上起作用,并且测试不太可能发现与新代码或新修订的代码中发现的错误一样多的错误。
如果您没有工作(我怀疑在不久的将来,如果您附近有 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.
看看 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