Skip to content
Advertisement

VSCode Python Test Discovery fails?

VS Code version: version Version: 1.69.2 (Universal))

Extension version (available under the Extensions sidebar): v2021.12.1559732655

OS and version: MacOS 12.3.1

Python version: 3.6.8

Type of virtual environment used (N/A | venv | virtualenv | conda | …): venv

Relevant/affected Python packages and their versions: pytest==7.0.1

problem:

~/wkspace/one_service/.venv/bin/python ~/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir ~/wkspace/one_service -s --cache-clear tests
cwd: ~/wkspace/one_service
[ERROR 2022-7-10 14:19:35.323]: Error discovering pytest tests:
 r [Error]: LOADING_FILE: app.__init__.py
BASE_DIR in app.__init__.py: /Users/77/wkspace/one_service
SRE::START_ENV::[None]
b'xe5x8axa0xe8xbdxbdxe9x85x8dxe7xbdxaexe6x96x87xe4xbbxb6xe4xb8xbaxefxbcx9aenv_local.py'
ImportError while loading conftest '/Users/77/wkspace/one_service/tests/conftest.py'.
tests/conftest.py:3: in <module>
    from app import app
app/__init__.py:165: in <module>
    print(f"os.environu73afu5883u53d8u91cfuff1a{os.environ}")
E   UnicodeEncodeError: 'ascii' codec can't encode characters in position 10-14: ordinal not in range(128)

Traceback (most recent call last):
  File "/Users/77/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/testing_tools/run_adapter.py", line 22, in <module>
    main(tool, cmd, subargs, toolargs)
  File "/Users/77/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/testing_tools/adapter/__main__.py", line 100, in main
    parents, result = run(toolargs, **subargs)
  File "/Users/77/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 44, in discover
    raise Exception("pytest discovery failed (exit code {})".format(ec))
Exception: pytest discovery failed (exit code 4)

    at ChildProcess.<anonymous> (/Users/tiantian/.vscode/extensions/ms-python.python-2021.12.1559732655/out/client/extension.js:32:39076)
    at Object.onceWrapper (node:events:510:26)
    at ChildProcess.emit (node:events:390:28)
    at ChildProcess.emit (node:domain:475:12)
    at maybeClose (node:internal/child_process:1064:16)
    at Socket.<anonymous> (node:internal/child_process:450:11)
    at Socket.emit (node:events:390:28)
    at Socket.emit (node:domain:475:12)
    at Pipe.<anonymous> (node:net:687:12)

Advertisement

Answer

Since none of the above answers solved my problem, I recently google the information myself and found the root cause. I added the following statement to the conftest.py file

print(sys.stdout.encoding, '----',
      os.getenv('PYTHONIOENCODING'))

The result obtained is

US-ASCII — utf-8

Checked the documentation to confirm the environment variables that affect sys.stdout.encoding

LC_CTYPE LANG

because the test find script was found to execute successfully in terminal.

~/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/testing_tools/run_adapter.py discover pytest -- - rootdir

In the terminal, look at these two environment variables and add them to the .env file.

LANG=”zh_CN.UTF-8″ LC_CTYPE=”zh_CN.UTF-8″

The problem is solved.

9 People found this is helpful
Advertisement