首页 > 解决方案 > 使用 `-s` 和 `-t` 的 Python 单元测试会抛出 AssertionError: Path must be within the project

问题描述

我想为我的项目执行单元测试。在项目根目录中,我src为我的代码使用一个文件夹,为我的单元测试使用一个文件tests夹。这是我的项目结构:

project/
│
├── src/
│   └── func.py
|
└── tests/
    ├── __init__.py
    └── test.py

内容func.py

def func():
    return True

内容test.py

import unittest

target = __import__("func.py")
func = target.func

class TestFunc(unittest.TestCase):
    def test_func(self):
        self.assertTrue(func())

if __name__ == '__main__':
    unittest.main()

目前,我不想将我的代码作为模块提供。这就是为什么没有__init__.py内部src,我导入我的代码以通过__import__().

如果我通过python -m unittest discover -s tests -t src在 PowerShell 中执行我的单元测试,我会收到以下错误消息:

PS C:\Users\username\Desktop\project> python -m unittest discover -s tests -t src
Traceback (most recent call last):
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\unittest\__main__.py", line 18, in <module>
    main(module=None)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\unittest\main.py", line 100, in __init__
    self.parseArgs(argv)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\unittest\main.py", line 124, in parseArgs
    self._do_discovery(argv[2:])
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\unittest\main.py", line 244, in _do_discovery
    self.createTests(from_discovery=True, Loader=Loader)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\unittest\main.py", line 154, in createTests
    self.test = loader.discover(self.start, self.pattern, self.top)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\unittest\loader.py", line 349, in discover
    tests = list(self._find_tests(start_dir, pattern))
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\unittest\loader.py", line 387, in _find_tests
    name = self._get_name_from_path(start_dir)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\unittest\loader.py", line 371, in _get_name_from_path
    assert not _relpath.startswith('..'), "Path must be within the project"
AssertionError: Path must be within the project

如果我正确理解了本教程,它应该可以工作。那么,我做错了什么?

标签: python-3.xpython-unittest

解决方案


zardosht在他的回答中指出失败的原因后,我找到了这个答案并将我的项目更改为:

project/
│
├── src/
│   ├── __init__.py
│   └── func.py
|
└── tests/
    ├── __init__.py
    └── test.py

内容func.py

def func():
    return True

内容test.py

import unittest

from src.func import func

class TestFunc(unittest.TestCase):
    def test_func(self):
        self.assertTrue(func())

if __name__ == '__main__':
    unittest.main()

然后,只需发出以下命令,我就可以从项目根目录成功运行我的测试:

python -m unittest

我现在可能想更改一些名称,但在这里,我将保留原来的名称以便更好地比较。


命令

python -m unittest discover -s tests

python -m unittest discover -s tests -t .

也可以,但不是

python -m unittest discover -s tests -t src

因为你用-s, 指定的内容必须包含在你用 指定的内容中-t


推荐阅读