question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`python.testing.cwd` does not work properly

See original GitHub issue
  • Update setting description saying it only works with absolute paths. Folks often try relative paths which do not work for them https://github.com/microsoft/vscode-python/issues/15739#issuecomment-847618474.

  • For some reason we need to reload window everytime for changes in the setting to take effect.

  • It seems the setting doesn’t work when using forward slashes in windows: Have the following test ./Test/test2.py where . is workspace folder

import unittest   # The test framework

class Test_TestIncrementDecrement1(unittest.TestCase):
    def test_increment(self):
        f = open("./demofile.txt", "r")
        print(f.read())
        self.assertEqual(2, 4)


if __name__ == '__main__':
    unittest.main()

And ./demofile.txt carrying some random content. Set python.testing.cwd to ./Test. Run the file, it doesn’t work. Setting python.testing.cwd to .\\Test works.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
anzvaniecommented, May 15, 2020

The below setting is working for me with python unittest

{
    "python.testing.cwd": "path\to\yourworkingdir",
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        ".",
        "-p",
        "test_*.py"
    ],
    "python.testing.unittestEnabled": true,
}

Remember you need to restart your VSCode application after modify your settings.json file.

0reactions
b-reichcommented, Mar 12, 2022

@karrtikr what’s way you running for test.

If using launch.json file and run directly your file, you need to set working directory manually with cwd property

"configurations": [
	{
		"name": "Python: Current File",
		"type": "python",
		"request": "launch",
		"program": "${file}",
		"console": "integratedTerminal",
		"cwd": "${fileDirname}"
	}
]

You also able to using TEST EXPLORER of VSCode for testing with setting in my previous comment. But you will be got a problems with TEST EXPLORER of VSCode: run a test case or test class is working normally but debugging will throws an exception Unhandled exception in thread started by.

This launchfile doesnt work for me anymore… Do you have the same problem?

Read more comments on GitHub >

github_iconTop Results From Across the Web

python.testing.cwd ignored when discovering or running tests
Seems like we don't take python.testing.cwd into account anymore when discovering tests, or running single tests using pytest.
Read more >
vscode-python runs pytest in wrong directory such that relative ...
In the 'Settings' pane, search for 'python.testing.cwd'. The docs say that 'cwd' 'Specifies an optional working directory for unit tests.'.
Read more >
Testing Python in Visual Studio Code
Unit tests are concerned only with the unit's interface—its arguments and return values—not with its implementation (which is why no code is shown...
Read more >
Python Unit Testing With VS Code
VS Code can automatically recognize existing Python tests and run them. Learn how to configure it to run test suites, individual tests as...
Read more >
Set up unit testing for Python code - Visual Studio
The Run menu gives you commands to run failed, passed, or not run tests as a group. You can select one or more...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found