使用 `-s` 和 `-t` 的 Python 单元测试会抛出断言错误:路径必须在项目内

问题描述 投票:0回答:2

我想为我的项目执行单元测试。在项目根目录中,我使用

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__()
进行测试。

如果我在 PowerShell 中通过

python -m unittest discover -s tests -t src
执行单元测试,我会收到以下错误消息:

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 python-3.x python-unittest
2个回答
5
投票

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
指定的内容中。


2
投票

我认为问题在于测试发现

...所有测试文件必须是可从项目顶级目录导入的模块或包(包括命名空间包)...

您将

src
指定为顶级目录 (
python -m unittest discover -s tests -t src
),并且测试无法导入到
src
目录中。

© www.soinside.com 2019 - 2024. All rights reserved.