单元测试套件多大内存合适?
我有大约 700 个测试和大约 1500 个断言,测试在 RAM 驱动器上的 sqlite 上运行。我使用 phpunit /dir/name 运行此程序,无需任何额外的套件设置。
测试写得很好(我希望如此),应用程序和每个测试都运行得很快,并且每个测试的内存使用量都不大(永远不会超过 10M)。
一旦测试分开运行,一切就OK了。但是当我一起运行所有测试时 PHPUnit 报告内存使用量约为 450MB。
- 500M可以吗?为了安全起见,我应该将
memory_limit
更新为 700MB 吗? - 如何避免这样的内存使用?
我知道答案取决于应用程序,但我想知道可以测试中等级别应用程序达到的内存级别。
更新:
我们在 tearDown()
中使用事务并回滚数据库更改
I have ~700 tests with ~1500 assertions, tests are running on sqlite on RAM drive. I run this using phpunit /dir/name
, without any additional suite setup.
The tests are written well (I hope so), the app and each one test runs fast and the memory usage for each one is not big (never bigger than 10M).
Everything is ok once the tests are running separated. But when I run all the tests together,
PHPUnit reports memory usage about 450MB.
- Is 500M ok? Shall I update the
memory_limit
to 700MB to be safe? - How to avoid such a memory usage?
I understand that the answer is depends on the app, but I wonder which memory level can tests for a medium level app reach.
Update:
We are using transactions and rollback db changes in tearDown()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在运行任何测试之前,PHPUnit 会为每个测试方法和每个数据提供程序的每个参数数组实例化一个测试用例。首先,任何异常和断言失败都会引用引发它的实例。正如 MasterCassim 所说,这使得在
tearDown()
中取消设置分配给测试的任何实例变量非常重要。但这主要意味着随着测试数量的增加,您必须接受不断增长的内存需求。如果您还生成代码覆盖率,那么这会大大增加。我们的主网站项目运行时的内存限制为 2GB。
您可以在单独的进程中运行测试,但您将因运行时间的增加而付出高昂的代价。
注意: 问题 #10 应该解决这个问题,但是不知道3.6的情况如何。
PHPUnit instantiates a test case for each test method and each argument array for each data provider before running any tests. To boot, any exception and assertion failure has a reference to the instance that threw it. This makes it very important to unset any instance variables you assigned to the test in
tearDown()
as MasterCassim said.But it mainly means that you have to accept a growing memory requirement as the number of tests increase. If you generate code coverage as well this jumps considerably. Our main website project runs with a limit of 2GB.
You can run the tests in separate processes, but you'll pay a high price in increased run times.
Note: Issue #10 is supposed to address this, but I don't know what the status of it is in 3.6.
如果一起运行测试,则必须使用tearDown() 方法来“释放”内存。
If you run the tests together you have to use the tearDown() method to "free" memory.