擅长:python、mysql、java
<p>不,你做的每件事都是正确的。我也不能让它工作。另一方面,pytest有fixture <code>tmpdir</code>,它可以满足您在测试中的需要:为您的测试提供唯一的临时目录。在</p>
<p>下面是一个测试示例:</p>
<pre><code>def test_one(tmpdir):
test_file = tmpdir.join('dir', 'file').ensure(file=True)
test_file.write('test\ntest\n')
assert test_file.read().rsplit() == ['test', 'test']
</code></pre>
<p>另一方面,pytest使用py.path路径,它有py.path.local对象,并使用<code>tempfile.mkdtemp</code>创建临时目录对象:</p>
^{pr2}$