社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python测试:如何查看用哪个args statsd client gauge方法调用的?

Aaron • 5 年前 • 620 次点击  

我有一个函数装饰器,用于跟踪一个函数运行需要多长时间,然后发出该度量。

def measure_and_emit_runtime(metric_name):

     def timer(func):
         @functools.wraps(func)
         def timer_wrapper(*args, **kwargs):
             start_time = time.clock()
             output = func(*args, **kwargs)
             end_time = time.clock()
             run_time = end_time - start_time
             with StatsClient() as client:
                 client.gauge(metric_name, run_time)
             return output

         return timer_wrapper

     return timer

我试图编写一个单元测试,看看在修饰和调用给定的函数之后是否使用预期的参数调用client.gauge,但对如何调用感到困惑。

@measure_and_emit_runtime('some_metric_name')
def some_function():
    return None

some_function()

# Expect client.gauge to be called with 'some_metric_name' as one argument

任何提示都会有帮助的!不幸的是,我一直在玩模拟模块没有成功。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/49296
 
620 次点击