microsoft / PTVS

Python Tools for Visual Studio
https://aka.ms/PTVS
Apache License 2.0
2.53k stars 676 forks source link

Error executing test by pytest #7959

Closed kenichiice closed 2 months ago

kenichiice commented 2 months ago

Describe the bug When running tests using pytest 8.3.1 on Visual Studio 2022, the following error occurs.

Note that this error does not occur when pytest 8.0.2 is used.

Traceback (most recent call last):
  File "C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\EXTENSIONS\MICROSOFT\PYTHON\CORE\testlauncher.py", line 189, in <module>
    main()
  File "C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\EXTENSIONS\MICROSOFT\PYTHON\CORE\testlauncher.py", line 30, in main
    run(testRunner, coverage_file, test_file, args)
  File "C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\EXTENSIONS\MICROSOFT\PYTHON\CORE\testlauncher.py", line 119, in run
    patch_translate_non_printable()
  File "C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\EXTENSIONS\MICROSOFT\PYTHON\CORE\testlauncher.py", line 136, in patch_translate_non_printable
    translate_non_printable =  getattr(_pytest.compat, "_translate_non_printable")
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module '_pytest.compat' has no attribute '_translate_non_printable'

The _translate_non_printable that is causing the error seems to have been removed from the pytest code.

Steps to Reproduce

  1. Create a Python project on VS 2022 that uses pytest as the testing framework.
  2. Install the latest pytest (currently 8.3.1) in the Python environment of the Python project.
  3. Add any test code to the Python project.
  4. Run the test from VS's Test Explorer.

Expected behavior No error should occur.

Sample Code For example, the following test code will reproduce the error.

def test_failure():
    assert 1 == 1

Configuration information (If you are providing a diagnostics file (see below), skip this section) VS Version: 17.10.4 PTVS version: 17.0.24064.1 Python version: 3.11

StellaHuang95 commented 2 months ago

The issue is that PTVS is trying to access pytest's private API, we will take a look and have it fixed.

Jeroen6 commented 1 month ago

For people finding this with google, and VS 2022 17.11.1 with pytest 8.3.2:

Change line 136 of this file to bypass this problem.

C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\EXTENSIONS\MICROSOFT\PYTHON\Core\testlauncher.py

#note: this must match adapter\pytest\_discovery.py
def patch_translate_non_printable():
    import _pytest.compat
    translate_non_printable =  getattr(_pytest.compat, "_translate_non_printable", None)

+,None