简体   繁体   English

如何在python中的模块中测试私有静态方法

[英]how do you test private static method in module in python

we have a module of static methods in our python app. 我们的python应用程序中有一个静态方法模块。 these methods use a lot of private (eg: "__do_sub_task2(**args)") I would like to write unit tests for these private static methods within this module, but I am getting refernce errors. 这些方法使用了很多私有方法(例如:“ __ do_sub_task2(** args)”),我想在此模块中为这些私有静态方法编写单元测试,但出现引用错误。

is there a way to do this? 有没有办法做到这一点?

update: adding scenario 更新:添加方案

I have a module file named 'my_module.py' contents of said file is as follows: 我有一个名为“ my_module.py”的模块文件,该文件的内容如下:

def public_method_foo(my_number):
  return __sub_method_bar(my_number * 10)

def __sub_method_bar(other_number)
  return other_number + 11

update #2 The reason I am asking this question is because I have a similar scenario as above, but when I add the following reference to my test.py module file: 更新#2我问这个问题的原因是因为我有与上述类似的情况,但是当我向test.py模块文件添加以下引用时:

from my_module import __sub_method_bar

and try to use it in my test, I get the following exception in my test 并尝试在测试中使用它,我在测试中得到以下异常

global name '_MyTests__sub_method_bar' is not defined 全局名称'_MyTests__sub_method_bar'未定义

What you have are not methods, not private, and not static; 您拥有的不是方法,不是私有的,也不是静态的。 they're just plain old public functions in the module. 它们只是模块中的普通旧公共函数。 So you call them the same way as any other function. 因此,您可以像调用其他任何函数一样调用它们。 For your example: 例如:

>>> my_module.__sub_method_bar(5)

That's it; 而已; nothing tricky. 没什么棘手的。 * *

* Well, there is one tricky thing, but it's probably not going to affect you here: If my_module doesn't have an __all__ , and you do from my_module import * , you will not get any of the globals (including functions) whose names start with _ . *好吧,这是一件棘手的事情,但是在这里可能不会影响您:如果my_module没有__all__ ,并且您from my_module import * ,您将不会获得任何具有其名称的全局变量(包括函数)以_开头。 But normally your unit tests are going to import my_module , so this won't be relevant. 但是通常您的单元测试将import my_module ,因此这将import my_module


Methods are callables that are members of a class . 方法是属于成员的可调用对象。 And methods can be private ("private" in this sense means "visible only to this class, not even to super- or sub-classes", so it doesn't make sense for anything but methods). 并且方法可以是私有的(在这种意义上,“私有”的意思是“仅对此类可见,甚至对超类或子类都不可见”,因此对方法而言,它没有任何意义)。 The tutorial chapter on Classes explains how private methods are implemented, with name-mangling. 关于的教程一章说明了如何通过名称处理实现私有方法。 Methods (private or otherwise) can also be static ("static" in this context means "does not take the normal self ", so again, it doesn't make sense for anything but methods). 方法(私有方法或其他方法)也可以是静态的(在这种情况下,“静态”表示“不采用正常的self ”,因此,除了方法之外,它对其他任何事物都没有意义)。 Either way, for a private method, you have to manually demangle the name to call it from outside: 无论哪种方式,对于私有方法,都必须手动取消名称的名称以从外部调用它:

>>> thingy = Thingy()
>>> thingy._Thingy__private_method(5)
>>> Thingy._Thingy__private_static_method(5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM