pytest报错:"ERROR: file or directory not found: and".

11

pytest在我的电脑上一直很稳定。今天重启后,它抛出了这个错误:

PS C:\> pytest
================================================= test session starts =================================================
platform win32 -- Python 3.9.1, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: C:\
plugins: forked-1.3.0, html-3.1.1, metadata-1.11.0, xdist-2.2.0
collected 0 items

================================================ no tests ran in 0.01s ================================================
ERROR: file or directory not found: and

无论我是在一个有效的pytest框架内还是在框架外运行,都没有关系。我卸载了pytest和python,然后重新安装,但结果相同。我尝试运行“python -m pytest”,但结果相同。我正在运行Windows 10,python 3.8或3.9。我错过了什么?

以下是使用debug标志的额外输出。

PS C:\> mkdir empty_directory

    Directory: C:\

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        1/14/2021   8:23 PM                empty_directory

PS C:\> cd empty_directory
PS C:\empty_directory> python -m pytest -vvv --debug
writing pytestdebug information to C:\empty_directory\pytestdebug.log
================================================= test session starts =================================================
platform win32 -- Python 3.9.1, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- C:\Users\611528\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe
using: pytest-6.2.1 pylib-1.10.0
setuptools registered plugins:
  pytest-forked-1.3.0 at C:\Users\611528\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pytest_forked\__init__.py
  pytest-html-3.1.1 at C:\Users\611528\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pytest_html\plugin.py
  pytest-metadata-1.11.0 at C:\Users\611528\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pytest_metadata\plugin.py
  pytest-xdist-2.2.0 at C:\Users\611528\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\xdist\plugin.py
  pytest-xdist-2.2.0 at C:\Users\611528\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\xdist\looponfail.py
cachedir: .pytest_cache
metadata: {'Python': '3.9.1', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '6.2.1', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'forked': '1.3.0', 'html': '3.1.1', 'metadata': '1.11.0', 'xdist': '2.2.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk-15.0.1'}
rootdir: C:\empty_directory
plugins: forked-1.3.0, html-3.1.1, metadata-1.11.0, xdist-2.2.0
collected 0 items

================================================ no tests ran in 0.04s ================================================
wrote pytestdebug information to C:\empty_directory\pytestdebug.log
ERROR: file or directory not found: and

日志文件很大,因此我会浏览它,看是否有什么异常情况。
好的,这很有趣:
    finish pytest_sessionstart --> [] [hook]
    pytest_collection [hook]
        session: <Session empty_directory exitstatus=<ExitCode.OK: 0> testsfailed=0 testscollected=0>
    perform_collect <Session empty_directory exitstatus=<ExitCode.OK: 0> testsfailed=0 testscollected=0> ['and', 'level1'] [collection]
        pytest_collection_finish [hook]
            session: <Session empty_directory exitstatus=<ExitCode.OK: 0> testsfailed=0 testscollected=0>
          pytest_report_collectionfinish [hook]
              config: <_pytest.config.Config object at 0x0000025C74701FD0>
              startdir: C:\empty_directory
              items: []
          finish pytest_report_collectionfinish --> [] [hook]
        finish pytest_collection_finish --> [] [hook]
      pytest_sessionfinish [hook]
          session: <Session empty_directory exitstatus=<ExitCode.USAGE_ERROR: 4> testsfailed=0 testscollected=0>
          exitstatus: ExitCode.USAGE_ERROR
        pytest_terminal_summary [hook]
            terminalreporter: <_pytest.terminal.TerminalReporter object at 0x0000025C74C0F550>
            exitstatus: ExitCode.USAGE_ERROR
            config: <_pytest.config.Config object at 0x0000025C74701FD0>
        finish pytest_terminal_summary --> [] [hook]
      finish pytest_sessionfinish --> [] [hook]
      pytest_unconfigure [hook]
          config: <_pytest.config.Config object at 0x0000025C74701FD0>
      finish pytest_unconfigure --> [] [hook]

这行代码是做什么的:

perform_collect <Session empty_directory exitstatus=<ExitCode.OK: 0>
 testsfailed=0 testscollected=0> ['and', 'level1'] [collection]

也许这就是'and'的来源。这行引起了我的注意,因为我在一个pytest框架中有一个名为'level1'的标记。但我现在不在那个目录下,可能与它无关。

如果我切换到我的MacBook并在一个空目录中运行pytest,那么该行看起来像:

perform_collect <Session temp exitstatus=<ExitCode.OK: 0> 
testsfailed=0 testscollected=0> ['/Users/jhayden/temp'] [collection]

这是我运行的空目录。

我还尝试在虚拟环境中运行:

C:\venvs>python -m venv aaa

C:\venvs>cd aaa\Scripts

C:\venvs\aaa\Scripts>.\activate.bat

(aaa) C:\venvs\aaa\Scripts>pip install pytest
Collecting pytest
  Using cached pytest-6.2.1-py3-none-any.whl (279 kB)
Collecting py>=1.8.2
  Using cached py-1.10.0-py2.py3-none-any.whl (97 kB)
Collecting colorama; sys_platform == "win32"
  Using cached colorama-0.4.4-py2.py3-none-any.whl (16 kB)
Collecting packaging
  Using cached packaging-20.8-py2.py3-none-any.whl (39 kB)
Collecting toml
  Using cached toml-0.10.2-py2.py3-none-any.whl (16 kB)
Collecting iniconfig
  Using cached iniconfig-1.1.1-py2.py3-none-any.whl (5.0 kB)
Collecting atomicwrites>=1.0; sys_platform == "win32"
  Using cached atomicwrites-1.4.0-py2.py3-none-any.whl (6.8 kB)
Collecting pluggy<1.0.0a1,>=0.12
  Using cached pluggy-0.13.1-py2.py3-none-any.whl (18 kB)
Collecting attrs>=19.2.0
  Using cached attrs-20.3.0-py2.py3-none-any.whl (49 kB)
Collecting pyparsing>=2.0.2
  Using cached pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
Installing collected packages: py, colorama, pyparsing, packaging, toml, iniconfig, atomicwrites, pluggy, attrs, pytest
Successfully installed atomicwrites-1.4.0 attrs-20.3.0 colorama-0.4.4 iniconfig-1.1.1 packaging-20.8 pluggy-0.13.1 py-1.10.0 pyparsing-2.4.7 pytest-6.2.1 toml-0.10.2
WARNING: You are using pip version 20.2.3; however, version 20.3.3 is available.
You should consider upgrading via the 'c:\venvs\aaa\scripts\python.exe -m pip install --upgrade pip' command.

(aaa) C:\venvs\aaa\Scripts>pytest
================================================= test session starts =================================================
platform win32 -- Python 3.9.1, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: C:\venvs\aaa\Scripts
collected 0 items

================================================ no tests ran in 0.01s ================================================
ERROR: file or directory not found: and

1
你正在运行驱动器根目录下,因此可能无法收集来自临时文件夹或c:\windows等位置的垃圾。运行pytest -vvv --debug并检查是否有其他有趣的输出。 - hoefling
谢谢。从哪里运行似乎并不重要。我可以创建一个空目录并从该目录运行,但仍会得到相同的错误。我将在帖子中增加额外的调试输出。 - Jon Hayden
3个回答

4

我最后终于弄清楚了。我设置了这个环境变量:

PYTEST_ADDOPTS=-m networks 和 level1

我之前一直在尝试环境变量,但从未遇到过问题,直到我重新启动后。希望这可以帮助未来的某些人不像我这样傻 lol。


1

对于任何遇到使用标记(例如pytest -m“not your-mark”)时遇到相同问题的人:

请注意,双引号是必需的!(至少在Windows-CMD上是这样)

pytest -m 'my_mark and not my_other_mark'

使用单引号会导致上述错误,但是

pytest -m "my_mark and not my_other_mark"

用双引号是不行的。


0

我遇到了同样的问题,最终通过导航到我的当前路径下的Pytest测试文件夹来解决了它。

例如:

tests是包含测试文件的文件夹

tests

|--test_01.py

|--test_02.py

我在终端更改了路径:"C:...\path_to_tests",然后运行命令:pytest test_01.py。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接