首页 > 解决方案 > 如何使用“discover”在我的“tests”目录中运行测试?

问题描述

使用 DJango/Python 3.7。我在这里读到——如何在一个目录中运行所有 Python 单元测试?我可以使用“发现”命令在指定目录中查找测试。我想要一个“测试”文件夹,所以我创建了一个然后运行

(venv) localhost:myproject davea$ python -m unittest discover tests
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/__main__.py", line 18, in <module>
    main(module=None)
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 100, in __init__
    self.parseArgs(argv)
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 124, in parseArgs
    self._do_discovery(argv[2:])
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 244, in _do_discovery
    self.createTests(from_discovery=True, Loader=Loader)
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py", line 154, in createTests
    self.test = loader.discover(self.start, self.pattern, self.top)
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 344, in discover
    raise ImportError('Start directory is not importable: %r' % start_dir)
ImportError: Start directory is not importable: 'tests'

这对我来说很奇怪,因为我有一个(空的)初始化文件......

(venv) localhost:myproject davea$ ls web/tests/
__init__.py  model_tests.py

我还需要做什么才能让我的测试目录被识别?

编辑:以下是 model_tests.py 的内容...

from django.conf import settings
from django.test import TestCase
from django.core import management


def setup():
    print("setup")
    management.call_command('loaddata', 'test_data.yaml', verbosity=0)


def teardown():
    management.call_command('flush', verbosity=0, interactive=False)


class ModelTest(TestCase):

    # Verify we can correctly calculate the amount of taxes when we are working
    # with a state whose tax rates are defined in our test data
    def test_calculate_tax_rate_for_defined_state(self):
        state = "MN"
        income = 30000
        taxes = IndividualTaxBracket.objects.get_taxes_owed(state, income)
        print(taxes)
        self.assertTrue(taxes > 0, "Failed to calucate taxes owed properly.")

标签: djangopython-3.xunit-testingtesting

解决方案


我认为您对discover命令有些困惑。根据文档。

Unittest 支持简单的测试发现。为了与测试发现兼容,所有测试文件必须是可从项目顶级目录导入的模块或包(包括命名空间包)(这意味着它们的文件名必须是有效的标识符)。

这意味着所有测试文件都必须可以从您运行命令的目录(包含您的目录的目录)中导入web。它确保所有测试文件都必须位于有效的 python 包中(包含 的目录__init__.py)。

其次,您正在运行python -m unittest discover tests错误的命令。您不必tests在最后添加。带有 discover 命令的单元测试支持 4 个选项。你可以在这里阅读更多关于它的信息。

我有以下目录结构。

 web
    ├── __init__.py
    └── tests
        ├── __init__.py
        └── test_models.py

我正在运行以下命令。

python3 -m unittest discover

结果如下。

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

推荐阅读