在 PHP 中独立运行单元测试
我用 PHP 创建了一个基本的单元测试套件。它显示测试是否通过以及执行测试所花费的时间。它工作得很好,但我遇到了一个问题,我不知道如何解决它。每个单元测试类的每个初始单元测试似乎总是比该类中的其他单元测试花费更多的时间来执行,通常是一个重要因素。
例如:
TestClass1
- Test1.1 (0.042395)
- Test1.2 (0.000392)
- Test1.3 (0.000325)
- Test1.4 (0.000645)
TestClass2
- Test2.1 (0.042395)
- 测试2.2 (0.000392)
- 测试2.3 (0.000325)
- Test2.4 (0.000645)
我假设初始测试时间也考虑了资源的加载,但我似乎无法使第一次测试与其他时间保持一致。每个测试都有运行的setUp 和tearDown 方法。我还尝试以不同的名称重复完全相同的测试,并且它在预期的更短的时间内运行,因此第一个测试总是有某种开销。
PHP 没有任何类型的 unloadClass 或 unloadFile 函数,因此我不确定如果第一个测试有将资源加载到内存中的开销,是否可以为每个测试重新创建一致的测试环境。我尝试过预加载文件和初始类实例化,但似乎都没有产生影响。
我是否遗漏了其他可能影响初始测试时间的可能性?解决这个问题的正确方法是什么?
注意:我知道现有的 PHP 单元测试框架,但我正在尝试做一些不同的事情,所以请不要说“Just use PHPUnit”,因为它对帮助我解决这个问题。
I've created a basic unit test suite in PHP. It shows whether or not a test passed and the time it took to execute the test. It works great, but I've run into a problem and I'm not sure how to address it. Each initial unit test of each unit test class always appears to take more time to execute than the others in that class, usually by a significant factor.
As an example:
TestClass1
- Test1.1 (0.042395)
- Test1.2 (0.000392)
- Test1.3 (0.000325)
- Test1.4 (0.000645)
TestClass2
- Test2.1 (0.042395)
- Test2.2 (0.000392)
- Test2.3 (0.000325)
- Test2.4 (0.000645)
I'm assuming that the initial test time is also factoring in the loading of resources, but I can't seem to make the first test consistent with the other times. Each test has setUp and tearDown methods that are run. I've also tried repeating the exact same test under a different name and it runs in the expected shorter time, so the first test always has some sort of overhead.
PHP doesn't have any sort of unloadClass or unloadFile function, so I'm not sure if I can recreate a consistent test environment for each test if the first test has the overhead of loading resources into the memory. I've tried preloading files and an initial class instantiation, but neither seems to make an impact.
Are there other possibilities that I might be missing that could be affecting the initial test time? What is the proper way to address this?
Note: I'm aware of existing PHP unit testing frameworks, but I'm trying to do something a little different, so please don't say, "Just use PHPUnit", as it's not helpful in helping me solve this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在 PHP 代码中进行的任何基准测试都会出现偏差。我建议研究使用 Xdebug 的分析功能来获得更准确的基准: http://xdebug.org/docs/profiler< /a>
Any benchmarks you're doing in PHP code are going to be skewed. I would suggest looking into using Xdebug's profiling features to get more accurate benchmarks: http://xdebug.org/docs/profiler