Jon Hayden
Jon Hayden

Reputation: 423

pytest fails with "ERROR: file or directory not found: and"

pytest has been working for me forever. Today, after rebooting, it throws this error:

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

It doesn't matter if I run in a valid pytest framework or outside the framework. I uninstalled pytest and python and then reinstalled with the same results. I tried running "python -m pytest" and same results. I am running Windows 10, python 3.8 or 3.9. What am I missing?

Here's some additional output using the debug flag

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

The log file is pretty big so I'll go through it to see if there's anything that jumps out.

Okay, this is interesting:

    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]

What is this line doing:

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

Maybe that's where it's getting the 'and'. This line caught my attention because I have a marker called 'level1' in one of my pytest frameworks. But I'm not in that directory so it could be unrelated.

If I switch to my macbook and run pytest in an empty directory, that line looks like:

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

And that's the empty directory I ran from.

I also tried running in a venv:

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

Upvotes: 13

Views: 35659

Answers (4)

bang Nguyen
bang Nguyen

Reputation: 11

I reviewed my pytest.ini, or sometimes it is setup.cfg, then I removed the command containing "addopts ...." then pytest worked again.

Upvotes: 1

djm
djm

Reputation: 103

For anyone stopping by and facing the same issues when using markers (e.g. pytest -m "not your-mark")

Note that (at least for me on Windows-CMD), the double-quotes are required!

pytest -m 'my_mark and not my_other_mark'

with single-quotes will fail with the mentioned error, but

pytest -m "my_mark and not my_other_mark"

with double-quotes does not.

Upvotes: 1

Xiangyong Roy Luo
Xiangyong Roy Luo

Reputation: 11

I have the same issue and finally figured it out by navigating my current path to Pytest test folder.

For example:

tests is the folder that contains test files

tests

|--test_01.py

|--test_02.py

I changed the path at the terminal: "C:...\path_to_tests"

and then, run the command: pytest test_01.py

Upvotes: 0

Jon Hayden
Jon Hayden

Reputation: 423

I figured it out finally. I had this environment variable set:

PYTEST_ADDOPTS=-m networks and level1

I was experimenting with env variables a while back but never had an issue until I did a reboot. Hopefully this helps someone in the future not be an idiot like myself lol.

Upvotes: 5

Related Questions