擅长:python、mysql、java
<p><code>test.support.EnvironmentVarGuard</code>是一个内部API,可以通过中断(向后不兼容)更改从一个版本更改到另一个版本。<strong>事实上,整个<code>test</code>包只供内部使用。</strong>在测试包文档页上明确声明,它用于核心库的内部测试,而不是公共API。(见下面的链接)</p>
<p>您应该在python的标准库中使用<a href="https://docs.python.org/3.4/library/unittest.mock.html#patch-dict" rel="noreferrer">^{<cd3>}</a>。它可以用作上下文管理器、装饰器或类装饰器。请参阅下面从正式Python文档复制的示例代码。</p>
<pre><code>import os
from unittest.mock import patch
with patch.dict('os.environ', {'newkey': 'newvalue'}):
print(os.environ['newkey']) # should print out 'newvalue'
assert 'newkey' in os.environ # should be True
assert 'newkey' not in os.environ # should be True
</code></pre>
<p>更新:对于那些没有完全阅读文档并且可能错过了注释的人,请阅读更多的<code>test</code>包注释</p>
<p><a href="https://docs.python.org/2/library/test.html" rel="noreferrer">https://docs.python.org/2/library/test.html</a>或</p>
<p><a href="https://docs.python.org/3/library/test.html" rel="noreferrer">https://docs.python.org/3/library/test.html</a></p>