简体   繁体   English

Python 模块单元测试

[英]Python unittest for modules

I want to run a Python unittest by invoking the通过调用

python -m unittest discover 

command.命令。 Two packages are in in a module as follows两个包在一个模块中,如下所示

test
 |_ mytest.py
my_module
  |__foo.py
  |__bar.py 

where in bar.pybar.py中的什么位置

#bar.py
from foo import baz
# Do things using baz

The problem is that when the unittest discover command runs, foo cannot be found in bar.py anymore.问题是当 unittest discover 命令运行时,在bar.py中找不到foo了。 This because my_module is seen as a proper module inside the python test, so I would need to do这是因为my_module在 python 测试中被视为正确的模块,所以我需要这样做

from my_module.foo import baz

but that wouldn't work when using the module stand-alone.但这在单独使用模块时不起作用。 So what to do?那么该怎么办? I see 2 options, but they are not that neat so I would like more input:我看到 2 个选项,但它们不是那么整洁,所以我想要更多输入:

  1. Let init .py in my_module add my_module to PYTHONPATH让 my_module 中的init .py 将 my_module 添加到 PYTHONPATH
  2. Let mytest.py add my_module to PYTHONPATH让 mytest.py 将 my_module 添加到 PYTHONPATH

However, I found this page stating that但是,我发现此页面说明

Never add a package directory, or any directory inside a package, directly to the Python path.切勿将 package 目录或 package 内的任何目录直接添加到 Python 路径。

The arguments put forth seems to make sense, and as our repository grows constantly we should try to get this right.提出的 arguments 似乎是有道理的,随着我们的存储库不断增长,我们应该努力做到这一点。

What would you do?你会怎么做?

If you ALSO want to have those modules STANDALONE, then they should NOT be INSIDE another module.如果您还想让这些模块独立,那么它们不应该在另一个模块内。 Because that way, you are ALREADY violating "Never add a page directory inside a module..." And then you could add that path to the pythonpath as well.因为那样的话,你已经违反了“永远不要在模块中添加页面目录......”然后你也可以将该路径添加到 pythonpath 中。

If you don't want to make any changes to my_module to turn it into a package that can be imported by your unit tests, one solution is to move your unit tests into my_module .如果您不想对my_module进行任何更改以将其变成可以由单元测试导入的 package,则一种解决方案是将单元测试移至my_module中。 For example, you could have:例如,您可以:

# my_module/bar.py

from foo import baz
# my_module/test_bar.py

import bar

# Tests...

Then run python -m unittest discover from inside my_module or python -m unittest discover --start-directory my_module from the project root.然后从my_module内部运行python -m unittest discover或从项目根目录运行 python -m python -m unittest discover --start-directory my_module

By the way, I think there might be some confusion with terminology here.顺便说一句,我认为这里的术语可能有些混乱。 In Python, a module is typically a single .py file, and a package is typically a folder that contains a group of .py files, one of which is __init__.py .在 Python 中,模块通常是单个.py文件,而 package 通常是包含一组.py文件的文件夹,其中一个是__init__.py For that reason, foo and bar are probably really modules, and my_module is probably a package (or would be if it had an __init__.py file).出于这个原因, foobar可能真的是模块,而my_module可能是 package (或者如果它有一个__init__.py文件)。

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

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