跟踪 PHP 中先前的方法调用
用例:我正在尝试为我的单元测试框架创建一个诊断测试,该框架运行每个测试用例,然后返回测试用例尚未涵盖的公共方法列表。这个想法是通过确保每个方法的所有公共方法都经过至少 1 个单元测试来测试,从而提高测试覆盖率。
问题:我无法检测类中的方法是否已被调用。我尝试过使用 debug_backtrace() 函数,但它只返回来自测试框架的调用,而不返回正在测试的类方法。我可以获得正在运行的方法名称的唯一方法是将 debug_backtrace() 函数实际放置到被测试类的方法中,这是一个不可接受的选项,因为我必须将它放在每个类的每个公共方法中。
是否有另一种方法可以进行回溯以获取测试的类方法?或者,采用稍微不同的方法,是否有一种方法可以启动和停止侦听器,该侦听器将记录正在调用的所有方法(我可以解析出任何不属于的内容)?
伪代码:
runTest($testName){
//run the test
//get all recent classes and methods used
//parse out test framework info
//compare used public methods to public method list for the tested class
}
或者:
runTest($testName){
//start listener
//run the test
//close listener, get all recent classes and methods used
//parse out test framework info
//compare used public methods to public method list for the tested class
}
Use Case: I'm trying to create a diagnostic test for my unit testing framework that runs each test case and then returns a list of public methods which have not yet been covered by the test case. The idea is to get increased test coverage by making sure that all public methods of each are tested by at least 1 unit test.
Problem: I'm having trouble detecting if a method from a class has been called. I've tried using the debug_backtrace() function, but it only returns calls from the test framework, not the class method being tested. The only way I can get the method name being run is to actually place the debug_backtrace() function into the method of the tested class, which is an unacceptable option because I would have to have it in every public method of every class.
Is there an alternate way of doing the backtrace to get the tested class method? Or, in a slightly different approach, is there a way to start and stop a listener that will record all methods that are being called (I can parse out anything that doesn't belong)?
Pseudo-code:
runTest($testName){
//run the test
//get all recent classes and methods used
//parse out test framework info
//compare used public methods to public method list for the tested class
}
Or:
runTest($testName){
//start listener
//run the test
//close listener, get all recent classes and methods used
//parse out test framework info
//compare used public methods to public method list for the tested class
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅PHP 代码覆盖率工具、Xdebug 2:代码覆盖率分析 和 Sebastian Bergmann 的 PHP_CodeCoverage 了解解决问题的一些常见方法。
你想要的通常是从外部观看 php 来完成。使用
debug_backtrace()
既慢又,正如您自己所说,不能完全依赖,因为您确实想要一个不会修改 php 脚本任何行为的观察程序(例如包括您的测试)回溯中的方法)。See Code Coverage tools for PHP, Xdebug 2: Code Coverage Analysis and Sebastian Bergmann's PHP_CodeCoverage for some common ways to address your issue.
What you want is usually done watching php from the outside. Using
debug_backtrace()
is both slow and, as you said yourself, can't be fully relied on since you really want a watcher that does not modify any behavior of your php script (for example including your test methods in the backtrace).请参阅我们的 PHP 测试覆盖率工具。它在程序运行时收集测试覆盖率数据,而不使用 xdebug。它通过临时将检测插入到代码中来实现这一点,例如,在测试过程期间。
您将获得测试覆盖率数据,可在漂亮的 UI 中叠加在代码上显示,这样您就可以看到覆盖了什么(另一个 OP 指出可能会执行 a 方法,但没有有效覆盖)。您还会收到一份报告,指示未涵盖的内容,该报告与您的代码层次结构相匹配。它的 XML 版本包含您想要的信息:某个方法是否被覆盖以及覆盖到什么程度。
See our PHP Test Coverage Tool. It collects test coverage data as your program runs, without use of xdebug. It does so by inserting instrumentation into your code temporarily, e.g., for the duration of the testing process.
You get back test coverage data, displayable superimposed on the code in a nice UI, so you can see what is covered (another OP pointed out the a method might be executed, but not usefully covered). You also get back a report indicating what did not covered, that matches the hierarchy of your code. The XML version of that contains the information you want: such-and-such a method was/was not-covered and to what degree.