嘲笑os.environ回归,str预计不会直言不讳

问题描述 投票:-1回答:1

enter image description here

执行此代码会导致:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1630, in __enter__
    self._patch_dict()
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1652, in _patch_dict
    in_dict.update(values)
  File "/Users/<redacted>/<redeacted>/<redacted>/venv/bin/../lib/python3.7/_collections_abc.py", line 841, in update
    self[key] = other[key]
  File "/Users/<redacted>/<redacted>/<redacted>/venv/bin/../lib/python3.7/os.py", line 683, in __setitem__
    value = self.encodevalue(value)
  File "/Users/<redacted>/<redacted>/<redacted>/venv/bin/../lib/python3.7/os.py", line 753, in encode
    raise TypeError("str expected, not %s" % type(value).__name__)
TypeError: str expected, not int
python python-unittest
1个回答
0
投票

你试图通过key URL传递dict,导致问题。

有关进一步的方法,请参阅this documentation

试试以下代码:

import os
import unittest
from mock import patch
with patch.dict('os.environ',{"devUrl":"https://devurl.com","testUrl":"https://testurl.com"}):
     print(os.environ['devUrl'])
     print(os.environ['testUrl'])
© www.soinside.com 2019 - 2024. All rights reserved.