内置 fixtures 之 tmpdir:
tmpdir 的作用是:在本地生成临时文件夹,并返回文件对象;
代码演示:
先写个测试用例,调用 tmpdir,执行一下看看:
class TestDemoA:
def test_A_001(self, tmpdir):
time.sleep(2)
print('\n',tmpdir)
if __name__ == '__main__':
pytest.main(['-s'])
# 执行结果:
============================= test session starts =============================
test_Z.py::TestDemoA::test_A_001
C:\Users\MyPC\AppData\Local\Temp\pytest-of-MyPC\pytest-0\test_A_0010
PASSED
============================== 2 passed in 3.04s ==============================
Process finished with exit code 0

可以看到控制台有打印出生成的临时文件夹的目录,并且电脑本地也生成了文件夹!
然后我们发现临时文件目录是有规律的,改下代码多执行几次看看:
class TestDemoA:
def test_A_001(self, tmpdir):
time.sleep(2)
print('\n',tmpdir)
def test_A_002(self, tmpdir):
time.sleep(2)
print('\n',tmpdir)
if __name__ == '__main__':
pytest.main(['-s'])
# 执行结果:
============================= test session starts =============================
test_Z.py::TestDemoA::test_A_001
C:\Users\MyPC\AppData\Local\Temp\pytest-of-MyPC\pytest-8\test_A_0010
PASSED
test_Z.py::TestDemoA::test_A_002
C:\Users\MyPC\AppData\Local

最低0.47元/天 解锁文章
1075

被折叠的 条评论
为什么被折叠?



