如何在 Python 中对局部变量进行单元测试 [英] How to unittest local variable in Python

查看:36
本文介绍了如何在 Python 中对局部变量进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个测试来检查静态函数内的局部变量值.我浏览了所有 unittest 文档,但一无所获.

I need to write a test that will check a local variable value inside a static function. I went through all the unittest docs but found nothing yet.

推荐答案

你不能.局部变量是函数的局部变量,不能从外部访问.

You can't. Local variables are local to the function and they cannot be accessed from the outside.

但更重要的一点是,您实际上不应该尝试测试局部变量的值.函数应该被视为外部的黑匣子.给定了一些参数,它们返回一些值和/或更改外部状态.这些是您应该检查的唯一事项.

But the bigger point is that you shouldn't actually be trying to test the value of a local variable. Functions should be treated as black boxes from the outside. The are given some parameters and they return some values and/or change the external state. Those are the only things you should check for.

这篇关于如何在 Python 中对局部变量进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆