对于一个简单的Python模块来说,非常常见的目录结构似乎是将单元测试分离到它们自己的测试目录中:

new_project/
    antigravity/
        antigravity.py
    test/
        test_antigravity.py
    setup.py
    etc.

我的问题很简单,实际运行测试的通常方式是什么?我怀疑这对每个人来说都是显而易见的,除了我,但你不能只是从测试目录运行python test_antigravity.py,因为它的导入antigravity将失败,因为模块不在路径上。

我知道我可以修改PYTHONPATH和其他与搜索路径相关的技巧,但我不能相信这是最简单的方法——如果您是开发人员,这很好,但如果用户只是想检查测试是否通过,那么期望他们使用这种方法是不现实的。

另一种替代方法是将测试文件复制到另一个目录中,但这似乎有点愚蠢,并且没有注意到将它们放在一个单独的目录中。

那么,如果您刚刚下载源代码到我的新项目,您将如何运行单元测试?我更喜欢这样的答案:“要运行单元测试,请执行x。”


当前回答

如果没有一些巫术,就不能从父目录导入。下面是另一种至少适用于Python 3.6的方法。

首先,创建一个包含以下内容的test/context.py文件:

import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

然后在test/test_antigravity.py文件中导入如下内容:

import unittest
try:
    import context
except ModuleNotFoundError:
    import test.context    
import antigravity

请注意,这个try-except子句的原因是

导入测试。当使用“python test_antigravity.py”运行时,上下文将失败 在new_project目录下使用"python -m unittest"运行时导入context失败。

通过这种诡计,他们都成功了。

现在你可以运行test目录下的所有测试文件:

$ pwd
/projects/new_project
$ python -m unittest

或者运行一个单独的测试文件:

$ cd test
$ python test_antigravity

好吧,这并不比在test_antigravity。py中包含context。py的内容漂亮多少,但也许会漂亮一点。欢迎提出建议。

其他回答

这种方式将允许您从任何您想要的地方运行测试脚本,而不会从命令行中破坏系统变量。

这将把主项目文件夹添加到python路径中,其位置相对于脚本本身,而不是相对于当前工作目录。

import sys, os

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

将其添加到所有测试脚本的顶部。这将把主项目文件夹添加到系统路径中,因此从那里导入的任何模块现在都可以工作了。你在哪里做测试并不重要

显然,您可以更改project_path_hack文件以匹配您的主项目文件夹位置。

如果你运行“python setup.py develop”,那么包就会在路径中。但你可能不想这样做,因为你可能会感染你的系统python安装,这就是virtualenv和buildout等工具存在的原因。

如果您正在寻找命令行解决方案:

基于以下目录结构(使用专用源目录进行概括):

new_project/
    src/
        antigravity.py
    test/
        test_antigravity.py

Windows:(在new_project中)

$ set PYTHONPATH=%PYTHONPATH%;%cd%\src
$ python -m unittest discover -s test

如果您想在批for循环中使用此语句,请参阅此问题。

Linux:(在new_project中)

$ export PYTHONPATH=$PYTHONPATH:$(pwd)/src  [I think - please edit this answer if you are a Linux user and you know this]
$ python -m unittest discover -s test

使用这种方法,还可以在必要时向PYTHONPATH添加更多目录。

我通常在项目目录中创建一个“运行测试”脚本(对于源目录和测试都是通用的),用于加载我的“所有测试”套件。这通常是样板代码,所以我可以在项目之间重用它。

run_tests.py:

import unittest
import test.all_tests
testSuite = test.all_tests.create_test_suite()
text_runner = unittest.TextTestRunner().run(testSuite)

test/all_tests.py(来自如何在一个目录中运行所有Python单元测试?)

import glob
import unittest

def create_test_suite():
    test_file_strings = glob.glob('test/test_*.py')
    module_strings = ['test.'+str[5:len(str)-3] for str in test_file_strings]
    suites = [unittest.defaultTestLoader.loadTestsFromName(name) \
              for name in module_strings]
    testSuite = unittest.TestSuite(suites)
    return testSuite

有了这个设置,你确实可以在你的测试模块中包含反重力。缺点是你需要更多的支持代码来执行一个特定的测试…我每次都把它们都运行一遍。

从你链接的文章中:

Create a test_modulename.py file and put your unittest tests in it. Since the test modules are in a separate directory from your code, you may need to add your module’s parent directory to your PYTHONPATH in order to run them: $ cd /path/to/googlemaps $ export PYTHONPATH=$PYTHONPATH:/path/to/googlemaps/googlemaps $ python test/test_googlemaps.py Finally, there is one more popular unit testing framework for Python (it’s that important!), nose. nose helps simplify and extend the builtin unittest framework (it can, for example, automagically find your test code and setup your PYTHONPATH for you), but it is not included with the standard Python distribution.

也许你应该像它所暗示的那样看看鼻子?