Python中的单元测试

Python中的单元测试

本文介绍了如何执行“基于性能"的操作(基准)Python中的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我的代码库已经达到了合理的单元测试覆盖率. (超过某个点,扩大覆盖范围并没有良好的投资回报率.)

Let's say that I've got my code base to as high a degree of unit test coverage as makes sense. (Beyond a certain point, increasing coverage doesn't have a good ROI.)

下一步,我要测试性能.对代码进行基准测试,以确保新提交不会不必要地降低速度. Safari的零容忍政策引起了提交速度的降低,对此我很感兴趣.我不确定大多数项目的速度承诺是否具有良好的投资回报率,但我至少希望得到有关速度退化已发生的警报,并能够对此做出判断.

Next I want to test performance. To benchmark code to make sure that new commits aren't slowing things down needlessly. I was very intrigued by Safari's zero tolerance policy for slowdowns from commits. I'm not sure that level of commitment to speed has a good ROI for most projects, but I'd at least like to be alerted that a speed regression has happened, and be able to make a judgment call about it.

环境是Linux上的Python,对于BASH脚本也可行的建议会让我非常高兴. (但是Python是主要焦点.)

Environment is Python on Linux, and a suggestion that was also workable for BASH scripts would make me very happy. (But Python is the main focus.)

推荐答案

如果可能,您将希望在系统级别进行性能测试-在上下文中对应用程序进行整体测试,使其数据和行为与生产环境接近尽可能.

You will want to do performance testing at a system level if possible - test your application as a whole, in context, with data and behaviour as close to production use as possible.

这并不容易,要使其自动化并获得一致的结果将变得更加困难.

This is not easy, and it will be even harder to automate it and get consistent results.

此外,您不能使用VM进行性能测试(除非您的生产环境在VM中运行,即使那样,您也需要在没有其他任何设备的主机上运行VM).

Moreover, you can't use a VM for performance testing (unless your production environment runs in VMs, and even then, you'd need to run the VM on a host with nothing else on).

当您说执行性能单元测试时,这可能是有价值的,但前提是要将其用于诊断在系统级别(不仅仅是在开发人员的头上)确实存在的问题.

When you say doing performance unit-testing, that may be valuable, but only if it is being used to diagnose a problem which really exists at a system level (not just in the developer's head).

此外,单元测试中的单元性能有时无法在上下文中反映其性能,因此可能根本没有用.

Also, performance of units in unit testing sometimes fails to reflect their performance in-context, so it may not be useful at all.

这篇关于如何执行“基于性能"的操作(基准)Python中的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 00:42