Skip to content

python.unitTest.cwd no effect? #5252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rob-miller opened this issue Apr 14, 2019 · 18 comments
Closed

python.unitTest.cwd no effect? #5252

rob-miller opened this issue Apr 14, 2019 · 18 comments
Labels
area-testing bug Issue identified by VS Code Team member as probable bug

Comments

@rob-miller
Copy link

Issue Type: Bug

I am working on a repository with unit tests in a Tests subfolder, and the specific test needs to read a file from a subdirectory of Tests. The read fails with 'file not found', and when I print os.getcwd() before the read it reports ${workspaceFolder} regardless of what I put into python.unittest.cwd. I am using Python unittest (python.unitTest.unittestEnabled is true) framework.

Apologies if I am not understanding how this is supposed to work! Thanks

Extension version: 2019.3.6558
VS Code version: Code 1.33.1 (51b0b28134d51361cf996d2f0a1c698247aeabd8, 2019-04-11T08:22:55.268Z)
OS version: Darwin x64 18.5.0

System Info
Item Value
CPUs Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz (4 x 3200)
GPU Status 2d_canvas: enabled
checker_imaging: disabled_off
flash_3d: enabled
flash_stage3d: enabled
flash_stage3d_baseline: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
native_gpu_memory_buffers: enabled
rasterization: unavailable_off
surface_synchronization: enabled_on
video_decode: enabled
webgl: enabled
webgl2: enabled
Load (avg) 2, 2, 2
Memory (System) 32.00GB (3.24GB free)
Process Argv -psn_0_4936885
Screen Reader no
VM 0%
@ghost ghost added the triage-needed Needs assignment to the proper sub-team label Apr 14, 2019
@DonJayamanne DonJayamanne added area-testing triage bug Issue identified by VS Code Team member as probable bug labels Apr 15, 2019
@DonJayamanne DonJayamanne self-assigned this Apr 15, 2019
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Apr 15, 2019
@DonJayamanne
Copy link

What version of python are you using?
And are there any errors logged in the Python Test output panel.

@DonJayamanne DonJayamanne added the info-needed Issue requires more information from poster label Apr 15, 2019
@rob-miller
Copy link
Author

Python 3.7.3
If I hardcode the absolute dir for the file read there are no errors in the test output panel:

test_noHstruct (test_PDB_pic.test_pic) ... /Users/rob/sync/rgithub/biopython
ok

----------------------------------------------------------------------

If I try to read from the subdir as intended (with python.unittests.cwd set to the absolute path or ${workspaceFolder}/Tests), I get:

test_noHstruct (test_PDB_pic.test_pic) ... /Users/rob/sync/rgithub/biopython
ERROR
NoneType: None

======================================================================
ERROR: test_noHstruct (test_PDB_pic.test_pic)

@DonJayamanne
Copy link

I'm unable to replicate this issue at my end, please could you:

  • Provide a simple sample repo or archive of the source folder with the files
  • please include the settings.json as well

@hvdklauw
Copy link

hvdklauw commented May 2, 2019

I have the same thing with Pytest.

Setting python.testing.cwd to ${workspaceFolder}/api then pytest reports the rootdir as the ${workspaceFolder} same with relative paths, settings it to an absolute path will work.

@DonJayamanne DonJayamanne removed their assignment May 15, 2019
@DonJayamanne
Copy link

Got it, thanks

@DonJayamanne DonJayamanne removed info-needed Issue requires more information from poster triage labels May 19, 2019
@rob-miller
Copy link
Author

Not clear if you need more info on this, but I am back to the project now and can report that the public repo biopython exhibits the problem for me. A specific example is Tests/test_PDB_parse_pdb_header.py where the test test_parse_header_line() tries to read a file from the Tests/PDB subdirectory.

I have updated VSCode to 1.34.0 and the python extension reports 2019.4.12954.

I have added print(os.getcwd()) to the beginning of this test and can't get it to change to anything other than the workspaceFolder. I have tried setting in python.testing.cwd as well as the unittestArgs in the workspace settings.json (attached). That is, putting ${workspaceFolder}/Tests or the absolute path in these places still found the tests but did not change the working directory for the test, so it fails to read the input file at the start.

settings.json -> settings.txt

@ericsnowcurrently
Copy link

FYI, I ran into this while working on #6548.

@Ark-kun
Copy link

Ark-kun commented Mar 25, 2020

I have the same issue.
My terminal CWD is sdk/python/tests. But the test discovery ignores that. I tied to change the "Testing: Cwd", but it has no effect:

> ~\AppData\Local\Programs\Python\Python37\python.exe -c "
import unittest
loader = unittest.TestLoader()
suites = loader.discover(".", pattern="*test*.py")
print("start") #Don't remove this line
for suite in suites._tests:
    for cls in suite._tests:
        try:
            for m in cls._tests:
                print(m.id())
        except:
            pass"
cwd: <my workspace root>

@jaron-l
Copy link

jaron-l commented Sep 10, 2020

I have been able to replicate this issue only when I have python.testing.pytestEnabled enabled. It however works properly with python.testing.unittestEnabled and python.testing.nosetestsEnabled.

@cebor
Copy link

cebor commented Dec 3, 2020

I also have this issue with python.testing.unittestEnabled.

@Mutantpenguin
Copy link

Having the same problem python.testing.unittestEnabled.

When I enter ${workspaceFolder}/../.. into python.testing.cwd it has no effect whatsoever.

os.getcwd() always returns my ${workspaceFolder}

@jaron-l
Copy link

jaron-l commented Sep 8, 2021

I recently investigated this probably briefly and found that anytime that python.testing.cwd is changed, the window needs to be reloaded for the setting to be picked up. With that method, I have found that it works as expected.

@Mutantpenguin
Copy link

Mutantpenguin commented Sep 10, 2021

@jaron-l Thank you very much, that worked.

I just realized that python.testing.cwd doesn't seem to do what I expected it to do.
It looks to my like it is used as the CWD for the test discovery too. I thought it would only be used for running the tests.

Can anyone confirm this?

And to make matters worse: Doesn't seem to get used when debugging a unittest.

@jaron-l
Copy link

jaron-l commented Sep 10, 2021

It does work for test discovery too, which I'm pretty sure is intentional lest code would likely crash on discovery and not when running (or vice-versa) as you'd be running in different environments with different python paths.

The second issue you are referencing can be found here: #8678

@hvdklauw
Copy link

hvdklauw commented Sep 13, 2021

Funny, I tested the reloading and that did indeed fix running the tests through the Test Explorer UI and Python Test explorer (which use the python settings) but not through the normal python extension, it finds the tests, but then running the tests gives me a file or directory not found with a path relative to the workspace folder but not the testing cwd.

This is with pytest btw.

@jaron-l
Copy link

jaron-l commented Sep 13, 2021

@hvdklauw, what do you mean by "the normal python extension"? Are you talking about the CodeLens buttons? What's your workflow?

@hvdklauw
Copy link

What I mean is the vscode-python test explorer, it doesn't run the tests correctly, but the Test Explorer UI (which is now deprecated but you can still use) with the python text explorer extension actually works for me, so they must be doing something right that the python extension is doing wrong, because it uses the same settings.

@github-actions github-actions bot removed the needs PR label Aug 9, 2022
@karrtikr karrtikr added the needs PR Ready to be worked on label Aug 9, 2022
@eleanorjboyd
Copy link
Member

Hello! I have reviewed this issue and given the architectural changes of the testing rewrite, this issue should no longer exist on the rewrite. Therefore I am going to close this issue but please comment or open a new issue if you are still seeing a problem when you try this yourself!

To use the rewrite yourself just add ”python.experiments.optInto": ["pythonTestAdapter"] to your user settings. You can confirm you have the rewrite enabled by setting your log level to trace, via the Developer: Set Log Level command in the command palette. Then check to see if Experiment 'pythonTestAdapter' is active is in your python logs.

Thanks!

@github-actions github-actions bot removed the needs PR Ready to be worked on label Oct 30, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-testing bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests