Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 97327

How to setup the root for Tests in Python in Visual Studio Code?

$
0
0
project\
  src\
      __init__.py  
  tests\
      __init__.py
      stubs\r_json_response_expired.json

Within my test I have to load a JSON file. this.

@patch('requests.post')
def test_subscription_delete_only_if_invalid(self, mock_post):
    with app.app_context():
        expires_at = datetime.utcnow()
        m = self.inject_expired_r_json()

def inject_expired_r_json(self):
        with open('stubs/r_json_response_expired.json') as fb_file:
            receipt_response = json.load(fb_file)
        m = MagicMock()
        m.json = MagicMock(name='json')
        m.json.return_value = receipt_response
        return m

When running the test VS Code shows:

FileNotFoundError: [Errno 2] No such file or directory: 'stubs/r_json_response_expired.json'

I have .env in my project:

PYTHONPATH=./src

And in settings.json (VS CODE)

"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceFolder}/src"
},
"python.envFile": "${workspaceFolder}/.env"

I don't like changing the path in the test to avoid breaking PyCharm, if I happen to switch the IDE.


Viewing all articles
Browse latest Browse all 97327

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>