python-3.x - 使用 `-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
如果我正确理解了本教程,它应该可以工作。那么,我做错了什么?
解决方案
在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
。
推荐阅读
- python - 根据最后一列和最后一行的总和查找前 5 个值
- intellij-idea - 如何阻止 IntelliJ Cursive 重新格式化我的 Clojure 代码?
- agora.io - 单用户加入时,Agora 云录制无法正常工作
- python - Kivy 中程序生成的按钮
- python - 如果太长,终端不会读取整个文本文件
- git - 有没有办法在没有嵌套子模块的情况下添加 git 子模块?
- apache-spark - Spark Cassandra 使用区分大小写的名称写入 UDT 失败
- swift - 如何以编程方式调整 UIScrollView 中内容视图的大小?
- pandas - 替换为 loc 会引发无效的类型比较
- c# - 绑定到 ListView 的 Xamarin.Forms 列表有时会短暂显示元素,然后一切都消失了